PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 179 lines | 107 code | 31 blank | 41 comment | 13 complexity | ce8a7dd9760f21e8ecd1e291838adce9 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#ifndef BOOST_INTERPROCESS_CONDITION_HPP
 12#define BOOST_INTERPROCESS_CONDITION_HPP
 13
 14#if (defined _MSC_VER) && (_MSC_VER >= 1200)
 15#  pragma once
 16#endif
 17
 18/// @cond
 19
 20#include <boost/interprocess/detail/config_begin.hpp>
 21#include <boost/interprocess/detail/workaround.hpp>
 22
 23#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
 24#include <boost/interprocess/sync/scoped_lock.hpp>
 25#include <boost/interprocess/sync/interprocess_condition.hpp>
 26#include <boost/interprocess/sync/interprocess_mutex.hpp>
 27#include <boost/interprocess/exceptions.hpp>
 28#include <boost/limits.hpp>
 29#include <boost/assert.hpp>
 30
 31#if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
 32   #include <pthread.h>
 33   #include <errno.h>   
 34   #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
 35   #define BOOST_INTERPROCESS_USE_POSIX
 36#else
 37   #include <boost/interprocess/detail/atomic.hpp>
 38   #include <boost/cstdint.hpp>
 39   #include <boost/interprocess/detail/os_thread_functions.hpp>
 40   #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
 41#endif
 42
 43/// @endcond
 44
 45//!\file
 46//!Describes process-shared variables interprocess_condition class
 47
 48namespace boost {
 49
 50namespace posix_time
 51{  class ptime;   }
 52
 53namespace interprocess {
 54
 55class named_condition;
 56
 57//!This class is a condition variable that can be placed in shared memory or
 58//!memory mapped files.
 59class interprocess_condition
 60{
 61   /// @cond
 62   //Non-copyable
 63   interprocess_condition(const interprocess_condition &);
 64   interprocess_condition &operator=(const interprocess_condition &);
 65   friend class named_condition;
 66   /// @endcond
 67   public:
 68   //!Constructs a interprocess_condition. On error throws interprocess_exception.
 69   interprocess_condition();
 70
 71   //!Destroys *this
 72   //!liberating system resources.
 73   ~interprocess_condition();
 74
 75   //!If there is a thread waiting on *this, change that 
 76   //!thread's state to ready. Otherwise there is no effect.
 77   void notify_one();
 78
 79   //!Change the state of all threads waiting on *this to ready.
 80   //!If there are no waiting threads, notify_all() has no effect.
 81   void notify_all();
 82
 83   //!Releases the lock on the interprocess_mutex object associated with lock, blocks 
 84   //!the current thread of execution until readied by a call to 
 85   //!this->notify_one() or this->notify_all(), and then reacquires the lock.
 86   template <typename L>
 87   void wait(L& lock)
 88   {
 89      if (!lock)
 90         throw lock_exception();
 91      do_wait(*lock.mutex());
 92   }
 93
 94   //!The same as:
 95   //!while (!pred()) wait(lock)
 96   template <typename L, typename Pr>
 97   void wait(L& lock, Pr pred)
 98   {
 99      if (!lock)
100         throw lock_exception();
101
102      while (!pred())
103         do_wait(*lock.mutex());
104   }
105
106   //!Releases the lock on the interprocess_mutex object associated with lock, blocks 
107   //!the current thread of execution until readied by a call to 
108   //!this->notify_one() or this->notify_all(), or until time abs_time is reached, 
109   //!and then reacquires the lock.
110   //!Returns: false if time abs_time is reached, otherwise true.
111   template <typename L>
112   bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
113   {
114      if(abs_time == boost::posix_time::pos_infin){
115         this->wait(lock);
116         return true;
117      }
118      if (!lock)
119            throw lock_exception();
120      return do_timed_wait(abs_time, *lock.mutex());
121   }
122
123   //!The same as:   while (!pred()) { 
124   //!                  if (!timed_wait(lock, abs_time)) return pred(); 
125   //!               } return true;
126   template <typename L, typename Pr>
127   bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
128   {
129      if(abs_time == boost::posix_time::pos_infin){
130         this->wait(lock, pred);
131         return true;
132      }
133      if (!lock)
134            throw lock_exception();
135      while (!pred()){
136         if (!do_timed_wait(abs_time, *lock.mutex()))
137            return pred();
138      }
139
140      return true;
141   }
142
143   /// @cond
144   private:
145   void do_wait(interprocess_mutex  &mut);
146
147   bool do_timed_wait(const boost::posix_time::ptime &abs_time, interprocess_mutex &mut);
148
149   #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
150      enum { SLEEP = 0, NOTIFY_ONE, NOTIFY_ALL };
151      interprocess_mutex         m_enter_mut;
152      //interprocess_mutex       m_check_mut;
153      volatile boost::uint32_t    m_command;
154      volatile boost::uint32_t    m_num_waiters;
155      bool do_timed_wait(bool tout_enabled, const boost::posix_time::ptime &abs_time, interprocess_mutex &mut);
156      void notify(boost::uint32_t command);
157   #elif defined(BOOST_INTERPROCESS_USE_POSIX)
158      pthread_cond_t   m_condition;
159   #endif
160   /// @endcond
161};
162
163}  //namespace interprocess
164
165}  // namespace boost
166
167#ifdef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
168#  undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
169#  include <boost/interprocess/sync/emulation/interprocess_condition.hpp>
170#endif
171
172#ifdef BOOST_INTERPROCESS_USE_POSIX
173#  undef BOOST_INTERPROCESS_USE_POSIX
174#  include <boost/interprocess/sync/posix/interprocess_condition.hpp>
175#endif
176
177#include <boost/interprocess/detail/config_end.hpp>
178
179#endif // BOOST_INTERPROCESS_CONDITION_HPP