1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #ifdef __cplusplus
8 
9 // ConditionVariable is a C++ helper class intended to wrap a condition variable synchronization
10 // primitive. It is also responsible for automatically initializing and destroying the internal
11 // object.
12 //
13 // This object is currently only supported in userspace.
14 #ifndef _KERNEL
15 
16 #include <threads.h>
17 
18 #include <fbl/macros.h>
19 #include <fbl/mutex.h>
20 #include <zircon/compiler.h>
21 #include <zircon/types.h>
22 
23 namespace fbl {
24 
25 class ConditionVariable {
26 public:
ConditionVariable()27     ConditionVariable() { cnd_init(&cond_); }
~ConditionVariable()28     ~ConditionVariable() { cnd_destroy(&cond_); }
29     DISALLOW_COPY_ASSIGN_AND_MOVE(ConditionVariable);
30 
Wait(Mutex * mutex)31     void Wait(Mutex* mutex) __TA_REQUIRES(mutex) {
32         cnd_wait(&cond_, mutex->GetInternal());
33     }
34 
Signal()35     void Signal() {
36         cnd_signal(&cond_);
37     }
38 
Broadcast()39     void Broadcast() {
40         cnd_broadcast(&cond_);
41     }
42 
43 private:
44     cnd_t cond_;
45 };
46 
47 }
48 
49 #endif  // ifndef _KERNEL
50 #endif  // ifdef __cplusplus
51