Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_CONDITION_HPP Chris@16: #define BOOST_INTERPROCESS_CONDITION_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) Chris@16: #include Chris@16: #define BOOST_INTERPROCESS_USE_POSIX Chris@16: //Experimental... Chris@16: #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: #include Chris@16: #define BOOST_INTERPROCESS_USE_WINDOWS Chris@16: #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: #include Chris@16: #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION Chris@16: #endif Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!\file Chris@16: //!Describes process-shared variables interprocess_condition class Chris@16: Chris@16: namespace boost { Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@101: Chris@16: namespace posix_time Chris@16: { class ptime; } Chris@16: Chris@101: #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@101: Chris@16: namespace interprocess { Chris@16: Chris@16: class named_condition; Chris@16: Chris@16: //!This class is a condition variable that can be placed in shared memory or Chris@16: //!memory mapped files. Chris@16: //!Destroys the object of type std::condition_variable_any Chris@16: //! Chris@16: //!Unlike std::condition_variable in C++11, it is NOT safe to invoke the destructor if all Chris@16: //!threads have been only notified. It is required that they have exited their respective wait Chris@101: //!functions. Chris@16: class interprocess_condition Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: interprocess_condition(const interprocess_condition &); Chris@16: interprocess_condition &operator=(const interprocess_condition &); Chris@16: friend class named_condition; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: public: Chris@16: //!Constructs a interprocess_condition. On error throws interprocess_exception. Chris@16: interprocess_condition() Chris@16: {} Chris@16: Chris@16: //!Destroys *this Chris@16: //!liberating system resources. Chris@16: ~interprocess_condition() Chris@16: {} Chris@16: Chris@16: //!If there is a thread waiting on *this, change that Chris@16: //!thread's state to ready. Otherwise there is no effect. Chris@16: void notify_one() Chris@16: { m_condition.notify_one(); } Chris@16: Chris@16: //!Change the state of all threads waiting on *this to ready. Chris@16: //!If there are no waiting threads, notify_all() has no effect. Chris@16: void notify_all() Chris@16: { m_condition.notify_all(); } Chris@16: Chris@16: //!Releases the lock on the interprocess_mutex object associated with lock, blocks Chris@16: //!the current thread of execution until readied by a call to Chris@16: //!this->notify_one() or this->notify_all(), and then reacquires the lock. Chris@16: template Chris@16: void wait(L& lock) Chris@101: { Chris@16: ipcdetail::internal_mutex_lock internal_lock(lock); Chris@16: m_condition.wait(internal_lock); Chris@16: } Chris@16: Chris@16: //!The same as: Chris@16: //!while (!pred()) wait(lock) Chris@16: template Chris@16: void wait(L& lock, Pr pred) Chris@16: { Chris@16: ipcdetail::internal_mutex_lock internal_lock(lock); Chris@16: m_condition.wait(internal_lock, pred); Chris@16: } Chris@16: Chris@16: //!Releases the lock on the interprocess_mutex object associated with lock, blocks Chris@16: //!the current thread of execution until readied by a call to Chris@16: //!this->notify_one() or this->notify_all(), or until time abs_time is reached, Chris@16: //!and then reacquires the lock. Chris@16: //!Returns: false if time abs_time is reached, otherwise true. Chris@16: template Chris@16: bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time) Chris@16: { Chris@16: ipcdetail::internal_mutex_lock internal_lock(lock); Chris@16: return m_condition.timed_wait(internal_lock, abs_time); Chris@16: } Chris@16: Chris@16: //!The same as: while (!pred()) { Chris@16: //! if (!timed_wait(lock, abs_time)) return pred(); Chris@16: //! } return true; Chris@16: template Chris@16: bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred) Chris@16: { Chris@16: ipcdetail::internal_mutex_lock internal_lock(lock); Chris@16: return m_condition.timed_wait(internal_lock, abs_time, pred); Chris@16: } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: private: Chris@16: #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION) Chris@16: #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION Chris@16: ipcdetail::spin_condition m_condition; Chris@16: #elif defined(BOOST_INTERPROCESS_USE_POSIX) Chris@16: #undef BOOST_INTERPROCESS_USE_POSIX Chris@16: ipcdetail::posix_condition m_condition; Chris@16: #elif defined(BOOST_INTERPROCESS_USE_WINDOWS) Chris@16: #undef BOOST_INTERPROCESS_USE_WINDOWS Chris@16: ipcdetail::windows_condition m_condition; Chris@16: #else Chris@16: #error "Unknown platform for interprocess_mutex" Chris@16: #endif Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: } //namespace interprocess Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_INTERPROCESS_CONDITION_HPP