1 #pragma once 2 3 #include "../relacy/relacy_std.hpp" 4 5 6 7 struct test_condvar : rl::test_suite<test_condvar, 2> 8 { 9 std::mutex mtx; 10 std::condition_variable cv; 11 rl::var<int> data; 12 beforetest_condvar13 void before() 14 { 15 data($) = 0; 16 } 17 threadtest_condvar18 void thread(unsigned index) 19 { 20 if (0 == index) 21 { 22 mtx.lock($); 23 data($) += 1; 24 mtx.unlock($); 25 cv.notify_one($); 26 } 27 else 28 { 29 mtx.lock($); 30 while (0 == data($)) 31 { 32 cv.wait(mtx, $); 33 } 34 mtx.unlock($); 35 } 36 } 37 }; 38 39 40 41 42 struct test_condvar2 : rl::test_suite<test_condvar2, 3> 43 { 44 rl::var<int> stage; 45 std::mutex mtx; 46 std::condition_variable cv; 47 beforetest_condvar248 void before() 49 { 50 stage($) = 0; 51 } 52 threadtest_condvar253 void thread(unsigned index) 54 { 55 if (0 == index) 56 { 57 mtx.lock($); 58 stage($) += 1; 59 cv.notify_all($); 60 while (stage($) != 2) 61 cv.wait(mtx, $); 62 mtx.unlock($); 63 } 64 else if (1 == index) 65 { 66 mtx.lock($); 67 while (stage($) != 1) 68 cv.wait(mtx, $); 69 stage($) += 1; 70 cv.notify_all($); 71 mtx.unlock($); 72 } 73 else if (2 == index) 74 { 75 mtx.lock($); 76 while (stage($) != 2) 77 cv.wait(mtx, $); 78 mtx.unlock($); 79 } 80 } 81 }; 82 83