forked from Dushistov/qt_monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphore.hpp
45 lines (42 loc) · 1.12 KB
/
semaphore.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#pragma once
#include <chrono>
#include <condition_variable>
#include <mutex>
namespace qt_monkey_common
{
// implement own semaphore, because QSemaphore
// not annotated for -fsanitize=thread
class Semaphore final
{
public:
explicit Semaphore(int n) : count_(n) {}
Semaphore(const Semaphore &) = delete;
Semaphore &operator=(const Semaphore &) = delete;
void acquire(int n = 1)
{
std::unique_lock<std::mutex> lock{mutex_};
cv_.wait(lock, [this, n] { return count_ >= n; });
count_ -= n;
}
template <class Rep, class Period>
bool tryAcquire(int n, const std::chrono::duration<Rep, Period> &d)
{
std::unique_lock<std::mutex> lock{mutex_};
const bool finished
= cv_.wait_for(lock, d, [this, n] { return count_ >= n; });
if (finished)
count_ -= n;
return finished;
}
void release(int n = 1)
{
std::lock_guard<std::mutex> lock{mutex_};
count_ += n;
cv_.notify_one();
}
private:
int count_ = 0;
std::mutex mutex_;
std::condition_variable cv_;
};
} // namespace qt_monkey_common