upgradable_mutex_test.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-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/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include "mutex_test_template.hpp"
  12. #include "sharable_mutex_test_template.hpp"
  13. #include <boost/interprocess/sync/interprocess_upgradable_mutex.hpp>
  14. #include <boost/interprocess/sync/scoped_lock.hpp>
  15. #include <boost/interprocess/sync/sharable_lock.hpp>
  16. #include <boost/interprocess/sync/upgradable_lock.hpp>
  17. #include <boost/date_time/posix_time/posix_time_types.hpp>
  18. #include "util.hpp"
  19. int main ()
  20. {
  21. using namespace boost::interprocess;
  22. test::test_all_lock<interprocess_upgradable_mutex>();
  23. test::test_all_mutex<interprocess_upgradable_mutex>();
  24. test::test_all_sharable_mutex<interprocess_upgradable_mutex>();
  25. //Test lock transition
  26. {
  27. typedef interprocess_upgradable_mutex Mutex;
  28. Mutex mut;
  29. Mutex mut2;
  30. //Conversions to scoped_lock
  31. {
  32. scoped_lock<Mutex> lock(mut);
  33. scoped_lock<Mutex> e_lock(boost::move(lock));
  34. lock.swap(e_lock);
  35. }
  36. {
  37. scoped_lock<Mutex> lock(mut);
  38. scoped_lock<Mutex> e_lock(mut2);
  39. e_lock = boost::move(lock);
  40. }
  41. {
  42. upgradable_lock<Mutex> u_lock(mut);
  43. //This calls unlock_upgradable_and_lock()
  44. scoped_lock<Mutex> e_lock(boost::move(u_lock));
  45. }
  46. {
  47. upgradable_lock<Mutex> u_lock(mut);
  48. //This calls unlock_upgradable_and_lock()
  49. scoped_lock<Mutex> e_lock(mut2);
  50. scoped_lock<Mutex> moved(boost::move(u_lock));
  51. e_lock = boost::move(moved);
  52. }
  53. {
  54. upgradable_lock<Mutex> u_lock(mut);
  55. //This calls try_unlock_upgradable_and_lock()
  56. scoped_lock<Mutex> e_lock(boost::move(u_lock), try_to_lock);
  57. }
  58. {
  59. upgradable_lock<Mutex> u_lock(mut);
  60. //This calls try_unlock_upgradable_and_lock()
  61. scoped_lock<Mutex> e_lock(mut2);
  62. scoped_lock<Mutex> moved(boost::move(u_lock), try_to_lock);
  63. e_lock = boost::move(moved);
  64. }
  65. {
  66. boost::posix_time::ptime t = test::delay(100);
  67. upgradable_lock<Mutex> u_lock(mut);
  68. //This calls timed_unlock_upgradable_and_lock()
  69. scoped_lock<Mutex> e_lock(boost::move(u_lock), t);
  70. }
  71. {
  72. boost::posix_time::ptime t = test::delay(100);
  73. upgradable_lock<Mutex> u_lock(mut);
  74. //This calls timed_unlock_upgradable_and_lock()
  75. scoped_lock<Mutex> e_lock(mut2);
  76. scoped_lock<Mutex> moved(boost::move(u_lock), t);
  77. e_lock = boost::move(moved);
  78. }
  79. {
  80. sharable_lock<Mutex> s_lock(mut);
  81. //This calls try_unlock_sharable_and_lock()
  82. scoped_lock<Mutex> e_lock(boost::move(s_lock), try_to_lock);
  83. }
  84. {
  85. sharable_lock<Mutex> s_lock(mut);
  86. //This calls try_unlock_sharable_and_lock()
  87. scoped_lock<Mutex> e_lock(mut2);
  88. scoped_lock<Mutex> moved(boost::move(s_lock), try_to_lock);
  89. e_lock = boost::move(moved);
  90. }
  91. //Conversions to upgradable_lock
  92. {
  93. upgradable_lock<Mutex> lock(mut);
  94. upgradable_lock<Mutex> u_lock(boost::move(lock));
  95. lock.swap(u_lock);
  96. }
  97. {
  98. upgradable_lock<Mutex> lock(mut);
  99. upgradable_lock<Mutex> u_lock(mut2);
  100. upgradable_lock<Mutex> moved(boost::move(lock));
  101. u_lock = boost::move(moved);
  102. }
  103. {
  104. sharable_lock<Mutex> s_lock(mut);
  105. //This calls unlock_sharable_and_lock_upgradable()
  106. upgradable_lock<Mutex> u_lock(boost::move(s_lock), try_to_lock);
  107. }
  108. {
  109. sharable_lock<Mutex> s_lock(mut);
  110. //This calls unlock_sharable_and_lock_upgradable()
  111. upgradable_lock<Mutex> u_lock(mut2);
  112. upgradable_lock<Mutex> moved(boost::move(s_lock), try_to_lock);
  113. u_lock = boost::move(moved);
  114. }
  115. {
  116. scoped_lock<Mutex> e_lock(mut);
  117. //This calls unlock_and_lock_upgradable()
  118. upgradable_lock<Mutex> u_lock(boost::move(e_lock));
  119. }
  120. {
  121. scoped_lock<Mutex> e_lock(mut);
  122. //This calls unlock_and_lock_upgradable()
  123. upgradable_lock<Mutex> u_lock(mut2);
  124. upgradable_lock<Mutex> moved(boost::move(e_lock));
  125. u_lock = boost::move(moved);
  126. }
  127. //Conversions to sharable_lock
  128. {
  129. sharable_lock<Mutex> lock(mut);
  130. sharable_lock<Mutex> s_lock(boost::move(lock));
  131. lock.swap(s_lock);
  132. }
  133. {
  134. sharable_lock<Mutex> lock(mut);
  135. sharable_lock<Mutex> s_lock(mut2);
  136. sharable_lock<Mutex> moved(boost::move(lock));
  137. s_lock = boost::move(moved);
  138. }
  139. {
  140. upgradable_lock<Mutex> u_lock(mut);
  141. //This calls unlock_upgradable_and_lock_sharable()
  142. sharable_lock<Mutex> s_lock(boost::move(u_lock));
  143. }
  144. {
  145. upgradable_lock<Mutex> u_lock(mut);
  146. //This calls unlock_upgradable_and_lock_sharable()
  147. sharable_lock<Mutex> s_lock(mut2);
  148. sharable_lock<Mutex> moved(boost::move(u_lock));
  149. s_lock = boost::move(moved);
  150. }
  151. {
  152. scoped_lock<Mutex> e_lock(mut);
  153. //This calls unlock_and_lock_sharable()
  154. sharable_lock<Mutex> s_lock(boost::move(e_lock));
  155. }
  156. {
  157. scoped_lock<Mutex> e_lock(mut);
  158. //This calls unlock_and_lock_sharable()
  159. sharable_lock<Mutex> s_lock(mut2);
  160. sharable_lock<Mutex> moved(boost::move(e_lock));
  161. s_lock = boost::move(moved);
  162. }
  163. }
  164. return 0;
  165. }
  166. #include <boost/interprocess/detail/config_end.hpp>