join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
semaphore.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_SEMAPHORE_HPP__
26#define __JOIN_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::make_error_code (static_cast <std::errc> (errno));
110 return false;
111 }
112
113 return true;
114 }
115
120 int value () noexcept;
121
122 private:
124 union {
127 };
128
130 bool _named = false;
131 };
132
137 {
138 public:
143 SharedSemaphore (size_t value = 0);
144
149 SharedSemaphore (const SharedSemaphore& other) = delete;
150
157
161 ~SharedSemaphore () noexcept;
162
166 void post () noexcept;
167
171 void wait () noexcept;
172
177 bool tryWait () noexcept;
178
184 template <class Rep, class Period>
185 bool timedWait (std::chrono::duration <Rep, Period> timeout)
186 {
187 struct timespec ts = toTimespec (std::chrono::system_clock::now () + timeout);
188 if (sem_timedwait (&_handle, &ts) == -1)
189 {
190 lastError = std::make_error_code (static_cast <std::errc> (errno));
191 return false;
192 }
193
194 return true;
195 }
196
201 int value () noexcept;
202
203 private:
205 sem_t _handle;
206 };
207}
208
209#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:126
~Semaphore() noexcept
destroy instance.
Definition semaphore.cpp:67
sem_t * _named_handle
Definition semaphore.hpp:125
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:137
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:185
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:423
Definition error.hpp:106