Deskflow 1.24.0.365
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 "mt/Mutex.h"
11
12class Stopwatch;
13
15
22{
23public:
29 explicit CondVarBase(Mutex *mutex);
31
33
34
36
42 void lock() const;
43
45 void unlock() const;
46
48
52 void signal();
53
55
58 void broadcast();
59
61
63
65
84 bool wait(double timeout = -1.0) const;
85
87
96 bool wait(Stopwatch &timer, double timeout) const;
97
99
102 Mutex *getMutex() const;
103
105
106private:
107 // not implemented
108 CondVarBase(const CondVarBase &);
109 CondVarBase &operator=(const CondVarBase &);
110
111private:
112 Mutex *m_mutex;
113 ArchCond m_cond;
114};
115
117
120template <class T> class CondVar : public CondVarBase
121{
122public:
124 CondVar(Mutex *mutex, const T &value);
126 CondVar(const CondVar &);
128
130
131
133
138
140
144 CondVar &operator=(const T &v);
145
147
149
151
155 operator const volatile T &() const;
156
158
159private:
160 volatile T m_data;
161};
162
163template <class T> inline CondVar<T>::CondVar(Mutex *mutex, const T &data) : CondVarBase(mutex), m_data(data)
164{
165 // do nothing
166}
167
168template <class T> inline CondVar<T>::CondVar(const CondVar &cv) : CondVarBase(cv.getMutex()), m_data(cv.m_data)
169{
170 // do nothing
171}
172
173template <class T> inline CondVar<T>::~CondVar() = default;
174
175template <class T> inline CondVar<T> &CondVar<T>::operator=(const CondVar<T> &cv)
176{
177 m_data = cv.m_data;
178 return *this;
179}
180
181template <class T> inline CondVar<T> &CondVar<T>::operator=(const T &data)
182{
183 m_data = data;
184 return *this;
185}
186
187template <class T> inline CondVar<T>::operator const volatile T &() const
188{
189 return m_data;
190}
static int void FAR * data
Definition ArchNetworkWinsock.cpp:35
static fd_set FAR fd_set FAR fd_set FAR const struct timeval FAR * timeout
Definition ArchNetworkWinsock.cpp:40
ArchCondImpl * ArchCond
Opaque condition variable type. An opaque type representing a condition variable.
Definition IArchMultithread.h:26
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:175
CondVar(Mutex *mutex, const T &value)
Initialize using value.
Definition CondVar.h:163
Mutual exclusion.
Definition Mutex.h:22
A timer class.
Definition Stopwatch.h:16