join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
semaphore.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_SEMAPHORE_HPP
26#define JOIN_CORE_SEMAPHORE_HPP
27
28// libjoin.
29#include <join/error.hpp>
30#include <join/utils.hpp>
31
32// C++.
33#include <chrono>
34
35// C.
36#include <semaphore.h>
37#include <sys/stat.h>
38#include <fcntl.h>
39#include <climits>
40
41namespace join
42{
47 {
48 public:
53 Semaphore (size_t value = 0);
54
62 Semaphore (const std::string& name, size_t value = 0, int oflag = O_CREAT, mode_t mode = 0644);
63
68 Semaphore (const Semaphore& other) = delete;
69
75 Semaphore& operator= (const Semaphore& other) = delete;
76
80 ~Semaphore () noexcept;
81
85 void post () noexcept;
86
90 void wait () noexcept;
91
96 bool tryWait () noexcept;
97
103 template <class Rep, class Period>
104 bool timedWait (std::chrono::duration<Rep, Period> timeout)
105 {
106 struct timespec ts = toTimespec (std::chrono::system_clock::now () + timeout);
107 if (((_named) ? ::sem_timedwait (_named_handle, &ts) : ::sem_timedwait (&_unnamed_handle, &ts)) == -1)
108 {
109 lastError = std::error_code (errno, std::generic_category ());
110 return false;
111 }
112
113 return true;
114 }
115
120 int value () noexcept;
121
122 private:
124 union
125 {
128 };
129
131 bool _named = false;
132 };
133
139 {
140 public:
145 SharedSemaphore (size_t value = 0);
146
151 SharedSemaphore (const SharedSemaphore& other) = delete;
152
159
163 ~SharedSemaphore () noexcept;
164
168 void post () noexcept;
169
173 void wait () noexcept;
174
179 bool tryWait () noexcept;
180
186 template <class Rep, class Period>
187 bool timedWait (std::chrono::duration<Rep, Period> timeout)
188 {
189 struct timespec ts = toTimespec (std::chrono::system_clock::now () + timeout);
190 if (sem_timedwait (&_handle, &ts) == -1)
191 {
192 lastError = std::error_code (errno, std::generic_category ());
193 return false;
194 }
195
196 return true;
197 }
198
203 int value () noexcept;
204
205 private:
207 sem_t _handle;
208 };
209}
210
211#endif
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition semaphore.hpp:47
void wait() noexcept
decrements the internal counter or blocks until it can
Definition semaphore.cpp:99
Semaphore(const Semaphore &other)=delete
copy constructor.
sem_t _unnamed_handle
Definition semaphore.hpp:127
~Semaphore() noexcept
destroy instance.
Definition semaphore.cpp:67
sem_t * _named_handle
Definition semaphore.hpp:126
int value() noexcept
get semaphore value.
Definition semaphore.cpp:129
bool tryWait() noexcept
tries to decrement the internal counter without blocking.
Definition semaphore.cpp:115
Semaphore & operator=(const Semaphore &other)=delete
copy assignment operator.
void post() noexcept
increments the internal counter and unblocks acquirers.
Definition semaphore.cpp:83
Semaphore(size_t value=0)
create an named semaphore.
Definition semaphore.cpp:41
bool timedWait(std::chrono::duration< Rep, Period > timeout)
tries to decrement the internal counter, blocking for up to a duration time.
Definition semaphore.hpp:104
class used to protect shared data from being simultaneously accessed by multiple process via a shared...
Definition semaphore.hpp:139
void post() noexcept
increments the internal counter and unblocks acquirers.
Definition semaphore.cpp:168
int value() noexcept
get semaphore value.
Definition semaphore.cpp:200
bool timedWait(std::chrono::duration< Rep, Period > timeout)
tries to decrement the internal counter, blocking for up to a duration time.
Definition semaphore.hpp:187
SharedSemaphore(const SharedSemaphore &other)=delete
copy constructor.
SharedSemaphore & operator=(const SharedSemaphore &other)=delete
copy assignment operator.
void wait() noexcept
decrements the internal counter or blocks until it can
Definition semaphore.cpp:177
bool tryWait() noexcept
tries to decrement the internal counter without blocking.
Definition semaphore.cpp:186
~SharedSemaphore() noexcept
destroy instance.
Definition semaphore.cpp:159
SharedSemaphore(size_t value=0)
create instance.
Definition semaphore.cpp:147
Definition acceptor.hpp:32
struct timespec toTimespec(std::chrono::time_point< Clock, Duration > timePoint)
converts time_point to timespec.
Definition utils.hpp:431
Definition error.hpp:137