xref: /llvm-project/libcxx/test/std/thread/thread.semaphore/lost_wakeup.pass.cpp (revision 825658856d94776889399a07a3939610ee1aa299)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
11 
12 // XFAIL: availability-synchronization_library-missing
13 
14 // This is a regression test for https://llvm.org/PR47013.
15 
16 // <semaphore>
17 
18 #include <barrier>
19 #include <semaphore>
20 #include <thread>
21 #include <vector>
22 
23 #include "make_test_thread.h"
24 
25 static std::counting_semaphore<> s(0);
26 static std::barrier<> b(8 + 1);
27 
acquire()28 void acquire() {
29   for (int i = 0; i < 10'000; ++i) {
30     s.acquire();
31     b.arrive_and_wait();
32   }
33 }
34 
release()35 void release() {
36   for (int i = 0; i < 10'000; ++i) {
37     s.release(1);
38     s.release(1);
39     s.release(1);
40     s.release(1);
41 
42     s.release(1);
43     s.release(1);
44     s.release(1);
45     s.release(1);
46 
47     b.arrive_and_wait();
48   }
49 }
50 
main(int,char **)51 int main(int, char**) {
52   for (int run = 0; run < 20; ++run) {
53     std::vector<std::thread> threads;
54     for (int i = 0; i < 8; ++i)
55       threads.push_back(support::make_test_thread(acquire));
56 
57     threads.push_back(support::make_test_thread(release));
58 
59     for (auto& thread : threads)
60       thread.join();
61   }
62 
63   return 0;
64 }
65