try_lock_until_pass.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. // Copyright (C) 2011 Vicente J. Botet Escriba
  10. //
  11. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  13. // <boost/thread/locks.hpp>
  14. // template <class Mutex> class shared_lock;
  15. // template <class Clock, class Duration>
  16. // bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  17. #include <boost/thread/lock_types.hpp>
  18. #include <boost/thread/shared_mutex.hpp>
  19. #include <boost/detail/lightweight_test.hpp>
  20. bool try_lock_until_called = false;
  21. struct shared_mutex
  22. {
  23. template <class Clock, class Duration>
  24. bool try_lock_shared_until(const boost::chrono::time_point<Clock, Duration>& abs_time)
  25. {
  26. typedef boost::chrono::milliseconds ms;
  27. BOOST_TEST(Clock::now() - abs_time < ms(5));
  28. try_lock_until_called = !try_lock_until_called;
  29. return try_lock_until_called;
  30. }
  31. void unlock_shared()
  32. {
  33. }
  34. };
  35. shared_mutex m;
  36. int main()
  37. {
  38. typedef boost::chrono::steady_clock Clock;
  39. boost::shared_lock<shared_mutex> lk(m, boost::defer_lock);
  40. BOOST_TEST(lk.try_lock_until(Clock::now()) == true);
  41. BOOST_TEST(try_lock_until_called == true);
  42. BOOST_TEST(lk.owns_lock() == true);
  43. try
  44. {
  45. lk.try_lock_until(Clock::now());
  46. BOOST_TEST(false);
  47. }
  48. catch (boost::system::system_error& e)
  49. {
  50. BOOST_TEST(e.code().value() == boost::system::errc::resource_deadlock_would_occur);
  51. }
  52. lk.unlock();
  53. BOOST_TEST(lk.try_lock_until(Clock::now()) == false);
  54. BOOST_TEST(try_lock_until_called == false);
  55. BOOST_TEST(lk.owns_lock() == false);
  56. lk.release();
  57. try
  58. {
  59. lk.try_lock_until(Clock::now());
  60. BOOST_TEST(false);
  61. }
  62. catch (boost::system::system_error& e)
  63. {
  64. BOOST_TEST(e.code().value() == boost::system::errc::operation_not_permitted);
  65. }
  66. return boost::report_errors();
  67. }