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