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_SHM_NAMED_CONDITION_HPP Chris@16: #define BOOST_INTERPROCESS_SHM_NAMED_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@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: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES) Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #else Chris@16: #include Chris@16: #endif Chris@16: Chris@16: Chris@16: //!\file Chris@16: //!Describes process-shared variables interprocess_condition class Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: namespace ipcdetail { Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: 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 shm_named_condition Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: shm_named_condition(); Chris@16: shm_named_condition(const shm_named_condition &); Chris@16: shm_named_condition &operator=(const shm_named_condition &); 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: shm_named_condition(create_only_t create_only, const char *name, const permissions &perm = permissions()); 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: //!shm_named_condition(create_only_t, ... ) Chris@16: //!If the condition is already created, this call is equivalent Chris@16: //!shm_named_condition(open_only_t, ... ) Chris@16: //!Does not throw Chris@16: shm_named_condition(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions()); 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: shm_named_condition(open_only_t open_only, const char *name); 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: ~shm_named_condition(); 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: 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: 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: 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: //!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: 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: //!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: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: Chris@16: #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES) Chris@16: class internal_condition_members Chris@16: { Chris@16: public: Chris@16: typedef interprocess_mutex mutex_type; Chris@16: typedef interprocess_condition condvar_type; Chris@101: Chris@16: condvar_type& get_condvar() { return m_cond; } Chris@16: mutex_type& get_mutex() { return m_mtx; } Chris@16: Chris@16: private: Chris@16: mutex_type m_mtx; Chris@16: condvar_type m_cond; Chris@16: }; Chris@16: Chris@16: typedef ipcdetail::condition_any_wrapper internal_condition; Chris@16: #else //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES) Chris@16: typedef interprocess_condition internal_condition; Chris@16: #endif //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES) Chris@16: Chris@16: internal_condition &internal_cond() Chris@16: { return *static_cast(m_shmem.get_user_address()); } Chris@16: Chris@16: friend class boost::interprocess::ipcdetail::interprocess_tester; Chris@16: void dont_close_on_destruction(); Chris@16: Chris@16: typedef ipcdetail::managed_open_or_create_impl open_create_impl_t; Chris@16: open_create_impl_t m_shmem; Chris@16: Chris@16: template friend class boost::interprocess::ipcdetail::named_creation_functor; Chris@16: typedef boost::interprocess::ipcdetail::named_creation_functor construct_func_t; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: Chris@16: inline shm_named_condition::~shm_named_condition() Chris@16: {} Chris@16: Chris@16: inline shm_named_condition::shm_named_condition(create_only_t, const char *name, const permissions &perm) Chris@16: : m_shmem (create_only Chris@16: ,name Chris@16: ,sizeof(internal_condition) + Chris@16: open_create_impl_t::ManagedOpenOrCreateUserOffset Chris@16: ,read_write Chris@16: ,0 Chris@16: ,construct_func_t(DoCreate) Chris@16: ,perm) Chris@16: {} Chris@16: Chris@16: inline shm_named_condition::shm_named_condition(open_or_create_t, const char *name, const permissions &perm) Chris@16: : m_shmem (open_or_create Chris@16: ,name Chris@16: ,sizeof(internal_condition) + Chris@16: open_create_impl_t::ManagedOpenOrCreateUserOffset Chris@16: ,read_write Chris@16: ,0 Chris@16: ,construct_func_t(DoOpenOrCreate) Chris@16: ,perm) Chris@16: {} Chris@16: Chris@16: inline shm_named_condition::shm_named_condition(open_only_t, const char *name) Chris@16: : m_shmem (open_only Chris@16: ,name Chris@16: ,read_write Chris@16: ,0 Chris@16: ,construct_func_t(DoOpen)) Chris@16: {} Chris@16: Chris@16: inline void shm_named_condition::dont_close_on_destruction() Chris@16: { interprocess_tester::dont_close_on_destruction(m_shmem); } Chris@16: Chris@16: inline void shm_named_condition::notify_one() Chris@16: { this->internal_cond().notify_one(); } Chris@16: Chris@16: inline void shm_named_condition::notify_all() Chris@16: { this->internal_cond().notify_all(); } Chris@16: Chris@16: template Chris@16: inline void shm_named_condition::wait(L& lock) Chris@16: { this->internal_cond().wait(lock); } Chris@16: Chris@16: template Chris@16: inline void shm_named_condition::wait(L& lock, Pr pred) Chris@16: { this->internal_cond().wait(lock, pred); } Chris@16: Chris@16: template Chris@16: inline bool shm_named_condition::timed_wait Chris@16: (L& lock, const boost::posix_time::ptime &abs_time) Chris@16: { return this->internal_cond().timed_wait(lock, abs_time); } Chris@16: Chris@16: template Chris@16: inline bool shm_named_condition::timed_wait Chris@16: (L& lock, const boost::posix_time::ptime &abs_time, Pr pred) Chris@16: { return this->internal_cond().timed_wait(lock, abs_time, pred); } Chris@16: Chris@16: inline bool shm_named_condition::remove(const char *name) Chris@16: { return shared_memory_object::remove(name); } Chris@16: Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: Chris@16: } //namespace ipcdetail Chris@16: } //namespace interprocess Chris@16: } //namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_HPP