Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-12 08:17:12

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