2 * Copyright (C) 2012-2021 Euclid Science Ground Segment
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)
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
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
21#include <system_error>
24class Semaphore::SemaphoreImpl {
26 explicit SemaphoreImpl(unsigned int i) {
27 if (sem_init(&m_semaphore, 0, i) < 0) {
28 throw std::system_error(errno, std::system_category());
33 sem_destroy(&m_semaphore);
37 if (sem_post(&m_semaphore) < 0) {
38 throw std::system_error(errno, std::system_category());
43 if (sem_wait(&m_semaphore) != 0) {
44 throw std::system_error(errno, std::system_category());
49 if (sem_trywait(&m_semaphore) == 0) {
52 if (errno == EAGAIN) {
55 throw std::system_error(errno, std::system_category());
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);
62 time_point_cast<std::chrono::nanoseconds>(abs_time) - time_point_cast<std::chrono::nanoseconds>(seconds);
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) {
70 if (errno == ETIMEDOUT) {
73 throw std::system_error(errno, std::system_category());