xref: /llvm-project/libcxx/test/std/thread/thread.semaphore/timed.pass.cpp (revision 31cbe0f240f660f15602c96b787c58a26f17e179)
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: libcpp-has-no-threads
10 // UNSUPPORTED: c++03, c++11
11 
12 // This test requires the dylib support introduced in D68480
13 // XFAIL: with_system_cxx_lib=macosx10.15
14 // XFAIL: with_system_cxx_lib=macosx10.14
15 // XFAIL: with_system_cxx_lib=macosx10.13
16 // XFAIL: with_system_cxx_lib=macosx10.12
17 // XFAIL: with_system_cxx_lib=macosx10.11
18 // XFAIL: with_system_cxx_lib=macosx10.10
19 // XFAIL: with_system_cxx_lib=macosx10.9
20 
21 // <semaphore>
22 
23 #include <semaphore>
24 #include <thread>
25 #include <chrono>
26 
27 #include "test_macros.h"
28 
29 int main(int, char**)
30 {
31   auto const start = std::chrono::steady_clock::now();
32 
33   std::counting_semaphore<> s(0);
34 
35   assert(!s.try_acquire_until(start + std::chrono::milliseconds(250)));
36   assert(!s.try_acquire_for(std::chrono::milliseconds(250)));
37 
38   std::thread t([&](){
39     std::this_thread::sleep_for(std::chrono::milliseconds(250));
40     s.release();
41     std::this_thread::sleep_for(std::chrono::milliseconds(250));
42     s.release();
43   });
44 
45   assert(s.try_acquire_until(start + std::chrono::seconds(2)));
46   assert(s.try_acquire_for(std::chrono::seconds(2)));
47   t.join();
48 
49   auto const end = std::chrono::steady_clock::now();
50   assert(end - start < std::chrono::seconds(10));
51 
52   return 0;
53 }
54