| 1 |
#ifndef SIGNAL_H |
| 2 |
#define SIGNAL_H |
| 3 |
|
| 4 |
#include <mutex> |
| 5 |
#include <condition_variable> |
| 6 |
|
| 7 |
class Signal { |
| 8 |
public: |
| 9 |
Signal() : mFlag(false), mWaiting(false) {} |
| 10 |
void wait() { |
| 11 |
std::unique_lock<std::mutex> lock(mMutex); |
| 12 |
while (!mFlag) { |
| 13 |
mWaiting = true; |
| 14 |
mCond.wait(lock); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
std::cv_status waitFor(std::chrono::milliseconds ms) { |
| 19 |
std::unique_lock<std::mutex> lock(mMutex); |
| 20 |
return mCond.wait_for(lock, ms); |
| 21 |
} |
| 22 |
|
| 23 |
void notify() { |
| 24 |
std::unique_lock<std::mutex> lock(mMutex); |
| 25 |
mFlag = true; |
| 26 |
mCond.notify_all(); |
| 27 |
} |
| 28 |
|
| 29 |
void reset() { |
| 30 |
std::unique_lock<std::mutex> lock(mMutex); |
| 31 |
mFlag = false; |
| 32 |
mWaiting = false; |
| 33 |
} |
| 34 |
|
| 35 |
bool isWaiting() { |
| 36 |
return mWaiting; |
| 37 |
} |
| 38 |
|
| 39 |
private: |
| 40 |
bool mFlag; |
| 41 |
bool mWaiting; |
| 42 |
std::mutex mMutex; |
| 43 |
std::condition_variable mCond; |
| 44 |
}; |
| 45 |
|
| 46 |
#endif |