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 // <mutex> 10 11 // template <class Mutex> class unique_lock; 12 13 // template <class Clock, class Duration> 14 // bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); 15 16 #include <cassert> 17 #include <chrono> 18 #include <mutex> 19 #include <system_error> 20 21 #include "checking_mutex.h" 22 #include "test_macros.h" 23 24 int main(int, char**) { 25 typedef std::chrono::system_clock Clock; 26 checking_mutex mux; 27 28 std::unique_lock<checking_mutex> lock(mux, std::defer_lock_t()); 29 30 assert(lock.try_lock_until(Clock::now())); 31 assert(mux.current_state == checking_mutex::locked_via_try_lock_until); 32 assert(lock.owns_lock()); 33 34 #ifndef TEST_HAS_NO_EXCEPTIONS 35 try { 36 mux.last_try = checking_mutex::none; 37 (void)lock.try_lock_until(Clock::now()); 38 assert(false); 39 } catch (std::system_error& e) { 40 assert(mux.last_try == checking_mutex::none); 41 assert(e.code() == std::errc::resource_deadlock_would_occur); 42 } 43 #endif 44 45 lock.unlock(); 46 mux.reject = true; 47 assert(!lock.try_lock_until(Clock::now())); 48 assert(mux.last_try == checking_mutex::locked_via_try_lock_until); 49 assert(lock.owns_lock() == false); 50 lock.release(); 51 52 #ifndef TEST_HAS_NO_EXCEPTIONS 53 try { 54 mux.last_try = checking_mutex::none; 55 (void)lock.try_lock_until(Clock::now()); 56 assert(false); 57 } catch (std::system_error& e) { 58 assert(mux.last_try == checking_mutex::none); 59 assert(e.code() == std::errc::operation_not_permitted); 60 } 61 #endif 62 63 return 0; 64 } 65