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 Rep, class Period> 17 // bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 18 19 #include <mutex> 20 #include <cassert> 21 22 bool try_lock_for_called = false; 23 24 typedef std::chrono::milliseconds ms; 25 26 struct mutex 27 { 28 template <class Rep, class Period> try_lock_formutex29 bool try_lock_for(const std::chrono::duration<Rep, Period>& rel_time) 30 { 31 assert(rel_time == ms(5)); 32 try_lock_for_called = !try_lock_for_called; 33 return try_lock_for_called; 34 } unlockmutex35 void unlock() {} 36 }; 37 38 mutex m; 39 main()40int main() 41 { 42 std::unique_lock<mutex> lk(m, std::defer_lock); 43 assert(lk.try_lock_for(ms(5)) == true); 44 assert(try_lock_for_called == true); 45 assert(lk.owns_lock() == true); 46 try 47 { 48 lk.try_lock_for(ms(5)); 49 assert(false); 50 } 51 catch (std::system_error& e) 52 { 53 assert(e.code().value() == EDEADLK); 54 } 55 lk.unlock(); 56 assert(lk.try_lock_for(ms(5)) == false); 57 assert(try_lock_for_called == false); 58 assert(lk.owns_lock() == false); 59 lk.release(); 60 try 61 { 62 lk.try_lock_for(ms(5)); 63 assert(false); 64 } 65 catch (std::system_error& e) 66 { 67 assert(e.code().value() == EPERM); 68 } 69 } 70