1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: libcpp-has-no-threads
10 
11 // ALLOW_RETRIES: 2
12 
13 // <condition_variable>
14 
15 // class condition_variable_any;
16 
17 // void notify_one();
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <thread>
22 #include <cassert>
23 
24 #include "make_test_thread.h"
25 #include "test_macros.h"
26 
27 std::condition_variable_any cv;
28 
29 typedef std::timed_mutex L0;
30 typedef std::unique_lock<L0> L1;
31 
32 L0 m0;
33 
34 int test0 = 0;
35 int test1 = 0;
36 int test2 = 0;
37 
38 void f1()
39 {
40     L1 lk(m0);
41     assert(test1 == 0);
42     while (test1 == 0)
43         cv.wait(lk);
44     assert(test1 == 1);
45     test1 = 2;
46 }
47 
48 void f2()
49 {
50     L1 lk(m0);
51     assert(test2 == 0);
52     while (test2 == 0)
53         cv.wait(lk);
54     assert(test2 == 1);
55     test2 = 2;
56 }
57 
58 int main(int, char**)
59 {
60     std::thread t1 = support::make_test_thread(f1);
61     std::thread t2 = support::make_test_thread(f2);
62     std::this_thread::sleep_for(std::chrono::milliseconds(100));
63     {
64         L1 lk(m0);
65         test1 = 1;
66         test2 = 1;
67     }
68     cv.notify_one();
69     {
70         std::this_thread::sleep_for(std::chrono::milliseconds(100));
71         L1 lk(m0);
72     }
73     if (test1 == 2)
74     {
75         t1.join();
76         test1 = 0;
77     }
78     else if (test2 == 2)
79     {
80         t2.join();
81         test2 = 0;
82     }
83     else
84         assert(false);
85     cv.notify_one();
86     {
87         std::this_thread::sleep_for(std::chrono::milliseconds(100));
88         L1 lk(m0);
89     }
90     if (test1 == 2)
91     {
92         t1.join();
93         test1 = 0;
94     }
95     else if (test2 == 2)
96     {
97         t2.join();
98         test2 = 0;
99     }
100     else
101         assert(false);
102 
103   return 0;
104 }
105