PageRenderTime 80ms CodeModel.GetById 68ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

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