named_condition_test.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <boost/interprocess/detail/workaround.hpp>
  12. #include <boost/interprocess/sync/named_mutex.hpp>
  13. #include <boost/interprocess/sync/named_condition.hpp>
  14. #include <boost/interprocess/sync/detail/locks.hpp>
  15. #include "condition_test_template.hpp"
  16. #include "named_creation_template.hpp"
  17. #include <string>
  18. #include <sstream>
  19. #include "get_process_id_name.hpp"
  20. using namespace boost::interprocess;
  21. struct condition_deleter
  22. {
  23. std::string name;
  24. ~condition_deleter()
  25. {
  26. if(name.empty())
  27. named_condition::remove(test::add_to_process_id_name("named_condition"));
  28. else
  29. named_condition::remove(name.c_str());
  30. }
  31. };
  32. inline std::string num_to_string(int n)
  33. { std::stringstream s; s << n; return s.str(); }
  34. //This wrapper is necessary to have a default constructor
  35. //in generic mutex_test_template functions
  36. class named_condition_test_wrapper
  37. : public condition_deleter, public named_condition
  38. {
  39. public:
  40. named_condition_test_wrapper()
  41. : named_condition(open_or_create,
  42. (test::add_to_process_id_name("test_cond") + num_to_string(count)).c_str())
  43. {
  44. condition_deleter::name += test::add_to_process_id_name("test_cond");
  45. condition_deleter::name += num_to_string(count);
  46. ++count;
  47. }
  48. ~named_condition_test_wrapper()
  49. { --count; }
  50. template <typename L>
  51. void wait(L& lock)
  52. {
  53. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  54. named_condition::wait(internal_lock);
  55. }
  56. template <typename L, typename Pr>
  57. void wait(L& lock, Pr pred)
  58. {
  59. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  60. named_condition::wait(internal_lock, pred);
  61. }
  62. template <typename L>
  63. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
  64. {
  65. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  66. return named_condition::timed_wait(internal_lock, abs_time);
  67. }
  68. template <typename L, typename Pr>
  69. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  70. {
  71. ipcdetail::internal_mutex_lock<L> internal_lock(lock);
  72. return named_condition::timed_wait(internal_lock, abs_time, pred);
  73. }
  74. static int count;
  75. };
  76. int named_condition_test_wrapper::count = 0;
  77. //This wrapper is necessary to have a common constructor
  78. //in generic named_creation_template functions
  79. class named_condition_creation_test_wrapper
  80. : public condition_deleter, public named_condition
  81. {
  82. public:
  83. named_condition_creation_test_wrapper(create_only_t)
  84. : named_condition(create_only, test::add_to_process_id_name("named_condition"))
  85. { ++count_; }
  86. named_condition_creation_test_wrapper(open_only_t)
  87. : named_condition(open_only, test::add_to_process_id_name("named_condition"))
  88. { ++count_; }
  89. named_condition_creation_test_wrapper(open_or_create_t)
  90. : named_condition(open_or_create, test::add_to_process_id_name("named_condition"))
  91. { ++count_; }
  92. ~named_condition_creation_test_wrapper() {
  93. if(--count_){
  94. ipcdetail::interprocess_tester::
  95. dont_close_on_destruction(static_cast<named_condition&>(*this));
  96. }
  97. }
  98. static int count_;
  99. };
  100. int named_condition_creation_test_wrapper::count_ = 0;
  101. struct mutex_deleter
  102. {
  103. std::string name;
  104. ~mutex_deleter()
  105. {
  106. if(name.empty())
  107. named_mutex::remove(test::add_to_process_id_name("named_mutex"));
  108. else
  109. named_mutex::remove(name.c_str());
  110. }
  111. };
  112. //This wrapper is necessary to have a default constructor
  113. //in generic mutex_test_template functions
  114. class named_mutex_test_wrapper
  115. : public mutex_deleter, public named_mutex
  116. {
  117. public:
  118. named_mutex_test_wrapper()
  119. : named_mutex(open_or_create,
  120. (test::add_to_process_id_name("test_mutex") + num_to_string(count)).c_str())
  121. {
  122. mutex_deleter::name += test::add_to_process_id_name("test_mutex");
  123. mutex_deleter::name += num_to_string(count);
  124. ++count;
  125. }
  126. typedef named_mutex internal_mutex_type;
  127. internal_mutex_type &internal_mutex()
  128. { return *this; }
  129. ~named_mutex_test_wrapper()
  130. { --count; }
  131. static int count;
  132. };
  133. int named_mutex_test_wrapper::count = 0;
  134. int main ()
  135. {
  136. try{
  137. //Remove previous mutexes and conditions
  138. named_mutex::remove(test::add_to_process_id_name("test_mutex0"));
  139. named_condition::remove(test::add_to_process_id_name("test_cond0"));
  140. named_condition::remove(test::add_to_process_id_name("test_cond1"));
  141. named_condition::remove(test::add_to_process_id_name("named_condition"));
  142. named_mutex::remove(test::add_to_process_id_name("named_mutex"));
  143. test::test_named_creation<named_condition_creation_test_wrapper>();
  144. test::do_test_condition<named_condition_test_wrapper
  145. ,named_mutex_test_wrapper>();
  146. }
  147. catch(std::exception &ex){
  148. std::cout << ex.what() << std::endl;
  149. return 1;
  150. }
  151. named_mutex::remove(test::add_to_process_id_name("test_mutex0"));
  152. named_condition::remove(test::add_to_process_id_name("test_cond0"));
  153. named_condition::remove(test::add_to_process_id_name("test_cond1"));
  154. named_condition::remove(test::add_to_process_id_name("named_condition"));
  155. named_mutex::remove(test::add_to_process_id_name("named_mutex"));
  156. return 0;
  157. }
  158. #include <boost/interprocess/detail/config_end.hpp>