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_NAMED_CONDITION_ANY_HPP Chris@16: #define BOOST_INTERPROCESS_NAMED_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@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS) Chris@16: #include Chris@16: #define BOOST_INTERPROCESS_USE_WINDOWS Chris@16: #else Chris@16: #include Chris@16: #endif Chris@16: Chris@16: //!\file Chris@16: //!Describes a named condition class for inter-process synchronization Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: namespace ipcdetail{ class interprocess_tester; } Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: //! A global condition variable that can be created by name. Chris@16: //! This condition variable is designed to work with named_mutex and Chris@16: //! can't be placed in shared memory or memory mapped files. Chris@16: class named_condition_any Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: named_condition_any(); Chris@16: named_condition_any(const named_condition_any &); Chris@16: named_condition_any &operator=(const named_condition_any &); Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: //!Creates a global condition with a name. Chris@16: //!If the condition can't be created throws interprocess_exception Chris@16: named_condition_any(create_only_t, const char *name, const permissions &perm = permissions()) Chris@16: : m_cond(create_only_t(), name, perm) Chris@16: {} Chris@16: Chris@16: //!Opens or creates a global condition with a name. Chris@16: //!If the condition is created, this call is equivalent to Chris@16: //!named_condition_any(create_only_t, ... ) Chris@16: //!If the condition is already created, this call is equivalent Chris@16: //!named_condition_any(open_only_t, ... ) Chris@16: //!Does not throw Chris@16: named_condition_any(open_or_create_t, const char *name, const permissions &perm = permissions()) Chris@16: : m_cond(open_or_create_t(), name, perm) Chris@16: {} Chris@16: Chris@16: //!Opens a global condition with a name if that condition is previously Chris@16: //!created. If it is not previously created this function throws Chris@16: //!interprocess_exception. Chris@16: named_condition_any(open_only_t, const char *name) Chris@16: : m_cond(open_only_t(), name) Chris@16: {} Chris@16: Chris@16: //!Destroys *this and indicates that the calling process is finished using Chris@16: //!the resource. The destructor function will deallocate Chris@16: //!any system resources allocated by the system for use by this process for Chris@16: //!this resource. The resource can still be opened again calling Chris@16: //!the open constructor overload. To erase the resource from the system Chris@16: //!use remove(). Chris@16: ~named_condition_any() 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_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 named_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: { return 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: { return m_cond.wait(lock, pred); } Chris@16: Chris@16: //!Releases the lock on the named_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: //!Erases a named condition from the system. Chris@16: //!Returns false on error. Never throws. Chris@16: static bool remove(const char *name) Chris@16: { return condition_any_type::remove(name); } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: #if defined(BOOST_INTERPROCESS_USE_WINDOWS) Chris@16: typedef ipcdetail::windows_named_condition_any condition_any_type; Chris@16: #else Chris@16: typedef ipcdetail::shm_named_condition_any condition_any_type; Chris@16: #endif Chris@16: condition_any_type m_cond; Chris@16: Chris@16: friend class ipcdetail::interprocess_tester; Chris@16: void dont_close_on_destruction() Chris@16: { ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); } 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_NAMED_CONDITION_ANY_HPP