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