1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 12 // <mutex> 13 14 // template <class Mutex> class unique_lock; 15 16 // template <class Clock, class Duration> 17 // bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); 18 19 #include <mutex> 20 #include <cassert> 21 22 #include "test_macros.h" 23 24 bool try_lock_until_called = false; 25 26 struct mutex 27 { 28 template <class Clock, class Duration> 29 bool try_lock_until(const std::chrono::time_point<Clock, Duration>& abs_time) 30 { 31 typedef std::chrono::milliseconds ms; 32 assert(Clock::now() - abs_time < ms(5)); 33 try_lock_until_called = !try_lock_until_called; 34 return try_lock_until_called; 35 } 36 void unlock() {} 37 }; 38 39 mutex m; 40 41 int main() 42 { 43 typedef std::chrono::steady_clock Clock; 44 std::unique_lock<mutex> lk(m, std::defer_lock); 45 assert(lk.try_lock_until(Clock::now()) == true); 46 assert(try_lock_until_called == true); 47 assert(lk.owns_lock() == true); 48 #ifndef TEST_HAS_NO_EXCEPTIONS 49 try 50 { 51 TEST_IGNORE_NODISCARD lk.try_lock_until(Clock::now()); 52 assert(false); 53 } 54 catch (std::system_error& e) 55 { 56 assert(e.code().value() == EDEADLK); 57 } 58 #endif 59 lk.unlock(); 60 assert(lk.try_lock_until(Clock::now()) == false); 61 assert(try_lock_until_called == false); 62 assert(lk.owns_lock() == false); 63 lk.release(); 64 #ifndef TEST_HAS_NO_EXCEPTIONS 65 try 66 { 67 TEST_IGNORE_NODISCARD lk.try_lock_until(Clock::now()); 68 assert(false); 69 } 70 catch (std::system_error& e) 71 { 72 assert(e.code().value() == EPERM); 73 } 74 #endif 75 } 76