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