Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-09 08:13:45

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
0012 #define BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 #include <boost/interprocess/creation_tags.hpp>
0025 #include <boost/interprocess/exceptions.hpp>
0026 #include <boost/interprocess/shared_memory_object.hpp>
0027 #include <boost/interprocess/timed_utils.hpp>
0028 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
0029 #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
0030 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
0031 #include <boost/interprocess/permissions.hpp>
0032 
0033 //!\file
0034 //!Describes a named sharable mutex class for inter-process synchronization
0035 
0036 namespace boost {
0037 namespace interprocess {
0038 
0039 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0040 namespace ipcdetail{ class interprocess_tester; }
0041 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0042 
0043 class named_condition;
0044 
0045 //!A sharable mutex with a global name, so it can be found from different
0046 //!processes. This mutex can't be placed in shared memory, and
0047 //!each process should have it's own named sharable mutex.
0048 class named_sharable_mutex
0049 {
0050    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0051    //Non-copyable
0052    named_sharable_mutex();
0053    named_sharable_mutex(const named_sharable_mutex &);
0054    named_sharable_mutex &operator=(const named_sharable_mutex &);
0055    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0056    public:
0057 
0058    //!Creates a global sharable mutex with a name.
0059    //!If the sharable mutex can't be created throws interprocess_exception
0060    named_sharable_mutex(create_only_t, const char *name, const permissions &perm = permissions());
0061 
0062    //!Opens or creates a global sharable mutex with a name.
0063    //!If the sharable mutex is created, this call is equivalent to
0064    //!named_sharable_mutex(create_only_t, ...)
0065    //!If the sharable mutex is already created, this call is equivalent to
0066    //!named_sharable_mutex(open_only_t, ... ).
0067    named_sharable_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
0068 
0069    //!Opens a global sharable mutex with a name if that sharable mutex
0070    //!is previously.
0071    //!created. If it is not previously created this function throws
0072    //!interprocess_exception.
0073    named_sharable_mutex(open_only_t, const char *name);
0074 
0075    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0076 
0077    //!Creates a global sharable mutex with a name.
0078    //!If the sharable mutex can't be created throws interprocess_exception
0079    //! 
0080    //!Note: This function is only available on operating systems with
0081    //!      native wchar_t APIs (e.g. Windows).
0082    named_sharable_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
0083 
0084    //!Opens or creates a global sharable mutex with a name.
0085    //!If the sharable mutex is created, this call is equivalent to
0086    //!named_sharable_mutex(create_only_t, ...)
0087    //!If the sharable mutex is already created, this call is equivalent to
0088    //!named_sharable_mutex(open_only_t, ... ).
0089    //! 
0090    //!Note: This function is only available on operating systems with
0091    //!      native wchar_t APIs (e.g. Windows).
0092    named_sharable_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
0093 
0094    //!Opens a global sharable mutex with a name if that sharable mutex
0095    //!is previously.
0096    //!created. If it is not previously created this function throws
0097    //!interprocess_exception.
0098    //! 
0099    //!Note: This function is only available on operating systems with
0100    //!      native wchar_t APIs (e.g. Windows).
0101    named_sharable_mutex(open_only_t, const wchar_t *name);
0102 
0103    #endif   //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0104 
0105    //!Destroys *this and indicates that the calling process is finished using
0106    //!the resource. The destructor function will deallocate
0107    //!any system resources allocated by the system for use by this process for
0108    //!this resource. The resource can still be opened again calling
0109    //!the open constructor overload. To erase the resource from the system
0110    //!use remove().
0111    ~named_sharable_mutex();
0112 
0113    //Exclusive locking
0114 
0115    //!Requires: The calling thread does not own the mutex.
0116    //!
0117    //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
0118    //!   and if another thread has exclusive or sharable ownership of
0119    //!   the mutex, it waits until it can obtain the ownership.
0120    //!Throws: interprocess_exception on error.
0121    //! 
0122    //!Note: A program may deadlock if the thread that has ownership calls 
0123    //!   this function. If the implementation can detect the deadlock,
0124    //!   an exception could be thrown.
0125    void lock();
0126 
0127    //!Requires: The calling thread does not own the mutex.
0128    //!
0129    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
0130    //!   without waiting. If no other thread has exclusive or sharable
0131    //!   ownership of the mutex this succeeds.
0132    //!Returns: If it can acquire exclusive ownership immediately returns true.
0133    //!   If it has to wait, returns false.
0134    //!Throws: interprocess_exception on error.
0135    //! 
0136    //!Note: A program may deadlock if the thread that has ownership calls 
0137    //!   this function. If the implementation can detect the deadlock,
0138    //!   an exception could be thrown.
0139    bool try_lock();
0140 
0141    //!Requires: The calling thread does not own the mutex.
0142    //!
0143    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
0144    //!   waiting if necessary until no other thread has exclusive, or sharable
0145    //!   ownership of the mutex or abs_time is reached.
0146    //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
0147    //!Throws: interprocess_exception on error.
0148    //! 
0149    //!Note: A program may deadlock if the thread that has ownership calls 
0150    //!   this function. If the implementation can detect the deadlock,
0151    //!   an exception could be thrown.
0152    template<class TimePoint>
0153    bool timed_lock(const TimePoint &abs_time);
0154 
0155    //!Same as `timed_lock`, but this function is modeled after the
0156    //!standard library interface.
0157    template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0158    {  return this->timed_lock(abs_time);  }
0159 
0160    //!Same as `timed_lock`, but this function is modeled after the
0161    //!standard library interface.
0162    template<class Duration>  bool try_lock_for(const Duration &dur)
0163    {  return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0164 
0165    //!Precondition: The thread must have exclusive ownership of the mutex.
0166    //!Effects: The calling thread releases the exclusive ownership of the mutex.
0167    //!Throws: An exception derived from interprocess_exception on error.
0168    void unlock();
0169 
0170    //Sharable locking
0171 
0172    //!Requires: The calling thread does not own the mutex.
0173    //!
0174    //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
0175    //!   and if another thread has exclusive ownership of the mutex,
0176    //!   waits until it can obtain the ownership.
0177    //!Throws: interprocess_exception on error.
0178    //!
0179    //!Note: A program may deadlock if the thread that has ownership calls 
0180    //!   this function. If the implementation can detect the deadlock,
0181    //!   an exception could be thrown
0182    void lock_sharable();
0183 
0184    //!Same as `lock_sharable` but with a std-compatible interface
0185    //! 
0186    void lock_shared()
0187    {  this->lock_sharable();  }
0188 
0189    //!Requires: The calling thread does not own the mutex.
0190    //!
0191    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
0192    //!   without waiting. If no other thread has exclusive ownership
0193    //!   of the mutex this succeeds.
0194    //!Returns: If it can acquire sharable ownership immediately returns true. If it
0195    //!   has to wait, returns false.
0196    //!Throws: interprocess_exception on error.
0197    //!
0198    //!Note: A program may deadlock if the thread that has ownership calls 
0199    //!   this function. If the implementation can detect the deadlock,
0200    //!   an exception could be thrown
0201    bool try_lock_sharable();
0202 
0203    //!Same as `try_lock_sharable` but with a std-compatible interface
0204    //! 
0205    bool try_lock_shared()
0206    {  return this->try_lock_sharable();  }
0207 
0208    //!Requires: The calling thread does not own the mutex.
0209    //!
0210    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
0211    //!   waiting if necessary until no other thread has exclusive
0212    //!   ownership of the mutex or abs_time is reached.
0213    //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
0214    //!Throws: interprocess_exception on error.
0215    //!
0216    //!Note: A program may deadlock if the thread that has ownership calls 
0217    //!   this function. If the implementation can detect the deadlock,
0218    //!   an exception could be thrown
0219    template<class TimePoint>
0220    bool timed_lock_sharable(const TimePoint &abs_time);
0221 
0222    //!Same as `timed_lock_sharable`, but this function is modeled after the
0223    //!standard library interface.
0224    template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
0225    {  return this->timed_lock_sharable(abs_time);  }
0226 
0227    //!Same as `timed_lock_sharable`, but this function is modeled after the
0228    //!standard library interface.
0229    template<class Duration>  bool try_lock_shared_for(const Duration &dur)
0230    {  return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
0231 
0232    //!Precondition: The thread must have sharable ownership of the mutex.
0233    //!Effects: The calling thread releases the sharable ownership of the mutex.
0234    //!Throws: An exception derived from interprocess_exception on error.
0235    void unlock_sharable();
0236 
0237    //!Same as `unlock_sharable` but with a std-compatible interface
0238    //! 
0239    void unlock_shared()
0240    {  this->unlock_sharable();  }
0241 
0242    //!Erases a named sharable mutex from the system.
0243    //!Returns false on error. Never throws.
0244    static bool remove(const char *name);
0245 
0246    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0247 
0248    //!Erases a named sharable mutex from the system.
0249    //!Returns false on error. Never throws.
0250    static bool remove(const wchar_t *name);
0251 
0252    #endif
0253 
0254    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0255    private:
0256    friend class ipcdetail::interprocess_tester;
0257    void dont_close_on_destruction();
0258 
0259    interprocess_sharable_mutex *mutex() const
0260    {  return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
0261 
0262    typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
0263    open_create_impl_t m_shmem;
0264    typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
0265    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0266 };
0267 
0268 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0269 
0270 inline named_sharable_mutex::~named_sharable_mutex()
0271 {}
0272 
0273 inline named_sharable_mutex::named_sharable_mutex
0274    (create_only_t, const char *name, const permissions &perm)
0275    :  m_shmem  (create_only
0276                ,name
0277                ,sizeof(interprocess_sharable_mutex) +
0278                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0279                ,read_write
0280                ,0
0281                ,construct_func_t(ipcdetail::DoCreate)
0282                ,perm)
0283 {}
0284 
0285 inline named_sharable_mutex::named_sharable_mutex
0286    (open_or_create_t, const char *name, const permissions &perm)
0287    :  m_shmem  (open_or_create
0288                ,name
0289                ,sizeof(interprocess_sharable_mutex) +
0290                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0291                ,read_write
0292                ,0
0293                ,construct_func_t(ipcdetail::DoOpenOrCreate)
0294                ,perm)
0295 {}
0296 
0297 inline named_sharable_mutex::named_sharable_mutex
0298    (open_only_t, const char *name)
0299    :  m_shmem  (open_only
0300                ,name
0301                ,read_write
0302                ,0
0303                ,construct_func_t(ipcdetail::DoOpen))
0304 {}
0305 
0306 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0307 
0308 inline named_sharable_mutex::named_sharable_mutex
0309    (create_only_t, const wchar_t *name, const permissions &perm)
0310    :  m_shmem  (create_only
0311                ,name
0312                ,sizeof(interprocess_sharable_mutex) +
0313                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0314                ,read_write
0315                ,0
0316                ,construct_func_t(ipcdetail::DoCreate)
0317                ,perm)
0318 {}
0319 
0320 inline named_sharable_mutex::named_sharable_mutex
0321    (open_or_create_t, const wchar_t *name, const permissions &perm)
0322    :  m_shmem  (open_or_create
0323                ,name
0324                ,sizeof(interprocess_sharable_mutex) +
0325                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0326                ,read_write
0327                ,0
0328                ,construct_func_t(ipcdetail::DoOpenOrCreate)
0329                ,perm)
0330 {}
0331 
0332 inline named_sharable_mutex::named_sharable_mutex
0333    (open_only_t, const wchar_t *name)
0334    :  m_shmem  (open_only
0335                ,name
0336                ,read_write
0337                ,0
0338                ,construct_func_t(ipcdetail::DoOpen))
0339 {}
0340 
0341 #endif
0342 
0343 inline void named_sharable_mutex::dont_close_on_destruction()
0344 {  ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem);  }
0345 
0346 inline void named_sharable_mutex::lock()
0347 {  this->mutex()->lock();  }
0348 
0349 inline void named_sharable_mutex::unlock()
0350 {  this->mutex()->unlock();  }
0351 
0352 inline bool named_sharable_mutex::try_lock()
0353 {  return this->mutex()->try_lock();  }
0354 
0355 template<class TimePoint>
0356 inline bool named_sharable_mutex::timed_lock
0357    (const TimePoint &abs_time)
0358 {  return this->mutex()->timed_lock(abs_time);  }
0359 
0360 inline void named_sharable_mutex::lock_sharable()
0361 {  this->mutex()->lock_sharable();  }
0362 
0363 inline void named_sharable_mutex::unlock_sharable()
0364 {  this->mutex()->unlock_sharable();  }
0365 
0366 inline bool named_sharable_mutex::try_lock_sharable()
0367 {  return this->mutex()->try_lock_sharable();  }
0368 
0369 template<class TimePoint>
0370 inline bool named_sharable_mutex::timed_lock_sharable
0371    (const TimePoint &abs_time)
0372 {  return this->mutex()->timed_lock_sharable(abs_time);  }
0373 
0374 inline bool named_sharable_mutex::remove(const char *name)
0375 {  return shared_memory_object::remove(name); }
0376 
0377 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0378 
0379 inline bool named_sharable_mutex::remove(const wchar_t *name)
0380 {  return shared_memory_object::remove(name); }
0381 
0382 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0383 
0384 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0385 
0386 }  //namespace interprocess {
0387 }  //namespace boost {
0388 
0389 #include <boost/interprocess/detail/config_end.hpp>
0390 
0391 #endif   //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP