Deskflow 1.22.0.197
Keyboard and mouse sharing utility
Loading...
Searching...
No Matches
CondVar.h
Go to the documentation of this file.
1/*
2 * Deskflow -- mouse and keyboard sharing utility
3 * SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
4 * SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
5 * SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
6 */
7
8#pragma once
9
10#include "common/Common.h"
11#include "mt/Mutex.h"
12
13class Stopwatch;
14
16
23{
24public:
30 explicit CondVarBase(Mutex *mutex);
32
34
35
37
43 void lock() const;
44
46 void unlock() const;
47
49
53 void signal();
54
56
59 void broadcast();
60
62
64
66
85 bool wait(double timeout = -1.0) const;
86
88
97 bool wait(Stopwatch &timer, double timeout) const;
98
100
103 Mutex *getMutex() const;
104
106
107private:
108 // not implemented
109 CondVarBase(const CondVarBase &);
110 CondVarBase &operator=(const CondVarBase &);
111
112private:
113 Mutex *m_mutex;
114 ArchCond m_cond;
115};
116
118
121template <class T> class CondVar : public CondVarBase
122{
123public:
125 CondVar(Mutex *mutex, const T &value);
127 CondVar(const CondVar &);
129
131
132
134
139
141
145 CondVar &operator=(const T &v);
146
148
150
152
156 operator const volatile T &() const;
157
159
160private:
161 volatile T m_data;
162};
163
164template <class T> inline CondVar<T>::CondVar(Mutex *mutex, const T &data) : CondVarBase(mutex), m_data(data)
165{
166 // do nothing
167}
168
169template <class T> inline CondVar<T>::CondVar(const CondVar &cv) : CondVarBase(cv.getMutex()), m_data(cv.m_data)
170{
171 // do nothing
172}
173
174template <class T> inline CondVar<T>::~CondVar() = default;
175
176template <class T> inline CondVar<T> &CondVar<T>::operator=(const CondVar<T> &cv)
177{
178 m_data = cv.m_data;
179 return *this;
180}
181
182template <class T> inline CondVar<T> &CondVar<T>::operator=(const T &data)
183{
184 m_data = data;
185 return *this;
186}
187
188template <class T> inline CondVar<T>::operator const volatile T &() const
189{
190 return m_data;
191}
static int void FAR * data
Definition ArchNetworkWinsock.cpp:34
static fd_set FAR fd_set FAR fd_set FAR const struct timeval FAR * timeout
Definition ArchNetworkWinsock.cpp:39
ArchCondImpl * ArchCond
Opaque condition variable type. An opaque type representing a condition variable.
Definition IArchMultithread.h:25
void signal()
Signal the condition variable.
Definition CondVar.cpp:37
void lock() const
Lock the condition variable's mutex.
Definition CondVar.cpp:27
CondVarBase(Mutex *mutex)
Definition CondVar.cpp:16
void broadcast()
Signal the condition variable.
Definition CondVar.cpp:42
~CondVarBase()
Definition CondVar.cpp:22
void unlock() const
Unlock the condition variable's mutex.
Definition CondVar.cpp:32
bool wait(double timeout=-1.0) const
Wait on the condition variable.
Definition CondVar.cpp:65
Mutex * getMutex() const
Get the mutex.
Definition CondVar.cpp:70
CondVar & operator=(const CondVar &cv)
Assigns the value of cv to this.
Definition CondVar.h:176
CondVar(Mutex *mutex, const T &value)
Initialize using value.
Definition CondVar.h:164
Mutual exclusion.
Definition Mutex.h:22
A timer class.
Definition Stopwatch.h:18