[mlir] Update Ch-2.md (#121379)
[llvm-project.git] / libcxx / test / std / thread / thread.condition / thread.condition.condvar / notify_one.pass.cpp
blobbc6c3016eb1a8e402f2b8c5c4f53dbf784b342ef
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
11 // This test occasionally fails on Android.
12 // UNSUPPORTED: LIBCXX-ANDROID-FIXME
14 // <condition_variable>
16 // class condition_variable;
18 // void notify_one();
21 // NOTE: `notify_one` is just a wrapper around pthread_cond_signal, but
22 // POSIX does not guarantee that one and only one thread will be woken:
24 // https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_signal.html
26 // Quote:
27 // Multiple Awakenings by Condition Signal
28 // On a multi-processor, it may be impossible for an implementation of
29 // pthread_cond_signal() to avoid the unblocking of more than one thread
30 // blocked on a condition variable. For example...
34 // NOTE: In previous versions of this test, `notify_one` was called WITHOUT
35 // holding the lock but POSIX says (in the aforementioned URL) that:
36 // ...if predictable scheduling behavior is required, then that mutex shall
37 // be locked by the thread calling pthread_cond_broadcast() or
38 // pthread_cond_signal().
41 #include <condition_variable>
42 #include <atomic>
43 #include <mutex>
44 #include <thread>
45 #include <cassert>
47 #include "make_test_thread.h"
48 #include "test_macros.h"
51 std::condition_variable cv;
52 std::mutex mut;
54 std::atomic_int test1(0);
55 std::atomic_int test2(0);
56 std::atomic_int ready(2);
57 std::atomic_int which(0);
59 void f1()
61 std::unique_lock<std::mutex> lk(mut);
62 assert(test1 == 0);
63 --ready;
64 while (test1 == 0)
65 cv.wait(lk);
66 which = 1;
67 assert(test1 == 1);
68 test1 = 2;
71 void f2()
73 std::unique_lock<std::mutex> lk(mut);
74 assert(test2 == 0);
75 --ready;
76 while (test2 == 0)
77 cv.wait(lk);
78 which = 2;
79 assert(test2 == 1);
80 test2 = 2;
83 int main(int, char**)
85 std::thread t1 = support::make_test_thread(f1);
86 std::thread t2 = support::make_test_thread(f2);
88 while (ready > 0)
89 std::this_thread::yield();
90 // At this point:
91 // 1) Both f1 and f2 have entered their condition variable wait.
92 // 2) Either f1 or f2 has the mutex locked and is about to wait.
93 std::unique_lock<std::mutex> lk(mut);
94 test1 = 1;
95 test2 = 1;
96 ready = 1;
97 cv.notify_one();
100 while (which == 0)
101 std::this_thread::yield();
102 std::unique_lock<std::mutex> lk(mut);
103 if (test1 == 2) {
104 assert(test2 == 1);
105 t1.join();
106 test1 = 0;
107 } else {
108 assert(test1 == 1);
109 assert(test2 == 2);
110 t2.join();
111 test2 = 0;
113 which = 0;
114 cv.notify_one();
117 while (which == 0)
118 std::this_thread::yield();
119 std::unique_lock<std::mutex> lk(mut);
120 if (test1 == 2) {
121 assert(test2 == 0);
122 t1.join();
123 test1 = 0;
124 } else {
125 assert(test1 == 0);
126 assert(test2 == 2);
127 t2.join();
128 test2 = 0;
132 return 0;