| 1 |
#ifndef _THREADUTILS_H_ |
| 2 |
#define _THREADUTILS_H_ |
| 3 |
|
| 4 |
#include <stdexcept> |
| 5 |
#include <type_traits> |
| 6 |
|
| 7 |
#include "tinycthread.h" |
| 8 |
#include "timeconv.h" |
| 9 |
|
| 10 |
class ConditionVariable; |
| 11 |
|
| 12 |
class Mutex { |
| 13 |
friend class ConditionVariable; |
| 14 |
tct_mtx_t _m; |
| 15 |
|
| 16 |
public: |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
Mutex(int type) { |
| 28 |
if (tct_mtx_init(&_m, type) != tct_thrd_success) { |
| 29 |
throw std::runtime_error("Mutex creation failed"); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
Mutex(const Mutex&) = delete; |
| 35 |
Mutex& operator=(const Mutex&) = delete; |
| 36 |
|
| 37 |
virtual ~Mutex() { |
| 38 |
tct_mtx_destroy(&_m); |
| 39 |
} |
| 40 |
|
| 41 |
void lock() { |
| 42 |
if (tct_mtx_lock(&_m) != tct_thrd_success) { |
| 43 |
throw std::runtime_error("Mutex failed to lock"); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
bool tryLock() { |
| 48 |
int res = tct_mtx_trylock(&_m); |
| 49 |
if (res == tct_thrd_success) { |
| 50 |
return true; |
| 51 |
} else if (res == tct_thrd_busy) { |
| 52 |
return false; |
| 53 |
} else { |
| 54 |
throw std::runtime_error("Mutex failed to trylock"); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
void unlock() { |
| 59 |
if (tct_mtx_unlock(&_m) != tct_thrd_success) { |
| 60 |
throw std::runtime_error("Mutex failed to unlock"); |
| 61 |
} |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
class Guard { |
| 66 |
Mutex* _mutex; |
| 67 |
|
| 68 |
public: |
| 69 |
Guard(Mutex* mutex) : _mutex(mutex) { |
| 70 |
_mutex->lock(); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
Guard(const Guard&) = delete; |
| 75 |
Guard& operator=(const Guard&) = delete; |
| 76 |
|
| 77 |
~Guard() { |
| 78 |
_mutex->unlock(); |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
class ConditionVariable { |
| 83 |
tct_mtx_t* _m; |
| 84 |
tct_cnd_t _c; |
| 85 |
|
| 86 |
public: |
| 87 |
ConditionVariable(Mutex& mutex) : _m(&mutex._m) { |
| 88 |
|
| 89 |
|
| 90 |
if (!std::is_integral<time_t>::value) |
| 91 |
throw std::runtime_error("Integral time_t type expected"); |
| 92 |
|
| 93 |
|
| 94 |
if (!std::is_signed<time_t>::value) |
| 95 |
throw std::runtime_error("Signed time_t type expected"); |
| 96 |
|
| 97 |
if (tct_cnd_init(&_c) != tct_thrd_success) |
| 98 |
throw std::runtime_error("Condition variable failed to initialize"); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
ConditionVariable(const ConditionVariable&) = delete; |
| 103 |
ConditionVariable& operator=(const ConditionVariable&) = delete; |
| 104 |
|
| 105 |
virtual ~ConditionVariable() { |
| 106 |
tct_cnd_destroy(&_c); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
void signal() { |
| 111 |
if (tct_cnd_signal(&_c) != tct_thrd_success) |
| 112 |
throw std::runtime_error("Condition variable failed to signal"); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
void broadcast() { |
| 117 |
if (tct_cnd_broadcast(&_c) != tct_thrd_success) |
| 118 |
throw std::runtime_error("Condition variable failed to broadcast"); |
| 119 |
} |
| 120 |
|
| 121 |
void wait() { |
| 122 |
if (tct_cnd_wait(&_c, _m) != tct_thrd_success) |
| 123 |
throw std::runtime_error("Condition variable failed to wait"); |
| 124 |
} |
| 125 |
|
| 126 |
bool timedwait(double timeoutSecs) { |
| 127 |
timespec ts; |
| 128 |
if (timespec_get(&ts, TIME_UTC) != TIME_UTC) { |
| 129 |
throw std::runtime_error("timespec_get failed"); |
| 130 |
} |
| 131 |
|
| 132 |
ts = addSeconds(ts, timeoutSecs); |
| 133 |
|
| 134 |
int res = tct_cnd_timedwait(&_c, _m, &ts); |
| 135 |
if (res == tct_thrd_success) { |
| 136 |
return true; |
| 137 |
} else if (res == tct_thrd_timedout) { |
| 138 |
return false; |
| 139 |
} else { |
| 140 |
throw std::runtime_error("Condition variable failed to timedwait"); |
| 141 |
} |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
#endif |