Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:31

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