Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/interprocess/sync/named_sharable_mutex.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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