/Src/Dependencies/Boost/boost/interprocess/sync/emulation/interprocess_condition.hpp

http://hadesmem.googlecode.com/ · C++ Header · 200 lines · 112 code · 24 blank · 64 comment · 23 complexity · 6351b5287424835a812b2455b2da500c MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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/posix_time_types_wrk.hpp>
  11. #include <boost/interprocess/detail/move.hpp>
  12. namespace boost {
  13. namespace interprocess {
  14. inline interprocess_condition::interprocess_condition()
  15. {
  16. //Note that this class is initialized to zero.
  17. //So zeroed memory can be interpreted as an initialized
  18. //condition variable
  19. m_command = SLEEP;
  20. m_num_waiters = 0;
  21. }
  22. inline interprocess_condition::~interprocess_condition()
  23. {
  24. //Trivial destructor
  25. }
  26. inline void interprocess_condition::notify_one()
  27. {
  28. this->notify(NOTIFY_ONE);
  29. }
  30. inline void interprocess_condition::notify_all()
  31. {
  32. this->notify(NOTIFY_ALL);
  33. }
  34. inline void interprocess_condition::notify(boost::uint32_t command)
  35. {
  36. //This interprocess_mutex guarantees that no other thread can enter to the
  37. //do_timed_wait method logic, so that thread count will be
  38. //constant until the function writes a NOTIFY_ALL command.
  39. //It also guarantees that no other notification can be signaled
  40. //on this interprocess_condition before this one ends
  41. m_enter_mut.lock();
  42. //Return if there are no waiters
  43. if(!detail::atomic_read32(&m_num_waiters)) {
  44. m_enter_mut.unlock();
  45. return;
  46. }
  47. //Notify that all threads should execute wait logic
  48. while(SLEEP != detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_command), command, SLEEP)){
  49. detail::thread_yield();
  50. }
  51. /*
  52. //Wait until the threads are woken
  53. while(SLEEP != detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_command), 0)){
  54. detail::thread_yield();
  55. }
  56. */
  57. //The enter interprocess_mutex will rest locked until the last waiting thread unlocks it
  58. }
  59. inline void interprocess_condition::do_wait(interprocess_mutex &mut)
  60. {
  61. this->do_timed_wait(false, boost::posix_time::ptime(), mut);
  62. }
  63. inline bool interprocess_condition::do_timed_wait
  64. (const boost::posix_time::ptime &abs_time, interprocess_mutex &mut)
  65. {
  66. return this->do_timed_wait(true, abs_time, mut);
  67. }
  68. inline bool interprocess_condition::do_timed_wait(bool tout_enabled,
  69. const boost::posix_time::ptime &abs_time,
  70. interprocess_mutex &mut)
  71. {
  72. boost::posix_time::ptime now = microsec_clock::universal_time();
  73. if(tout_enabled){
  74. if(now >= abs_time) return false;
  75. }
  76. typedef boost::interprocess::scoped_lock<interprocess_mutex> InternalLock;
  77. //The enter interprocess_mutex guarantees that while executing a notification,
  78. //no other thread can execute the do_timed_wait method.
  79. {
  80. //---------------------------------------------------------------
  81. InternalLock lock;
  82. if(tout_enabled){
  83. InternalLock dummy(m_enter_mut, abs_time);
  84. lock = boost::interprocess::move(dummy);
  85. }
  86. else{
  87. InternalLock dummy(m_enter_mut);
  88. lock = boost::interprocess::move(dummy);
  89. }
  90. if(!lock)
  91. return false;
  92. //---------------------------------------------------------------
  93. //We increment the waiting thread count protected so that it will be
  94. //always constant when another thread enters the notification logic.
  95. //The increment marks this thread as "waiting on interprocess_condition"
  96. detail::atomic_inc32(const_cast<boost::uint32_t*>(&m_num_waiters));
  97. //We unlock the external interprocess_mutex atomically with the increment
  98. mut.unlock();
  99. }
  100. //By default, we suppose that no timeout has happened
  101. bool timed_out = false, unlock_enter_mut= false;
  102. //Loop until a notification indicates that the thread should
  103. //exit or timeout occurs
  104. while(1){
  105. //The thread sleeps/spins until a interprocess_condition commands a notification
  106. //Notification occurred, we will lock the checking interprocess_mutex so that
  107. while(detail::atomic_read32(&m_command) == SLEEP){
  108. detail::thread_yield();
  109. //Check for timeout
  110. if(tout_enabled){
  111. now = microsec_clock::universal_time();
  112. if(now >= abs_time){
  113. //If we can lock the interprocess_mutex it means that no notification
  114. //is being executed in this interprocess_condition variable
  115. timed_out = m_enter_mut.try_lock();
  116. //If locking fails, indicates that another thread is executing
  117. //notification, so we play the notification game
  118. if(!timed_out){
  119. //There is an ongoing notification, we will try again later
  120. continue;
  121. }
  122. //No notification in execution, since enter interprocess_mutex is locked.
  123. //We will execute time-out logic, so we will decrement count,
  124. //release the enter interprocess_mutex and return false.
  125. break;
  126. }
  127. }
  128. }
  129. //If a timeout occurred, the interprocess_mutex will not execute checking logic
  130. if(tout_enabled && timed_out){
  131. //Decrement wait count
  132. detail::atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
  133. unlock_enter_mut = true;
  134. break;
  135. }
  136. else{
  137. boost::uint32_t result = detail::atomic_cas32
  138. (const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ONE);
  139. if(result == SLEEP){
  140. //Other thread has been notified and since it was a NOTIFY one
  141. //command, this thread must sleep again
  142. continue;
  143. }
  144. else if(result == NOTIFY_ONE){
  145. //If it was a NOTIFY_ONE command, only this thread should
  146. //exit. This thread has atomically marked command as sleep before
  147. //so no other thread will exit.
  148. //Decrement wait count.
  149. unlock_enter_mut = true;
  150. detail::atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
  151. break;
  152. }
  153. else{
  154. //If it is a NOTIFY_ALL command, all threads should return
  155. //from do_timed_wait function. Decrement wait count.
  156. unlock_enter_mut = 1 == detail::atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
  157. //Check if this is the last thread of notify_all waiters
  158. //Only the last thread will release the interprocess_mutex
  159. if(unlock_enter_mut){
  160. detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ALL);
  161. }
  162. break;
  163. }
  164. }
  165. }
  166. //Unlock the enter interprocess_mutex if it is a single notification, if this is
  167. //the last notified thread in a notify_all or a timeout has occurred
  168. if(unlock_enter_mut){
  169. m_enter_mut.unlock();
  170. }
  171. //Lock external again before returning from the method
  172. mut.lock();
  173. return !timed_out;
  174. }
  175. } //namespace interprocess
  176. } // namespace boost