|
5 | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
6 | 6 | //
|
7 | 7 | //===----------------------------------------------------------------------===//
|
8 |
| -// |
9 |
| -// UNSUPPORTED: no-threads |
10 |
| -// ALLOW_RETRIES: 2 |
| 8 | + |
| 9 | +// UNSUPPORTED: no-threads, c++03 |
11 | 10 |
|
12 | 11 | // <condition_variable>
|
13 | 12 |
|
|
20 | 19 | // Predicate pred);
|
21 | 20 |
|
22 | 21 | #include <condition_variable>
|
| 22 | +#include <atomic> |
| 23 | +#include <cassert> |
| 24 | +#include <chrono> |
23 | 25 | #include <mutex>
|
24 | 26 | #include <thread>
|
25 |
| -#include <chrono> |
26 |
| -#include <cassert> |
27 | 27 |
|
28 | 28 | #include "make_test_thread.h"
|
29 | 29 | #include "test_macros.h"
|
30 | 30 |
|
31 |
| -class Pred |
32 |
| -{ |
33 |
| - int& i_; |
34 |
| -public: |
35 |
| - explicit Pred(int& i) : i_(i) {} |
36 |
| - |
37 |
| - bool operator()() {return i_ != 0;} |
38 |
| -}; |
39 |
| - |
40 |
| -std::condition_variable cv; |
41 |
| -std::mutex mut; |
42 |
| - |
43 |
| -int test1 = 0; |
44 |
| -int test2 = 0; |
45 |
| - |
46 |
| -int runs = 0; |
47 |
| - |
48 |
| -void f() |
49 |
| -{ |
50 |
| - typedef std::chrono::system_clock Clock; |
51 |
| - typedef std::chrono::milliseconds milliseconds; |
52 |
| - std::unique_lock<std::mutex> lk(mut); |
53 |
| - assert(test2 == 0); |
54 |
| - test1 = 1; |
55 |
| - cv.notify_one(); |
56 |
| - Clock::time_point t0 = Clock::now(); |
57 |
| - bool r = cv.wait_for(lk, milliseconds(250), Pred(test2)); |
58 |
| - ((void)r); // Prevent unused warning |
59 |
| - Clock::time_point t1 = Clock::now(); |
60 |
| - if (runs == 0) |
61 |
| - { |
62 |
| - assert(t1 - t0 < milliseconds(250)); |
63 |
| - assert(test2 != 0); |
64 |
| - } |
65 |
| - else |
66 |
| - { |
67 |
| - assert(t1 - t0 - milliseconds(250) < milliseconds(50)); |
68 |
| - assert(test2 == 0); |
69 |
| - } |
70 |
| - ++runs; |
| 31 | +template <class Function> |
| 32 | +std::chrono::microseconds measure(Function f) { |
| 33 | + std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now(); |
| 34 | + f(); |
| 35 | + std::chrono::high_resolution_clock::time_point end = std::chrono::high_resolution_clock::now(); |
| 36 | + return std::chrono::duration_cast<std::chrono::microseconds>(end - start); |
71 | 37 | }
|
72 | 38 |
|
73 |
| -int main(int, char**) |
74 |
| -{ |
75 |
| - { |
76 |
| - std::unique_lock<std::mutex>lk(mut); |
77 |
| - std::thread t = support::make_test_thread(f); |
78 |
| - assert(test1 == 0); |
79 |
| - while (test1 == 0) |
80 |
| - cv.wait(lk); |
81 |
| - assert(test1 != 0); |
82 |
| - test2 = 1; |
83 |
| - lk.unlock(); |
84 |
| - cv.notify_one(); |
85 |
| - t.join(); |
86 |
| - } |
87 |
| - test1 = 0; |
88 |
| - test2 = 0; |
89 |
| - { |
90 |
| - std::unique_lock<std::mutex>lk(mut); |
91 |
| - std::thread t = support::make_test_thread(f); |
92 |
| - assert(test1 == 0); |
93 |
| - while (test1 == 0) |
94 |
| - cv.wait(lk); |
95 |
| - assert(test1 != 0); |
96 |
| - lk.unlock(); |
97 |
| - t.join(); |
98 |
| - } |
| 39 | +int main(int, char**) { |
| 40 | + // Test unblocking via a call to notify_one() in another thread. |
| 41 | + // |
| 42 | + // To test this, we set a very long timeout in wait_for() and we try to minimize |
| 43 | + // the likelihood that we got awoken by a spurious wakeup by updating the |
| 44 | + // likely_spurious flag only immediately before we perform the notification. |
| 45 | + { |
| 46 | + std::atomic<bool> ready = false; |
| 47 | + std::atomic<bool> likely_spurious = true; |
| 48 | + auto timeout = std::chrono::seconds(3600); |
| 49 | + std::condition_variable cv; |
| 50 | + std::mutex mutex; |
| 51 | + |
| 52 | + std::thread t1 = support::make_test_thread([&] { |
| 53 | + std::unique_lock<std::mutex> lock(mutex); |
| 54 | + auto elapsed = measure([&] { |
| 55 | + ready = true; |
| 56 | + bool result = cv.wait_for(lock, timeout, [&] { return !likely_spurious; }); |
| 57 | + assert(result); // return value should be true since we didn't time out |
| 58 | + }); |
| 59 | + assert(elapsed < timeout); |
| 60 | + }); |
| 61 | + |
| 62 | + std::thread t2 = support::make_test_thread([&] { |
| 63 | + while (!ready) { |
| 64 | + // spin |
| 65 | + } |
| 66 | + |
| 67 | + // Acquire the same mutex as t1. This ensures that the condition variable has started |
| 68 | + // waiting (and hence released that mutex). We don't actually need to hold the lock, we |
| 69 | + // simply use it as a signal that the condition variable has started waiting. |
| 70 | + std::unique_lock<std::mutex> lock(mutex); |
| 71 | + lock.unlock(); |
| 72 | + |
| 73 | + likely_spurious = false; |
| 74 | + cv.notify_one(); |
| 75 | + }); |
| 76 | + |
| 77 | + t2.join(); |
| 78 | + t1.join(); |
| 79 | + } |
| 80 | + |
| 81 | + // Test unblocking via a timeout. |
| 82 | + // |
| 83 | + // To test this, we create a thread that waits on a condition variable with a certain |
| 84 | + // timeout, and we never awaken it. The "stop waiting" predicate always returns false, |
| 85 | + // which means that we can't get out of the wait via a spurious wakeup. |
| 86 | + { |
| 87 | + auto timeout = std::chrono::milliseconds(250); |
| 88 | + std::condition_variable cv; |
| 89 | + std::mutex mutex; |
| 90 | + |
| 91 | + std::thread t1 = support::make_test_thread([&] { |
| 92 | + std::unique_lock<std::mutex> lock(mutex); |
| 93 | + auto elapsed = measure([&] { |
| 94 | + bool result = cv.wait_for(lock, timeout, [] { return false; }); // never stop waiting (until timeout) |
| 95 | + assert(!result); // return value should be false since the predicate returns false after the timeout |
| 96 | + }); |
| 97 | + assert(elapsed >= timeout); |
| 98 | + }); |
| 99 | + |
| 100 | + t1.join(); |
| 101 | + } |
| 102 | + |
| 103 | + // Test unblocking via a spurious wakeup. |
| 104 | + // |
| 105 | + // To test this, we set a fairly long timeout in wait_for() and we basically never |
| 106 | + // wake up the condition variable. This way, we are hoping to get out of the wait |
| 107 | + // via a spurious wakeup. |
| 108 | + // |
| 109 | + // However, since spurious wakeups are not required to even happen, this test is |
| 110 | + // only trying to trigger that code path, but not actually asserting that it is |
| 111 | + // taken. In particular, we do need to eventually ensure we get out of the wait |
| 112 | + // by standard means, so we actually wake up the thread at the end. |
| 113 | + { |
| 114 | + std::atomic<bool> ready = false; |
| 115 | + std::atomic<bool> awoken = false; |
| 116 | + auto timeout = std::chrono::seconds(3600); |
| 117 | + std::condition_variable cv; |
| 118 | + std::mutex mutex; |
| 119 | + |
| 120 | + std::thread t1 = support::make_test_thread([&] { |
| 121 | + std::unique_lock<std::mutex> lock(mutex); |
| 122 | + auto elapsed = measure([&] { |
| 123 | + ready = true; |
| 124 | + bool result = cv.wait_for(lock, timeout, [&] { return true; }); |
| 125 | + awoken = true; |
| 126 | + assert(result); // return value should be true since we didn't time out |
| 127 | + }); |
| 128 | + assert(elapsed < timeout); // can technically fail if t2 never executes and we timeout, but very unlikely |
| 129 | + }); |
| 130 | + |
| 131 | + std::thread t2 = support::make_test_thread([&] { |
| 132 | + while (!ready) { |
| 133 | + // spin |
| 134 | + } |
| 135 | + |
| 136 | + // Acquire the same mutex as t1. This ensures that the condition variable has started |
| 137 | + // waiting (and hence released that mutex). We don't actually need to hold the lock, we |
| 138 | + // simply use it as a signal that the condition variable has started waiting. |
| 139 | + std::unique_lock<std::mutex> lock(mutex); |
| 140 | + lock.unlock(); |
| 141 | + |
| 142 | + // Give some time for t1 to be awoken spuriously so that code path is used. |
| 143 | + std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 144 | + |
| 145 | + // We would want to assert that the thread has been awoken after this time, |
| 146 | + // however nothing guarantees us that it ever gets spuriously awoken, so |
| 147 | + // we can't really check anything. This is still left here as documentation. |
| 148 | + assert(awoken || !awoken); |
| 149 | + |
| 150 | + // Whatever happened, actually awaken the condition variable to ensure the test |
| 151 | + // doesn't keep running until the timeout. |
| 152 | + cv.notify_one(); |
| 153 | + }); |
| 154 | + |
| 155 | + t2.join(); |
| 156 | + t1.join(); |
| 157 | + } |
99 | 158 |
|
100 | 159 | return 0;
|
101 | 160 | }
|
0 commit comments