Newer
Older
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @author Daniel Mueller
*
* @copyright Copyright 2021 Daniel Mueller. All rights reserved.
*/
#ifndef _CPPSEMAPHORE_HPP
#define _CPPSEMAPHORE_HPP
extern "C" {
#include <semaphore.h>
}
/**
* @brief The Semaphore class provides a simple CPP wrapper for the sem_*
* functions from the C "semaphore.h" library. This only uses the thread-shared
* semaphore, not process shared.
*/
class Semaphore
{
private:
/**
* @brief Pointer to the underlying C semaphore.
*/
sem_t * _semaphore;
public:
/**
* @brief Initialize the Semaphore and set the start value.
*
* @param initialValue The Semaphore counter value to start with.
* 0 by default.
*
* @see sem_init
*/
Semaphore(int initialValue = 0);
/**
* @brief The semaphore must not be copied, so the copy constructors is deleted.
*
* @param other The reference to copy from.
*/
Semaphore(const Semaphore & other) = delete;
/**
* @brief The semaphore must not be copied, so the copy assignment is deleted.
*
* @param other The reference to copy from.
*/
Semaphore & operator=(const Semaphore & other) = delete;
/**
* @brief Create the instance by taking over an existing Semaphore.
*
* @warning The moved-from instance is no longer valid to use after the move.
*
* @param other The reference to take over.
*/
Semaphore(Semaphore && other);
/**
* @brief Take over an existing Semaphore. The internal samaphore is destroyed
* and replace by the internal semaphore of the other instance.
*
* @warning The moved-from instance is no longer valid to use after the move.
*
* @param other The reference to take over.
*/
Semaphore & operator=(Semaphore && other);
/**
* @brief Destructor destroys the underlying semaphore and frees the memory.
*
* @see sem_destroy
*/
~Semaphore();
/**
* @brief Incremet the Semaphore.
*
* Increments the underlying semaphore using sem_post. If another thread is
* waiting, it will be woken up.
*
* @see sem_post
*/
void post();
/**
* @brief Decrement the Semaphore or wait if 0.
*
* Decrements the underlying semaphore using sem_wait. If the semaphore has
* the value 0, the function blocks until it can decrement (post was
* called).
*
* @see sem_wait
*/
void wait();
/**
* @brief Same as wait() but doesn't block if the value is 0.
*
* Tries to decrement the underlying semaphore using
* sem_trywait. If the value is 0, it instantly returns false.
*
* @return True if the semaphore was decremented, false otherwise.
*
* @see sem_trywait
*/
bool tryWait();
/**
* @brief Same as wait() but only blocks for a specified ammount of time.
*
* Tries to decrement the underlying semaphore using sem_timedwait. If the
* value is 0, it will block until it can decrement or the specified time
* in ms has passed.
*
* @param ms The time in milliseconds that will be waited if the current
* value is 0, before returning false.
*
* @return True if the value could be decremented, false if the ms time has
* passed without the possibility to decrement.
*
* @see sem_timedwait
*/
bool timedWait(long ms);
/**
*
* @brief Get the current Semaphore value.
*
* Get the current value from the underlying semaphore using sem_getvalue.
*
* @return The current semaphore value. (Due to the nature of semaphores
* and threads, the real value might have already changed when parsing this
* result)
*
* @see sem_getvalue
*/
int getValue();
};
#endif // _SEMAPHORE_HPP