static_mutex_test.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. *
  3. * Copyright (c) 2004
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE static_mutex_test.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: test program for boost::static_mutex.
  16. */
  17. #include <boost/regex/pending/static_mutex.hpp>
  18. #include <boost/thread/thread.hpp>
  19. #include <boost/timer.hpp>
  20. #include <iostream>
  21. #include <iomanip>
  22. //
  23. // we cannot use the regular Boost.Test in here: it is not thread safe
  24. // and calls to BOOST_CHECK will eventually crash on some compilers
  25. // (Borland certainly) due to race conditions inside the Boost.Test lib.
  26. //
  27. #define BOOST_CHECK(pred) if(!(pred)) failed_test(__FILE__, __LINE__, BOOST_STRINGIZE(pred));
  28. int total_failures = 0;
  29. void failed_test(const char* file, int line, const char* pred)
  30. {
  31. static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
  32. boost::static_mutex::scoped_lock guard(mut);
  33. ++total_failures;
  34. std::cout << "Failed test in \"" << file << "\" at line " << line << ": " << pred << std::endl;
  35. }
  36. void print_cycles(int c)
  37. {
  38. static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
  39. boost::static_mutex::scoped_lock guard(mut);
  40. std::cout << "Thread exited after " << c << " cycles." << std::endl;
  41. }
  42. bool sufficient_time()
  43. {
  44. // return true if enough time has passed since the tests began:
  45. static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
  46. boost::static_mutex::scoped_lock guard(mut);
  47. static boost::timer t;
  48. // is 10 seconds enough?
  49. return t.elapsed() >= 10.0;
  50. }
  51. // define three trivial test proceedures:
  52. bool t1()
  53. {
  54. static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
  55. static int has_lock = 0;
  56. static int data = 10000;
  57. boost::static_mutex::scoped_lock guard(mut);
  58. BOOST_CHECK(++has_lock == 1);
  59. BOOST_CHECK(guard.locked());
  60. BOOST_CHECK(guard);
  61. bool result = (--data > 0) ? true : false;
  62. BOOST_CHECK(--has_lock == 0);
  63. return result;
  64. }
  65. bool t2()
  66. {
  67. static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
  68. static int has_lock = 0;
  69. static int data = 10000;
  70. boost::static_mutex::scoped_lock guard(mut, false);
  71. BOOST_CHECK(0 == guard.locked());
  72. BOOST_CHECK(!guard);
  73. guard.lock();
  74. BOOST_CHECK(++has_lock == 1);
  75. BOOST_CHECK(guard.locked());
  76. BOOST_CHECK(guard);
  77. bool result = (--data > 0) ? true : false;
  78. BOOST_CHECK(--has_lock == 0);
  79. guard.unlock();
  80. BOOST_CHECK(0 == guard.locked());
  81. BOOST_CHECK(!guard);
  82. return result;
  83. }
  84. bool t3()
  85. {
  86. static boost::static_mutex mut = BOOST_STATIC_MUTEX_INIT ;
  87. static int has_lock = 0;
  88. static int data = 10000;
  89. boost::static_mutex::scoped_lock guard(mut);
  90. BOOST_CHECK(++has_lock == 1);
  91. BOOST_CHECK(guard.locked());
  92. BOOST_CHECK(guard);
  93. bool result = (--data > 0) ? true : false;
  94. BOOST_CHECK(--has_lock == 0);
  95. return result;
  96. }
  97. // define their thread procs:
  98. void thread1_proc()
  99. {
  100. int cycles = 0;
  101. while(!sufficient_time())
  102. {
  103. t1();
  104. t2();
  105. ++cycles;
  106. }
  107. print_cycles(cycles);
  108. }
  109. void thread2_proc()
  110. {
  111. int cycles = 0;
  112. while(!sufficient_time())
  113. {
  114. t2();
  115. t3();
  116. ++cycles;
  117. }
  118. print_cycles(cycles);
  119. }
  120. void thread3_proc()
  121. {
  122. int cycles = 0;
  123. while(!sufficient_time())
  124. {
  125. t1();
  126. t3();
  127. ++cycles;
  128. }
  129. print_cycles(cycles);
  130. }
  131. // make sure that at least one of our test proceedures
  132. // is called during program startup:
  133. struct startup1
  134. {
  135. startup1()
  136. {
  137. t1();
  138. }
  139. ~startup1()
  140. {
  141. t1();
  142. }
  143. };
  144. startup1 up1;
  145. int main()
  146. {
  147. (void)up1;
  148. std::list<boost::shared_ptr<boost::thread> > threads;
  149. for(int i = 0; i < 2; ++i)
  150. {
  151. try{
  152. threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&thread1_proc)));
  153. }
  154. catch(const std::exception& e)
  155. {
  156. std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
  157. }
  158. }
  159. for(int i = 0; i < 2; ++i)
  160. {
  161. try{
  162. threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&thread2_proc)));
  163. }
  164. catch(const std::exception& e)
  165. {
  166. std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
  167. }
  168. }
  169. for(int i = 0; i < 2; ++i)
  170. {
  171. try{
  172. threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(&thread3_proc)));
  173. }
  174. catch(const std::exception& e)
  175. {
  176. std::cerr << "<note>Thread creation failed with message: " << e.what() << "</note>" << std::endl;
  177. }
  178. }
  179. std::list<boost::shared_ptr<boost::thread> >::const_iterator a(threads.begin()), b(threads.end());
  180. while(a != b)
  181. {
  182. (*a)->join();
  183. ++a;
  184. }
  185. return total_failures;
  186. }