try_lock_for_pass.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 Rep, class Period>
  16. // bool try_lock_for(const chrono::duration<Rep, Period>& rel_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_for_called = false;
  21. typedef boost::chrono::milliseconds ms;
  22. struct shared_mutex
  23. {
  24. template <class Rep, class Period>
  25. bool try_lock_shared_for(const boost::chrono::duration<Rep, Period>& rel_time)
  26. {
  27. BOOST_TEST(rel_time == ms(5));
  28. try_lock_for_called = !try_lock_for_called;
  29. return try_lock_for_called;
  30. }
  31. void unlock_shared()
  32. {
  33. }
  34. };
  35. shared_mutex m;
  36. int main()
  37. {
  38. boost::shared_lock<shared_mutex> lk(m, boost::defer_lock);
  39. BOOST_TEST(lk.try_lock_for(ms(5)) == true);
  40. BOOST_TEST(try_lock_for_called == true);
  41. BOOST_TEST(lk.owns_lock() == true);
  42. try
  43. {
  44. lk.try_lock_for(ms(5));
  45. BOOST_TEST(false);
  46. }
  47. catch (boost::system::system_error& e)
  48. {
  49. BOOST_TEST(e.code().value() == boost::system::errc::resource_deadlock_would_occur);
  50. }
  51. lk.unlock();
  52. BOOST_TEST(lk.try_lock_for(ms(5)) == false);
  53. BOOST_TEST(try_lock_for_called == false);
  54. BOOST_TEST(lk.owns_lock() == false);
  55. lk.release();
  56. try
  57. {
  58. lk.try_lock_for(ms(5));
  59. BOOST_TEST(false);
  60. }
  61. catch (boost::system::system_error& e)
  62. {
  63. BOOST_TEST(e.code().value() == boost::system::errc::operation_not_permitted);
  64. }
  65. return boost::report_errors();
  66. }