Back to home page

EIC code displayed by LXR

 
 

    


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

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