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 // Copyright (C) 2011 Vicente J. Botet Escriba 11 // 12 // Distributed under the Boost Software License, Version 1.0. (See accompanying 13 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 14 15 // <boost/thread/locks.hpp> 16 17 // template <class Mutex> class unique_lock; 18 19 // template <class Rep, class Period> 20 // bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 21 22 #include <boost/thread/lock_types.hpp> 23 //#include <boost/thread/mutex.hpp> 24 #include <boost/detail/lightweight_test.hpp> 25 26 #if defined BOOST_THREAD_USES_CHRONO 27 28 29 bool try_lock_for_called = false; 30 31 typedef boost::chrono::milliseconds ms; 32 33 struct mutex 34 { 35 template <class Rep, class Period> try_lock_formutex36 bool try_lock_for(const boost::chrono::duration<Rep, Period>& rel_time) 37 { 38 BOOST_TEST(rel_time == ms(5)); 39 try_lock_for_called = !try_lock_for_called; 40 return try_lock_for_called; 41 } unlockmutex42 void unlock() 43 { 44 } 45 }; 46 47 mutex m; 48 main()49int main() 50 { 51 boost::unique_lock<mutex> lk(m, boost::defer_lock); 52 BOOST_TEST(lk.try_lock_for(ms(5)) == true); 53 BOOST_TEST(try_lock_for_called == true); 54 BOOST_TEST(lk.owns_lock() == true); 55 try 56 { 57 lk.try_lock_for(ms(5)); 58 BOOST_TEST(false); 59 } 60 catch (boost::system::system_error& e) 61 { 62 BOOST_TEST(e.code().value() == boost::system::errc::resource_deadlock_would_occur); 63 } 64 lk.unlock(); 65 BOOST_TEST(lk.try_lock_for(ms(5)) == false); 66 BOOST_TEST(try_lock_for_called == false); 67 BOOST_TEST(lk.owns_lock() == false); 68 lk.release(); 69 try 70 { 71 lk.try_lock_for(ms(5)); 72 BOOST_TEST(false); 73 } 74 catch (boost::system::system_error& e) 75 { 76 BOOST_TEST(e.code().value() == boost::system::errc::operation_not_permitted); 77 } 78 79 return boost::report_errors(); 80 } 81 82 #else 83 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" 84 #endif 85 86