ORIGINAL
Loading...
Searching...
No Matches
condition.h
Go to the documentation of this file.
1#ifndef CONDITION_H
2#define CONDITION_H
3#include "mutex.h"
4#include "zeit.h"
5
23namespace original
24{
38 {
39 public:
41 explicit conditionBase() = default;
42
49 virtual void wait(mutexBase& mutex) = 0;
50
59 virtual bool waitFor(mutexBase& mutex, time::duration d) = 0;
60
69 template<typename Pred>
70 void wait(mutexBase& mutex, Pred predicate) noexcept(noexcept(predicate()));
71
81 template<typename Pred>
82 bool waitFor(mutexBase& mutex, const time::duration& d, Pred predicate) noexcept(noexcept(predicate()));
83
88 virtual void notify() = 0;
89
94 virtual void notifyAll() = 0;
95
109 void notifySome(u_integer n);
110
112 virtual ~conditionBase() = default;
113
115 conditionBase(const conditionBase&) = delete;
116
119 };
120
129 {
130 pthread_cond_t cond_;
131
132 public:
133 // Inherit template methods from conditionBase
136
141 explicit pCondition();
142
149 void wait(mutexBase& mutex) override;
150
159 bool waitFor(mutexBase& mutex, time::duration d) override;
160
165 void notify() override;
166
171 void notifyAll() override;
172
177 ~pCondition() override;
178 };
179}
180
181template<typename Pred>
183 while (!predicate()){
184 this->wait(mutex);
185 }
186}
187
188template<typename Pred>
190 const time::point start = time::point::now();
191 while (!predicate()) {
192 auto elapsed = time::point::now() - start;
193 if (elapsed >= d)
194 return false;
195 if (!this->waitFor(mutex, d - elapsed))
196 return false;
197 if (predicate())
198 return true;
199 }
200 return true;
201}
202
204 if (n == 0){
205 return;
206 }
207 if (n == 1){
208 this->notify();
209 } else {
210 this->notifyAll();
211 }
212}
213
215{
216 if (const int code = pthread_cond_init(&this->cond_, nullptr); code != 0)
217 {
218 throw sysError("Failed to initialize condition variable (pthread_cond_init returned " + printable::formatString(code) + ")");
219 }
220}
221
223{
224 const auto p_mutex = dynamic_cast<pMutex*>(&mutex);
225 if (!p_mutex) {
226 throw valueError("Invalid mutex type for condition variable: must be pMutex");
227 }
228
229 const auto handle = static_cast<pMutex::native_handle*>(p_mutex->nativeHandle());
230 if (const int code = pthread_cond_wait(&this->cond_, handle); code != 0) {
231 throw sysError("Failed to wait on condition variable (pthread_cond_wait returned " + printable::formatString(code) + ")");
232 }
233}
234
236{
237 const auto p_mutex = dynamic_cast<pMutex*>(&mutex);
238 if (!p_mutex) {
239 throw valueError("Invalid mutex type for condition variable: must be pMutex");
240 }
241
242 const auto deadline = time::point::now() + d;
243 const auto ts = deadline.toTimespec();
244 const auto handle = static_cast<pMutex::native_handle*>(p_mutex->nativeHandle());
245 const int code = pthread_cond_timedwait(&this->cond_, handle, &ts);
246 if (code == 0) return true;
247 if (code == ETIMEDOUT) return false;
248 throw sysError("Failed to timed wait on condition variable (pthread_cond_timed-wait returned " + printable::formatString(code) + ")");
249}
250
252{
253 if (const int code = pthread_cond_signal(&this->cond_); code != 0) {
254 throw sysError("Failed to signal condition variable (pthread_cond_signal returned " + printable::formatString(code) + ")");
255 }
256}
257
259{
260 if (const int code = pthread_cond_broadcast(&this->cond_); code != 0) {
261 throw sysError("Failed to broadcast condition variable (pthread_cond_broadcast returned " + printable::formatString(code) + ")");
262 }
263}
264
266{
267 if (const int code = pthread_cond_destroy(&this->cond_); code != 0) {
268 std::cerr << "Warning: Failed to destroy condition variable (pthread_cond_destroy returned "
269 << code << ")" << std::endl;
270 }
271}
272
273#endif //CONDITION_H
Abstract base class for condition variable implementations.
Definition condition.h:38
virtual void notifyAll()=0
Notifies all waiting threads.
conditionBase & operator=(const conditionBase &)=delete
Deleted copy assignment operator.
virtual void wait(mutexBase &mutex)=0
Waits for notification while holding the mutex.
conditionBase(const conditionBase &)=delete
Deleted copy constructor.
virtual bool waitFor(mutexBase &mutex, time::duration d)=0
Waits for notification with timeout.
void notifySome(u_integer n)
Notifies a specified number of waiting threads.
Definition condition.h:203
virtual ~conditionBase()=default
Virtual destructor.
conditionBase()=default
Default constructor.
virtual void notify()=0
Notifies one waiting thread.
Abstract base class for mutex implementations.
Definition mutex.h:30
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:37
POSIX condition variable implementation.
Definition condition.h:129
void wait(mutexBase &mutex) override
Waits for notification while holding the mutex.
Definition condition.h:222
bool waitFor(mutexBase &mutex, time::duration d) override
Waits for notification with timeout.
Definition condition.h:235
void notifyAll() override
Notifies all waiting threads.
Definition condition.h:258
void notify() override
Notifies one waiting thread.
Definition condition.h:251
~pCondition() override
Destroys the condition variable.
Definition condition.h:265
pCondition()
Constructs and initializes the condition variable.
Definition condition.h:214
POSIX thread mutex implementation.
Definition mutex.h:153
static std::string formatString(const TYPE &t)
Universal value-to-string conversion with type-specific formatting.
Definition printable.h:339
Exception for generic system failure.
Definition error.h:413
Represents a time duration with nanosecond precision.
Definition zeit.h:134
Represents a point in time with nanosecond precision.
Definition zeit.h:374
static point now()
Gets current time point.
Definition zeit.h:1352
Exception for invalid parameter values.
Definition error.h:219
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
Cross-platform mutex and lock management utilities.
Main namespace for the project Original.
Definition algorithms.h:21
Time and date handling utilities.