Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
cond.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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/roc_core/cond.h
10 //! @brief Condition variable.
11 
12 #ifndef ROC_CORE_COND_H_
13 #define ROC_CORE_COND_H_
14 
15 #include <errno.h>
16 #include <pthread.h>
17 
18 #include "roc_core/atomic.h"
19 #include "roc_core/attributes.h"
20 #include "roc_core/mutex.h"
21 #include "roc_core/noncopyable.h"
22 #include "roc_core/time.h"
23 
24 namespace roc {
25 namespace core {
26 
27 //! Condition variable.
28 class Cond : public NonCopyable<> {
29 public:
30  //! Initialize.
31  Cond(const Mutex& mutex);
32 
33  //! Destroy.
34  ~Cond();
35 
36  //! Wait with timeout.
37  //! @returns false if timeout expired.
39 
40  //! Wait.
41  void wait() const;
42 
43  //! Wake up one pending waits.
44  void signal() const;
45 
46  //! Wake up all pending waits.
47  void broadcast() const;
48 
49 private:
50  mutable pthread_cond_t cond_;
51  mutable Atomic<int> guard_;
52 
53  pthread_mutex_t& mutex_;
54 };
55 
56 } // namespace core
57 } // namespace roc
58 
59 #endif // ROC_CORE_COND_H_
Atomic.
Compiler attributes.
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
Definition: attributes.h:31
Condition variable.
Definition: cond.h:28
void signal() const
Wake up one pending waits.
Cond(const Mutex &mutex)
Initialize.
~Cond()
Destroy.
void wait() const
Wait.
void broadcast() const
Wake up all pending waits.
ROC_ATTR_NODISCARD bool timed_wait(nanoseconds_t timeout) const
Wait with timeout.
Mutex.
Definition: mutex.h:31
Base class for non-copyable objects.
Definition: noncopyable.h:23
Mutex.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
Root namespace.
Non-copyable object.
Time definitions.