Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:33

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