Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 08:07:32

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_SHM_NAMED_MUTEX_HPP
0012 #define BOOST_INTERPROCESS_SHM_NAMED_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/detail/interprocess_tester.hpp>
0028 #include <boost/interprocess/permissions.hpp>
0029 
0030 #include <boost/interprocess/shared_memory_object.hpp>
0031 #include <boost/interprocess/sync/interprocess_mutex.hpp>
0032 #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
0033 #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
0034 #include <boost/interprocess/timed_utils.hpp>
0035 
0036 //!\file
0037 //!Describes a named mutex class for inter-process synchronization
0038 
0039 namespace boost {
0040 namespace interprocess {
0041 namespace ipcdetail {
0042 
0043 class named_condition;
0044 
0045 //!A 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 mutex.
0048 class shm_named_mutex
0049 {
0050    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0051 
0052    //Non-copyable
0053    shm_named_mutex();
0054    shm_named_mutex(const shm_named_mutex &);
0055    shm_named_mutex &operator=(const shm_named_mutex &);
0056    friend class named_condition;
0057    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0058 
0059    public:
0060    //!Creates a global interprocess_mutex with a name.
0061    //!Throws interprocess_exception on error.
0062    shm_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
0063 
0064    //!Opens or creates a global mutex with a name.
0065    //!If the mutex is created, this call is equivalent to
0066    //!shm_named_mutex(create_only_t, ... )
0067    //!If the mutex is already created, this call is equivalent
0068    //!shm_named_mutex(open_only_t, ... )
0069    //!Does not throw
0070    shm_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
0071 
0072    //!Opens a global mutex with a name if that mutex is previously
0073    //!created. If it is not previously created this function throws
0074    //!interprocess_exception.
0075    shm_named_mutex(open_only_t, const char *name);
0076 
0077    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0078 
0079    //!Creates a global interprocess_mutex with a name.
0080    //!Throws interprocess_exception on error.
0081    shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
0082 
0083    //!Opens or creates a global mutex with a name.
0084    //!If the mutex is created, this call is equivalent to
0085    //!shm_named_mutex(create_only_t, ... )
0086    //!If the mutex is already created, this call is equivalent
0087    //!shm_named_mutex(open_only_t, ... )
0088    //!Does not throw
0089    shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
0090 
0091    //!Opens a global mutex with a name if that mutex is previously
0092    //!created. If it is not previously created this function throws
0093    //!interprocess_exception.
0094    shm_named_mutex(open_only_t, const wchar_t *name);
0095 
0096    #endif   //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0097 
0098    //!Destroys *this and indicates that the calling process is finished using
0099    //!the resource. The destructor function will deallocate
0100    //!any system resources allocated by the system for use by this process for
0101    //!this resource. The resource can still be opened again calling
0102    //!the open constructor overload. To erase the resource from the system
0103    //!use remove().
0104    ~shm_named_mutex();
0105 
0106    //!Unlocks a previously locked
0107    //!interprocess_mutex.
0108    void unlock();
0109 
0110    //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked.
0111    //!Throws interprocess_exception if a severe error is found
0112    void lock();
0113 
0114    //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
0115    //!is already locked, returns true when success.
0116    //!Throws interprocess_exception if a severe error is found
0117    bool try_lock();
0118 
0119    //!Tries to lock the interprocess_mutex until time abs_time,
0120    //!Returns false when timeout expires, returns true when locks.
0121    //!Throws interprocess_exception if a severe error is found
0122    template<class TimePoint>
0123    bool timed_lock(const TimePoint &abs_time);
0124 
0125    template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0126    {  return this->timed_lock(abs_time);  }
0127 
0128    template<class Duration>  bool try_lock_for(const Duration &dur)
0129    {  return this->timed_lock(duration_to_ustime(dur)); }
0130 
0131    //!Erases a named mutex from the system.
0132    //!Returns false on error. Never throws.
0133    static bool remove(const char *name);
0134 
0135    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0136 
0137    //!Erases a named mutex from the system.
0138    //!Returns false on error. Never throws.
0139    static bool remove(const wchar_t *name);
0140 
0141    #endif
0142 
0143    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0144    typedef interprocess_mutex internal_mutex_type;
0145    interprocess_mutex &internal_mutex()
0146    {  return *static_cast<interprocess_mutex*>(m_shmem.get_user_address()); }
0147 
0148    private:
0149    friend class ipcdetail::interprocess_tester;
0150    void dont_close_on_destruction();
0151    typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
0152    open_create_impl_t m_shmem;
0153    typedef ipcdetail::named_creation_functor<interprocess_mutex> construct_func_t;
0154    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0155 };
0156 
0157 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0158 
0159 inline void shm_named_mutex::dont_close_on_destruction()
0160 {  ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem);  }
0161 
0162 inline shm_named_mutex::~shm_named_mutex()
0163 {}
0164 
0165 inline shm_named_mutex::shm_named_mutex(create_only_t, const char *name, const permissions &perm)
0166    :  m_shmem  (create_only_t()
0167                ,name
0168                ,sizeof(interprocess_mutex) +
0169                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0170                ,read_write
0171                ,0
0172                ,construct_func_t(ipcdetail::DoCreate)
0173                ,perm)
0174 {}
0175 
0176 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const char *name, const permissions &perm)
0177    :  m_shmem  (open_or_create_t()
0178                ,name
0179                ,sizeof(interprocess_mutex) +
0180                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0181                ,read_write
0182                ,0
0183                ,construct_func_t(ipcdetail::DoOpenOrCreate)
0184                ,perm)
0185 {}
0186 
0187 inline shm_named_mutex::shm_named_mutex(open_only_t, const char *name)
0188    :  m_shmem  (open_only_t()
0189                ,name
0190                ,read_write
0191                ,0
0192                ,construct_func_t(ipcdetail::DoOpen))
0193 {}
0194 
0195 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0196 
0197 inline shm_named_mutex::shm_named_mutex(create_only_t, const wchar_t *name, const permissions &perm)
0198    :  m_shmem  (create_only_t()
0199                ,name
0200                ,sizeof(interprocess_mutex) +
0201                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0202                ,read_write
0203                ,0
0204                ,construct_func_t(ipcdetail::DoCreate)
0205                ,perm)
0206 {}
0207 
0208 inline shm_named_mutex::shm_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm)
0209    :  m_shmem  (open_or_create_t()
0210                ,name
0211                ,sizeof(interprocess_mutex) +
0212                   open_create_impl_t::ManagedOpenOrCreateUserOffset
0213                ,read_write
0214                ,0
0215                ,construct_func_t(ipcdetail::DoOpenOrCreate)
0216                ,perm)
0217 {}
0218 
0219 inline shm_named_mutex::shm_named_mutex(open_only_t, const wchar_t *name)
0220    :  m_shmem  (open_only_t()
0221                ,name
0222                ,read_write
0223                ,0
0224                ,construct_func_t(ipcdetail::DoOpen))
0225 {}
0226 
0227 #endif   //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0228 
0229 inline void shm_named_mutex::lock()
0230 {  this->internal_mutex().lock();  }
0231 
0232 inline void shm_named_mutex::unlock()
0233 {  this->internal_mutex().unlock();  }
0234 
0235 inline bool shm_named_mutex::try_lock()
0236 {  return this->internal_mutex().try_lock();  }
0237 
0238 template<class TimePoint>
0239 inline bool shm_named_mutex::timed_lock(const TimePoint &abs_time)
0240 {  return this->internal_mutex().timed_lock(abs_time);   }
0241 
0242 inline bool shm_named_mutex::remove(const char *name)
0243 {  return shared_memory_object::remove(name); }
0244 
0245 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0246 
0247 inline bool shm_named_mutex::remove(const wchar_t *name)
0248 {  return shared_memory_object::remove(name); }
0249 
0250 #endif   //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0251 
0252 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0253 
0254 }  //namespace ipcdetail {
0255 }  //namespace interprocess {
0256 }  //namespace boost {
0257 
0258 #include <boost/interprocess/detail/config_end.hpp>
0259 
0260 #endif   //BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP