blob: d0bbc91351ccdc4c6a5e6c4ad7282f7a806b9618 [file] [log] [blame]
Olivier Giroux54fa9ec2020-02-18 09:58:34 -05001//===----------------------------------------------------------------------===//
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++98, c++03, c++11
11
12// <semaphore>
13
14#include <semaphore>
15#include <thread>
16#include <chrono>
17
18#include "test_macros.h"
19
20int main(int, char**)
21{
22 auto const start = std::chrono::steady_clock::now();
23
24 std::counting_semaphore<> s(0);
25
26 assert(!s.try_acquire_until(start + std::chrono::milliseconds(250)));
27 assert(!s.try_acquire_for(std::chrono::milliseconds(250)));
28
29 std::thread t([&](){
30 std::this_thread::sleep_for(std::chrono::milliseconds(250));
31 s.release();
32 std::this_thread::sleep_for(std::chrono::milliseconds(250));
33 s.release();
34 });
35
36 assert(s.try_acquire_until(start + std::chrono::seconds(2)));
37 assert(s.try_acquire_for(std::chrono::seconds(2)));
38 t.join();
39
40 auto const end = std::chrono::steady_clock::now();
41 assert(end - start < std::chrono::seconds(10));
42
43 return 0;
44}