Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 08:17:16

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