poly_lockable_adapter.hpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Vicente J. Botet Escriba 2008-2009,2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/thread for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_THREAD_POLY_LOCKABLE_ADAPTER_HPP
  11. #define BOOST_THREAD_POLY_LOCKABLE_ADAPTER_HPP
  12. #include <boost/thread/poly_lockable.hpp>
  13. namespace boost
  14. {
  15. //[poly_basic_lockable_adapter
  16. template <typename Mutex, typename Base=poly_basic_lockable>
  17. class poly_basic_lockable_adapter : public Base
  18. {
  19. public:
  20. typedef Mutex mutex_type;
  21. protected:
  22. mutex_type& mtx() const
  23. {
  24. return mtx_;
  25. }
  26. mutable mutex_type mtx_; /*< mutable so that it can be modified by const functions >*/
  27. public:
  28. BOOST_THREAD_NO_COPYABLE( poly_basic_lockable_adapter) /*< no copyable >*/
  29. poly_basic_lockable_adapter()
  30. {}
  31. void lock()
  32. {
  33. mtx().lock();
  34. }
  35. void unlock()
  36. {
  37. mtx().unlock();
  38. }
  39. };
  40. //]
  41. //[poly_lockable_adapter
  42. template <typename Mutex, typename Base=poly_lockable>
  43. class poly_lockable_adapter : public poly_basic_lockable_adapter<Mutex, Base>
  44. {
  45. public:
  46. typedef Mutex mutex_type;
  47. bool try_lock()
  48. {
  49. return this->mtx().try_lock();
  50. }
  51. };
  52. //]
  53. //[poly_timed_lockable_adapter
  54. template <typename Mutex, typename Base=poly_timed_lockable>
  55. class poly_timed_lockable_adapter: public poly_lockable_adapter<Mutex, Base>
  56. {
  57. public:
  58. typedef Mutex mutex_type;
  59. bool try_lock_until(chrono::system_clock::time_point const & abs_time)
  60. {
  61. return this->mtx().try_lock_until(abs_time);
  62. }
  63. bool try_lock_until(chrono::steady_clock::time_point const & abs_time)
  64. {
  65. return this->mtx().try_lock_until(abs_time);
  66. }
  67. bool try_lock_for(chrono::nanoseconds const & rel_time)
  68. {
  69. return this->mtx().try_lock_for(rel_time);
  70. }
  71. };
  72. //]
  73. }
  74. #endif