Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
semaphore.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Roc Streaming authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_core/target_posix_ext/roc_core/semaphore.h
10//! @brief Semaphore.
11
12#ifndef ROC_CORE_SEMAPHORE_H_
13#define ROC_CORE_SEMAPHORE_H_
14
15#include <semaphore.h>
16
17#include "roc_core/atomic.h"
19#include "roc_core/time.h"
20
21namespace roc {
22namespace core {
23
24//! Semaphore.
25class Semaphore : public NonCopyable<> {
26public:
27 //! Initialize semaphore with given counter.
28 explicit Semaphore(unsigned counter = 0);
29
30 ~Semaphore();
31
32 //! Wait until the counter becomes non-zero, decrement it, and return true.
33 //! If deadline expires before the counter becomes non-zero, returns false.
34 //! Deadline should be in the same time domain as core::timestamp().
35 bool timed_wait(nanoseconds_t deadline);
36
37 //! Wait until the counter becomes non-zero, decrement it, and return.
38 void wait();
39
40 //! Increment counter and wake up blocked waits.
41 //! This method is lock-free at least on recent glibc and musl versions
42 //! (which implement POSIX semaphores using a futex and an atomic).
43 void post();
44
45private:
46 sem_t sem_;
47 Atomic<int> guard_;
48};
49
50} // namespace core
51} // namespace roc
52
53#endif // ROC_CORE_SEMAPHORE_H_
Atomic.
Atomic integer. Provides sequential consistency. For a fine-grained memory order control,...
Definition: atomic.h:26
Semaphore(unsigned counter=0)
Initialize semaphore with given counter.
bool timed_wait(nanoseconds_t deadline)
Wait until the counter becomes non-zero, decrement it, and return true. If deadline expires before th...
void wait()
Wait until the counter becomes non-zero, decrement it, and return.
void post()
Increment counter and wake up blocked waits. This method is lock-free at least on recent glibc and mu...
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Semaphore.
Time definitions.