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: no-threads |
10 | |
11 | // <condition_variable> |
12 | |
13 | // class condition_variable_any; |
14 | |
15 | // void notify_all(); |
16 | |
17 | #include <condition_variable> |
18 | #include <mutex> |
19 | #include <thread> |
20 | #include <vector> |
21 | #include <atomic> |
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 | const unsigned threadCount = 2; |
35 | bool pleaseExit = false; |
36 | std::atomic<unsigned> notReady; |
37 | |
38 | void helper() { |
39 | L1 lk(m0); |
40 | --notReady; |
41 | while (pleaseExit == false) |
42 | cv.wait(lock&: lk); |
43 | } |
44 | |
45 | int main(int, char**) |
46 | { |
47 | notReady = threadCount; |
48 | std::vector<std::thread> threads(threadCount); |
49 | for (unsigned i = 0; i < threadCount; i++) |
50 | threads[i] = support::make_test_thread(helper); |
51 | { |
52 | while (notReady > 0) |
53 | std::this_thread::yield(); |
54 | // At this point, both threads have had a chance to acquire the lock and are |
55 | // either waiting on the condition variable or about to wait. |
56 | L1 lk(m0); |
57 | pleaseExit = true; |
58 | // POSIX does not guarantee reliable scheduling if notify_all is called |
59 | // without the lock being held. |
60 | cv.notify_all(); |
61 | } |
62 | // The test will hang if not all of the threads were woken. |
63 | for (unsigned i = 0; i < threadCount; i++) |
64 | threads[i].join(); |
65 | |
66 | return 0; |
67 | } |
68 | |