Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2012-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_ANY_HPP Chris@16: #define BOOST_INTERPROCESS_CONDITION_ANY_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: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //!\file Chris@16: //!Describes process-shared variables interprocess_condition_any 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: //!This class is a condition variable that can be placed in shared memory or Chris@16: //!memory mapped files. Chris@16: //! Chris@16: //!The interprocess_condition_any class is a generalization of interprocess_condition. Chris@16: //!Whereas interprocess_condition works only on Locks with mutex_type == interprocess_mutex Chris@16: //!interprocess_condition_any can operate on any user-defined lock that meets the BasicLockable Chris@16: //!requirements (lock()/unlock() member functions). Chris@16: //! Chris@16: //!Unlike std::condition_variable_any 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_any Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: interprocess_condition_any(const interprocess_condition_any &); Chris@16: interprocess_condition_any &operator=(const interprocess_condition_any &); Chris@16: Chris@16: class members Chris@16: { Chris@16: public: Chris@16: typedef interprocess_condition condvar_type; Chris@16: typedef interprocess_mutex mutex_type; Chris@101: Chris@16: condvar_type &get_condvar() { return m_cond; } Chris@16: mutex_type &get_mutex() { return m_mut; } Chris@16: Chris@16: private: Chris@16: condvar_type m_cond; Chris@16: mutex_type m_mut; Chris@16: }; Chris@16: Chris@16: ipcdetail::condition_any_wrapper m_cond; Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: //!Constructs a interprocess_condition_any. On error throws interprocess_exception. Chris@16: interprocess_condition_any(){} Chris@16: Chris@16: //!Destroys *this Chris@16: //!liberating system resources. Chris@16: ~interprocess_condition_any(){} 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_cond.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_cond.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@16: { m_cond.wait(lock); } 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: { m_cond.wait(lock, pred); } 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: { return m_cond.timed_wait(lock, abs_time); } 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: { return m_cond.timed_wait(lock, abs_time, pred); } 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_ANY_HPP