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