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 // UNSUPPORTED: c++98, c++03, c++11 12 13 // <shared_mutex> 14 15 // template <class Mutex> class shared_lock; 16 17 // template <class Rep, class Period> 18 // bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 19 20 #include <shared_mutex> 21 #include <cassert> 22 23 #include "test_macros.h" 24 25 bool try_lock_for_called = false; 26 27 typedef std::chrono::milliseconds ms; 28 29 struct mutex 30 { 31 template <class Rep, class Period> 32 bool try_lock_shared_for(const std::chrono::duration<Rep, Period>& rel_time) 33 { 34 assert(rel_time == ms(5)); 35 try_lock_for_called = !try_lock_for_called; 36 return try_lock_for_called; 37 } 38 void unlock_shared() {} 39 }; 40 41 mutex m; 42 43 int main() 44 { 45 std::shared_lock<mutex> lk(m, std::defer_lock); 46 assert(lk.try_lock_for(ms(5)) == true); 47 assert(try_lock_for_called == true); 48 assert(lk.owns_lock() == true); 49 #ifndef TEST_HAS_NO_EXCEPTIONS 50 try 51 { 52 lk.try_lock_for(ms(5)); 53 assert(false); 54 } 55 catch (std::system_error& e) 56 { 57 assert(e.code().value() == EDEADLK); 58 } 59 #endif 60 lk.unlock(); 61 assert(lk.try_lock_for(ms(5)) == false); 62 assert(try_lock_for_called == false); 63 assert(lk.owns_lock() == false); 64 lk.release(); 65 #ifndef TEST_HAS_NO_EXCEPTIONS 66 try 67 { 68 lk.try_lock_for(ms(5)); 69 assert(false); 70 } 71 catch (std::system_error& e) 72 { 73 assert(e.code().value() == EPERM); 74 } 75 #endif 76 } 77