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