-
Notifications
You must be signed in to change notification settings - Fork 8
/
Semaphore.hpp
64 lines (54 loc) · 1.53 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* @file
* A classic Semaphore for synchronization in multi thread environments.
* @author Colin Graf
*/
#pragma once
#include <nstd/Base.hpp>
/**
* Encapsulation of a semaphore object.
*/
class Semaphore
{
public:
/**
* Construct a new semaphore object.
* @param value The initial value of the semaphore counter.
*/
Semaphore(uint value = 0);
/** Destructor. */
~Semaphore();
/**
* Increment the semaphore counter.
*/
void signal();
/**
* Decrement the semaphore counter. The method returns immediately if the
* counter is greater than zero. Otherwise it blocks the execution of the
* calling thread until another thread increases the semaphore counter.
* @return Whether the semaphore counter was successfully decremented or not.
*/
bool wait();
/**
* Decrement the semaphore counter. The method returns immediately if the
* counter is greater than zero. Otherwise it blocks the execution of the
* calling thread until another thread increases the semaphore counter or
* a timeout occurs.
* @param timeout The maximum time to wait. (in ms).
* @return Whether the semaphore counter was successfully decremented or not.
*/
bool wait(int64 timeout);
/**
* Try to decrement the semaphore counter. This method returns immediately.
* @return Whether the semaphore counter was successfully decremented or not.
*/
bool tryWait();
private:
#ifdef _WIN32
void* handle;
#else
int64 data[4]; // sizeof(sem_t)
#endif
Semaphore(const Semaphore&);
Semaphore& operator=(const Semaphore&);
};