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 // XFAIL: libcpp-no-exceptions 11 // UNSUPPORTED: libcpp-has-no-threads 12 13 // <mutex> 14 15 // template <class Mutex> class unique_lock; 16 17 // bool try_lock(); 18 19 #include <mutex> 20 #include <cassert> 21 22 bool try_lock_called = false; 23 24 struct mutex 25 { 26 bool try_lock() 27 { 28 try_lock_called = !try_lock_called; 29 return try_lock_called; 30 } 31 void unlock() {} 32 }; 33 34 mutex m; 35 36 int main() 37 { 38 std::unique_lock<mutex> lk(m, std::defer_lock); 39 assert(lk.try_lock() == true); 40 assert(try_lock_called == true); 41 assert(lk.owns_lock() == true); 42 try 43 { 44 lk.try_lock(); 45 assert(false); 46 } 47 catch (std::system_error& e) 48 { 49 assert(e.code().value() == EDEADLK); 50 } 51 lk.unlock(); 52 assert(lk.try_lock() == false); 53 assert(try_lock_called == false); 54 assert(lk.owns_lock() == false); 55 lk.release(); 56 try 57 { 58 lk.try_lock(); 59 assert(false); 60 } 61 catch (std::system_error& e) 62 { 63 assert(e.code().value() == EPERM); 64 } 65 } 66