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_SHARABLE_MUTEX_HPP Chris@16: #define BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_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: Chris@16: //!\file Chris@16: //!Describes a named sharable mutex 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: class named_condition; Chris@16: Chris@16: //!A sharable mutex with a global name, so it can be found from different Chris@16: //!processes. This mutex can't be placed in shared memory, and Chris@16: //!each process should have it's own named sharable mutex. Chris@16: class named_sharable_mutex Chris@16: { Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: //Non-copyable Chris@16: named_sharable_mutex(); Chris@16: named_sharable_mutex(const named_sharable_mutex &); Chris@16: named_sharable_mutex &operator=(const named_sharable_mutex &); Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: Chris@16: //!Creates a global sharable mutex with a name. Chris@16: //!If the sharable mutex can't be created throws interprocess_exception Chris@16: named_sharable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions()); Chris@16: Chris@16: //!Opens or creates a global sharable mutex with a name. Chris@16: //!If the sharable mutex is created, this call is equivalent to Chris@16: //!named_sharable_mutex(create_only_t, ...) Chris@16: //!If the sharable mutex is already created, this call is equivalent to Chris@16: //!named_sharable_mutex(open_only_t, ... ). Chris@16: named_sharable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions()); Chris@16: Chris@16: //!Opens a global sharable mutex with a name if that sharable mutex Chris@16: //!is previously. Chris@16: //!created. If it is not previously created this function throws Chris@16: //!interprocess_exception. Chris@16: named_sharable_mutex(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: ~named_sharable_mutex(); Chris@16: Chris@16: //Exclusive locking Chris@16: Chris@16: //!Effects: The calling thread tries to obtain exclusive ownership of the mutex, Chris@16: //! and if another thread has exclusive or sharable ownership of Chris@16: //! the mutex, it waits until it can obtain the ownership. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void lock(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire exclusive ownership of the mutex Chris@16: //! without waiting. If no other thread has exclusive or sharable Chris@16: //! ownership of the mutex this succeeds. Chris@16: //!Returns: If it can acquire exclusive ownership immediately returns true. Chris@16: //! If it has to wait, returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool try_lock(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire exclusive ownership of the mutex Chris@16: //! waiting if necessary until no other thread has exclusive, or sharable Chris@16: //! ownership of the mutex or abs_time is reached. Chris@16: //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool timed_lock(const boost::posix_time::ptime &abs_time); Chris@16: Chris@16: //!Precondition: The thread must have exclusive ownership of the mutex. Chris@16: //!Effects: The calling thread releases the exclusive ownership of the mutex. Chris@16: //!Throws: An exception derived from interprocess_exception on error. Chris@16: void unlock(); Chris@16: Chris@16: //Sharable locking Chris@16: Chris@16: //!Effects: The calling thread tries to obtain sharable ownership of the mutex, Chris@16: //! and if another thread has exclusive ownership of the mutex, Chris@16: //! waits until it can obtain the ownership. Chris@16: //!Throws: interprocess_exception on error. Chris@16: void lock_sharable(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire sharable ownership of the mutex Chris@16: //! without waiting. If no other thread has exclusive ownership Chris@16: //! of the mutex this succeeds. Chris@16: //!Returns: If it can acquire sharable ownership immediately returns true. If it Chris@16: //! has to wait, returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool try_lock_sharable(); Chris@16: Chris@16: //!Effects: The calling thread tries to acquire sharable ownership of the mutex Chris@16: //! waiting if necessary until no other thread has exclusive Chris@16: //! ownership of the mutex or abs_time is reached. Chris@16: //!Returns: If acquires sharable ownership, returns true. Otherwise returns false. Chris@16: //!Throws: interprocess_exception on error. Chris@16: bool timed_lock_sharable(const boost::posix_time::ptime &abs_time); Chris@16: Chris@16: //!Precondition: The thread must have sharable ownership of the mutex. Chris@16: //!Effects: The calling thread releases the sharable ownership of the mutex. Chris@16: //!Throws: An exception derived from interprocess_exception on error. Chris@16: void unlock_sharable(); Chris@16: Chris@16: //!Erases a named sharable mutex 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: friend class ipcdetail::interprocess_tester; Chris@16: void dont_close_on_destruction(); Chris@16: Chris@16: interprocess_sharable_mutex *mutex() const Chris@16: { return static_cast(m_shmem.get_user_address()); } 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: typedef 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 named_sharable_mutex::~named_sharable_mutex() Chris@16: {} Chris@16: Chris@16: inline named_sharable_mutex::named_sharable_mutex Chris@16: (create_only_t, const char *name, const permissions &perm) Chris@16: : m_shmem (create_only Chris@16: ,name Chris@16: ,sizeof(interprocess_sharable_mutex) + Chris@16: open_create_impl_t::ManagedOpenOrCreateUserOffset Chris@16: ,read_write Chris@16: ,0 Chris@16: ,construct_func_t(ipcdetail::DoCreate) Chris@16: ,perm) Chris@16: {} Chris@16: Chris@16: inline named_sharable_mutex::named_sharable_mutex Chris@16: (open_or_create_t, const char *name, const permissions &perm) Chris@16: : m_shmem (open_or_create Chris@16: ,name Chris@16: ,sizeof(interprocess_sharable_mutex) + Chris@16: open_create_impl_t::ManagedOpenOrCreateUserOffset Chris@16: ,read_write Chris@16: ,0 Chris@16: ,construct_func_t(ipcdetail::DoOpenOrCreate) Chris@16: ,perm) Chris@16: {} Chris@16: Chris@16: inline named_sharable_mutex::named_sharable_mutex Chris@16: (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(ipcdetail::DoOpen)) Chris@16: {} Chris@16: Chris@16: inline void named_sharable_mutex::dont_close_on_destruction() Chris@16: { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); } Chris@16: Chris@16: inline void named_sharable_mutex::lock() Chris@16: { this->mutex()->lock(); } Chris@16: Chris@16: inline void named_sharable_mutex::unlock() Chris@16: { this->mutex()->unlock(); } Chris@16: Chris@16: inline bool named_sharable_mutex::try_lock() Chris@16: { return this->mutex()->try_lock(); } Chris@16: Chris@16: inline bool named_sharable_mutex::timed_lock Chris@16: (const boost::posix_time::ptime &abs_time) Chris@16: { return this->mutex()->timed_lock(abs_time); } Chris@16: Chris@16: inline void named_sharable_mutex::lock_sharable() Chris@16: { this->mutex()->lock_sharable(); } Chris@16: Chris@16: inline void named_sharable_mutex::unlock_sharable() Chris@16: { this->mutex()->unlock_sharable(); } Chris@16: Chris@16: inline bool named_sharable_mutex::try_lock_sharable() Chris@16: { return this->mutex()->try_lock_sharable(); } Chris@16: Chris@16: inline bool named_sharable_mutex::timed_lock_sharable Chris@16: (const boost::posix_time::ptime &abs_time) Chris@16: { return this->mutex()->timed_lock_sharable(abs_time); } Chris@16: Chris@16: inline bool named_sharable_mutex::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 interprocess { Chris@16: } //namespace boost { Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP