Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-05 08:21:09

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