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 // ALLOW_RETRIES: 2
11 
12 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode.
13 // UNSUPPORTED: linux && 32bits-on-64bits
14 
15 // <condition_variable>
16 
17 // class condition_variable_any;
18 
19 // void notify_one();
20 
21 #include <condition_variable>
22 #include <mutex>
23 #include <thread>
24 #include <cassert>
25 
26 #include "make_test_thread.h"
27 #include "test_macros.h"
28 
29 std::condition_variable_any cv;
30 
31 typedef std::timed_mutex L0;
32 typedef std::unique_lock<L0> L1;
33 
34 L0 m0;
35 
36 int test0 = 0;
37 int test1 = 0;
38 int test2 = 0;
39 
40 void f1()
41 {
42     L1 lk(m0);
43     assert(test1 == 0);
44     while (test1 == 0)
45         cv.wait(lk);
46     assert(test1 == 1);
47     test1 = 2;
48 }
49 
50 void f2()
51 {
52     L1 lk(m0);
53     assert(test2 == 0);
54     while (test2 == 0)
55         cv.wait(lk);
56     assert(test2 == 1);
57     test2 = 2;
58 }
59 
60 int main(int, char**)
61 {
62     std::thread t1 = support::make_test_thread(f1);
63     std::thread t2 = support::make_test_thread(f2);
64     std::this_thread::sleep_for(std::chrono::milliseconds(100));
65     {
66         L1 lk(m0);
67         test1 = 1;
68         test2 = 1;
69     }
70     cv.notify_one();
71     {
72         std::this_thread::sleep_for(std::chrono::milliseconds(100));
73         L1 lk(m0);
74     }
75     if (test1 == 2)
76     {
77         t1.join();
78         test1 = 0;
79     }
80     else if (test2 == 2)
81     {
82         t2.join();
83         test2 = 0;
84     }
85     else
86         assert(false);
87     cv.notify_one();
88     {
89         std::this_thread::sleep_for(std::chrono::milliseconds(100));
90         L1 lk(m0);
91     }
92     if (test1 == 2)
93     {
94         t1.join();
95         test1 = 0;
96     }
97     else if (test2 == 2)
98     {
99         t2.join();
100         test2 = 0;
101     }
102     else
103         assert(false);
104 
105   return 0;
106 }
107