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 #include <fbl/auto_lock.h>
6 #include <fbl/condition_variable.h>
7 #include <fbl/mutex.h>
8 #include <unittest/unittest.h>
9 
10 namespace fbl {
11 namespace {
12 
EmptySignalTest()13 bool EmptySignalTest() {
14     BEGIN_TEST;
15 
16     ConditionVariable cvar;
17     cvar.Signal();
18     cvar.Broadcast();
19 
20     END_TEST;
21 }
22 
WaitTest()23 bool WaitTest() {
24     BEGIN_TEST;
25 
26     struct State {
27         Mutex mutex;
28         ConditionVariable cvar;
29     } state;
30 
31     thrd_t thread;
32     AutoLock lock(&state.mutex);
33 
34     thrd_create(&thread, [](void* arg) {
35         auto state = reinterpret_cast<State*>(arg);
36         AutoLock lock(&state->mutex);
37         state->cvar.Signal();
38         return 0;
39     }, &state);
40 
41     state.cvar.Wait(&state.mutex);
42     thrd_join(thread, NULL);
43 
44     END_TEST;
45 }
46 
47 }  // namespace
48 }  // namespace fbl
49 
50 BEGIN_TEST_CASE(ConditionVariableTests)
51 RUN_TEST(fbl::EmptySignalTest)
52 RUN_TEST(fbl::WaitTest)
53 END_TEST_CASE(ConditionVariableTests);
54