xref: /llvm-project/libcxx/test/std/thread/thread.semaphore/timed.pass.cpp (revision fa1c077b41ae1335332d65399802f2c68e82ca7b)
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, which shipped in macOS 11.0.
13 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11|12|13|14|15}}
14 
15 // <semaphore>
16 
17 #include <semaphore>
18 #include <thread>
19 #include <chrono>
20 #include <cassert>
21 
22 #include "make_test_thread.h"
23 #include "test_macros.h"
24 
25 int main(int, char**)
26 {
27   auto const start = std::chrono::steady_clock::now();
28 
29   std::counting_semaphore<> s(0);
30 
31   assert(!s.try_acquire_until(start + std::chrono::milliseconds(250)));
32   assert(!s.try_acquire_for(std::chrono::milliseconds(250)));
33 
34   std::thread t = support::make_test_thread([&](){
35     std::this_thread::sleep_for(std::chrono::milliseconds(250));
36     s.release();
37     std::this_thread::sleep_for(std::chrono::milliseconds(250));
38     s.release();
39   });
40 
41   assert(s.try_acquire_until(start + std::chrono::seconds(2)));
42   assert(s.try_acquire_for(std::chrono::seconds(2)));
43   t.join();
44 
45   auto const end = std::chrono::steady_clock::now();
46   assert(end - start < std::chrono::seconds(10));
47 
48   return 0;
49 }
50