lockable_concepts.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // (C) Copyright 2012 Vicente Botet
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
  6. #define BOOST_THREAD_LOCKABLE_CONCEPTS_HPP
  7. #include <boost/chrono/chrono.hpp>
  8. #include <boost/concept_check.hpp>
  9. namespace boost
  10. {
  11. /**
  12. * BasicLockable object supports the basic features
  13. * required to delimit a critical region
  14. * Supports the basic lock and unlock functions.
  15. */
  16. //[BasicLockable
  17. template <typename Mutex>
  18. struct BasicLockable
  19. {
  20. BOOST_CONCEPT_USAGE(BasicLockable)
  21. {
  22. l.lock();
  23. l.unlock();
  24. }
  25. BasicLockable() : l(*static_cast<Mutex*>(0)) {}
  26. private:
  27. BasicLockable operator=(BasicLockable const&);
  28. Mutex& l;
  29. }
  30. ;
  31. //]
  32. /**
  33. * Lockable extends BasicLockable
  34. * with try_lock functions.
  35. */
  36. //[Lockable
  37. template <typename Mutex>
  38. struct Lockable
  39. {
  40. BOOST_CONCEPT_ASSERT(( BasicLockable<Mutex> ));
  41. BOOST_CONCEPT_USAGE(Lockable)
  42. {
  43. if (l.try_lock()) return;
  44. }
  45. Lockable() : l(*static_cast<Mutex*>(0)) {}
  46. private:
  47. Lockable operator=(Lockable const&);
  48. Mutex& l;
  49. };
  50. //]
  51. /**
  52. * TimedLockable object extends Lockable
  53. * with timed lock functions: try_lock_until and try_lock_for and the exception based lock_until and lock_for
  54. */
  55. //[TimedLockable
  56. template <typename Mutex>
  57. struct TimedLockable
  58. {
  59. BOOST_CONCEPT_ASSERT(( Lockable<Mutex> ));
  60. BOOST_CONCEPT_USAGE(TimedLockable)
  61. {
  62. if (l.try_lock_until(t)) return;
  63. if (l.try_lock_for(d)) return;
  64. }
  65. TimedLockable() : l(*static_cast<Mutex*>(0)) {}
  66. private:
  67. TimedLockable operator=(TimedLockable const&);
  68. Mutex& l;
  69. chrono::system_clock::time_point t;
  70. chrono::system_clock::duration d;
  71. };
  72. //]
  73. /**
  74. * SharedLockable object extends TimedLockable
  75. * with the lock_shared, lock_shared_until, lock_shared_for, try_lock_shared_until, try_lock_shared
  76. * and unlock_shared functions
  77. */
  78. //[SharedLockable
  79. template <typename Mutex>
  80. struct SharedLockable
  81. {
  82. BOOST_CONCEPT_ASSERT(( TimedLockable<Mutex> ));
  83. BOOST_CONCEPT_USAGE(SharedLockable)
  84. {
  85. l.lock_shared();
  86. l.unlock_shared();
  87. if (l.try_lock_shared()) return;
  88. if (l.try_lock_shared_until(t)) return;
  89. if (l.try_lock_shared_for(d)) return;
  90. }
  91. SharedLockable() : l(*static_cast<Mutex*>(0)) {}
  92. private:
  93. SharedLockable operator=(SharedLockable const&);
  94. Mutex& l;
  95. chrono::system_clock::time_point t;
  96. chrono::system_clock::duration d;
  97. };
  98. //]
  99. /**
  100. * UpgradeLockable object extends SharedLockable
  101. * with the lock_upgrade, lock_upgrade_until, unlock_upgrade_and_lock,
  102. * unlock_and_lock_shared and unlock_upgrade_and_lock_shared functions
  103. */
  104. //[UpgradeLockable
  105. template <typename Mutex>
  106. struct UpgradeLockable
  107. {
  108. BOOST_CONCEPT_ASSERT(( SharedLockable<Mutex> ));
  109. BOOST_CONCEPT_USAGE(UpgradeLockable)
  110. {
  111. l.lock_upgrade();
  112. l.unlock_upgrade();
  113. if (l.try_lock_upgrade()) return;
  114. if (l.try_lock_upgrade_until(t)) return;
  115. if (l.try_lock_upgrade_for(d)) return;
  116. if (l.try_unlock_shared_and_lock()) return;
  117. if (l.try_unlock_shared_and_lock_until(t)) return;
  118. if (l.try_unlock_shared_and_lock_for(d)) return;
  119. l.unlock_and_lock_shared();
  120. if (l.try_unlock_shared_and_lock_upgrade()) return;
  121. if (l.try_unlock_shared_and_lock_upgrade_until(t)) return;
  122. if (l.try_unlock_shared_and_lock_upgrade_for(d)) return;
  123. l.unlock_and_lock_upgrade();
  124. l.unlock_upgrade_and_lock();
  125. if (l.try_unlock_upgrade_and_lock()) return;
  126. if (l.try_unlock_upgrade_and_lock_until(t)) return;
  127. if (l.try_unlock_upgrade_and_lock_for(d)) return;
  128. l.unlock_upgrade_and_lock_shared();
  129. }
  130. UpgradeLockable() : l(*static_cast<Mutex*>(0)) {}
  131. private:
  132. UpgradeLockable operator=(UpgradeLockable const&);
  133. Mutex& l;
  134. chrono::system_clock::time_point t;
  135. chrono::system_clock::duration d;
  136. };
  137. //]
  138. }
  139. #endif