Alexandria 2.31.0
SDC-CH common library for the Euclid project
Loading...
Searching...
No Matches
SemaphorePosix.icpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2012-2021 Euclid Science Ground Segment
3 *
4 * This library is free software; you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License as published by the Free
6 * Software Foundation; either version 3.0 of the License, or (at your option)
7 * any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 * details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <cerrno>
20#include <semaphore.h>
21#include <system_error>
22
23namespace Euclid {
24class Semaphore::SemaphoreImpl {
25public:
26 explicit SemaphoreImpl(unsigned int i) {
27 if (sem_init(&m_semaphore, 0, i) < 0) {
28 throw std::system_error(errno, std::system_category());
29 }
30 }
31
32 ~SemaphoreImpl() {
33 sem_destroy(&m_semaphore);
34 }
35
36 void post() {
37 if (sem_post(&m_semaphore) < 0) {
38 throw std::system_error(errno, std::system_category());
39 }
40 }
41
42 void wait() {
43 if (sem_wait(&m_semaphore) != 0) {
44 throw std::system_error(errno, std::system_category());
45 }
46 }
47
48 bool try_acquire() {
49 if (sem_trywait(&m_semaphore) == 0) {
50 return true;
51 }
52 if (errno == EAGAIN) {
53 return false;
54 }
55 throw std::system_error(errno, std::system_category());
56 }
57
58 bool try_acquire_until(std::chrono::system_clock::time_point abs_time) {
59 using std::chrono::time_point_cast;
60 auto seconds = time_point_cast<std::chrono::seconds>(abs_time);
61 auto nseconds =
62 time_point_cast<std::chrono::nanoseconds>(abs_time) - time_point_cast<std::chrono::nanoseconds>(seconds);
63
64 struct timespec timeout;
65 timeout.tv_sec = seconds.time_since_epoch().count();
66 timeout.tv_nsec = nseconds.count();
67 if (sem_timedwait(&m_semaphore, &timeout) == 0) {
68 return true;
69 }
70 if (errno == ETIMEDOUT) {
71 return false;
72 }
73 throw std::system_error(errno, std::system_category());
74 }
75
76private:
77 sem_t m_semaphore;
78};
79} // namespace Euclid