xref: /llvm-project/libcxx/test/std/thread/thread.semaphore/lost_wakeup.pass.cpp (revision 825658856d94776889399a07a3939610ee1aa299)
1*82565885SHui //===----------------------------------------------------------------------===//
2*82565885SHui //
3*82565885SHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*82565885SHui // See https://llvm.org/LICENSE.txt for license information.
5*82565885SHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*82565885SHui //
7*82565885SHui //===----------------------------------------------------------------------===//
8*82565885SHui 
9*82565885SHui // UNSUPPORTED: no-threads
10*82565885SHui // UNSUPPORTED: c++03, c++11, c++14, c++17
11*82565885SHui 
12*82565885SHui // XFAIL: availability-synchronization_library-missing
13*82565885SHui 
14*82565885SHui // This is a regression test for https://llvm.org/PR47013.
15*82565885SHui 
16*82565885SHui // <semaphore>
17*82565885SHui 
18*82565885SHui #include <barrier>
19*82565885SHui #include <semaphore>
20*82565885SHui #include <thread>
21*82565885SHui #include <vector>
22*82565885SHui 
23*82565885SHui #include "make_test_thread.h"
24*82565885SHui 
25*82565885SHui static std::counting_semaphore<> s(0);
26*82565885SHui static std::barrier<> b(8 + 1);
27*82565885SHui 
acquire()28*82565885SHui void acquire() {
29*82565885SHui   for (int i = 0; i < 10'000; ++i) {
30*82565885SHui     s.acquire();
31*82565885SHui     b.arrive_and_wait();
32*82565885SHui   }
33*82565885SHui }
34*82565885SHui 
release()35*82565885SHui void release() {
36*82565885SHui   for (int i = 0; i < 10'000; ++i) {
37*82565885SHui     s.release(1);
38*82565885SHui     s.release(1);
39*82565885SHui     s.release(1);
40*82565885SHui     s.release(1);
41*82565885SHui 
42*82565885SHui     s.release(1);
43*82565885SHui     s.release(1);
44*82565885SHui     s.release(1);
45*82565885SHui     s.release(1);
46*82565885SHui 
47*82565885SHui     b.arrive_and_wait();
48*82565885SHui   }
49*82565885SHui }
50*82565885SHui 
main(int,char **)51*82565885SHui int main(int, char**) {
52*82565885SHui   for (int run = 0; run < 20; ++run) {
53*82565885SHui     std::vector<std::thread> threads;
54*82565885SHui     for (int i = 0; i < 8; ++i)
55*82565885SHui       threads.push_back(support::make_test_thread(acquire));
56*82565885SHui 
57*82565885SHui     threads.push_back(support::make_test_thread(release));
58*82565885SHui 
59*82565885SHui     for (auto& thread : threads)
60*82565885SHui       thread.join();
61*82565885SHui   }
62*82565885SHui 
63*82565885SHui   return 0;
64*82565885SHui }
65