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 |
11 | |
12 | // Until we drop support for the synchronization library in C++11/14/17 |
13 | // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS |
14 | |
15 | // XFAIL: availability-synchronization_library-missing |
16 | |
17 | // <semaphore> |
18 | |
19 | #include <semaphore> |
20 | #include <thread> |
21 | #include <chrono> |
22 | #include <cassert> |
23 | |
24 | #include "make_test_thread.h" |
25 | #include "test_macros.h" |
26 | |
27 | int main(int, char**) |
28 | { |
29 | auto const start = std::chrono::steady_clock::now(); |
30 | |
31 | std::counting_semaphore<> s(0); |
32 | |
33 | assert(!s.try_acquire_until(start + std::chrono::milliseconds(250))); |
34 | assert(!s.try_acquire_for(std::chrono::milliseconds(250))); |
35 | |
36 | std::thread t = support::make_test_thread([&](){ |
37 | std::this_thread::sleep_for(std::chrono::milliseconds(250)); |
38 | s.release(); |
39 | std::this_thread::sleep_for(std::chrono::milliseconds(250)); |
40 | s.release(); |
41 | }); |
42 | |
43 | assert(s.try_acquire_until(start + std::chrono::seconds(2))); |
44 | assert(s.try_acquire_for(std::chrono::seconds(2))); |
45 | t.join(); |
46 | |
47 | auto const end = std::chrono::steady_clock::now(); |
48 | assert(end - start < std::chrono::seconds(10)); |
49 | |
50 | return 0; |
51 | } |
52 | |