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 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_libuv/roc_core/cond.h
10 //! @brief Condition variable.
11 
12 #ifndef ROC_CORE_COND_H_
13 #define ROC_CORE_COND_H_
14 
15 #include <uv.h>
16 
17 #include "roc_core/mutex.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/panic.h"
20 
21 namespace roc {
22 namespace core {
23 
24 //! Condition variable.
25 class Cond : public NonCopyable<> {
26 public:
27  //! Initialize.
28  Cond(const Mutex& mutex)
29  : mutex_(mutex.mutex_) {
30  if (int err = uv_cond_init(&cond_)) {
31  roc_panic("cond: uv_cond_init(): [%s] %s", uv_err_name(err),
32  uv_strerror(err));
33  }
34  }
35 
36  ~Cond() {
37  uv_cond_destroy(&cond_);
38  }
39 
40  //! Wait.
41  void wait() const {
42  uv_cond_wait(&cond_, &mutex_);
43  }
44 
45  //! Wake up all pending waits.
46  void broadcast() const {
47  uv_cond_broadcast(&cond_);
48  }
49 
50 private:
51  mutable uv_cond_t cond_;
52  uv_mutex_t& mutex_;
53 };
54 
55 } // namespace core
56 } // namespace roc
57 
58 #endif // ROC_CORE_COND_H_
Cond(const Mutex &mutex)
Initialize.
Definition: cond.h:28
Condition variable.
Definition: cond.h:25
Root namespace.
Mutex.
Definition: mutex.h:27
void wait() const
Wait.
Definition: cond.h:41
#define roc_panic(...)
Print error message and terminate program gracefully.
Definition: panic.h:42
void broadcast() const
Wake up all pending waits.
Definition: cond.h:46
Base class for non-copyable objects.
Definition: noncopyable.h:23
Mutex.
Panic function.
Non-copyable object.