Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 08:05:23

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_FILE_LOCK_HPP
0012 #define BOOST_INTERPROCESS_FILE_LOCK_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/exceptions.hpp>
0026 #include <boost/interprocess/timed_utils.hpp>
0027 #include <boost/interprocess/detail/os_file_functions.hpp>
0028 #include <boost/interprocess/detail/os_thread_functions.hpp>
0029 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0030 #include <boost/interprocess/sync/detail/locks.hpp>
0031 #include <boost/move/utility_core.hpp>
0032 
0033 //!\file
0034 //!Describes a class that wraps file locking capabilities.
0035 
0036 namespace boost {
0037 namespace interprocess {
0038 
0039 
0040 //!A file lock, is a mutual exclusion utility similar to a mutex using a
0041 //!file. A file lock has sharable and exclusive locking capabilities and
0042 //!can be used with scoped_lock and sharable_lock classes.
0043 //!A file lock can't guarantee synchronization between threads of the same
0044 //!process so just use file locks to synchronize threads from different processes.
0045 class file_lock
0046 {
0047    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0048    //Non-copyable
0049    BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock)
0050    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0051 
0052    public:
0053    //!Constructs an empty file mapping.
0054    //!Does not throw
0055    file_lock() BOOST_NOEXCEPT
0056       :  m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
0057    {}
0058 
0059    //!Opens a file lock. Throws interprocess_exception if the file does not
0060    //!exist or there are no operating system resources.
0061    file_lock(const char *name);
0062 
0063    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0064    //!Opens a file lock. Throws interprocess_exception if the file does not
0065    //!exist or there are no operating system resources.
0066    //! 
0067    //!Note: This function is only available on operating systems with
0068    //!      native wchar_t APIs (e.g. Windows).
0069    file_lock(const wchar_t *name);
0070    #endif
0071 
0072    //!Moves the ownership of "moved"'s file mapping object to *this.
0073    //!After the call, "moved" does not represent any file mapping object.
0074    //!Does not throw
0075    file_lock(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
0076       :  m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
0077    {  this->swap(moved);   }
0078 
0079    //!Moves the ownership of "moved"'s file mapping to *this.
0080    //!After the call, "moved" does not represent any file mapping.
0081    //!Does not throw
0082    file_lock &operator=(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
0083    {
0084       file_lock tmp(boost::move(moved));
0085       this->swap(tmp);
0086       return *this;
0087    }
0088 
0089    //!Closes a file lock. Does not throw.
0090    ~file_lock();
0091 
0092    //!Swaps two file_locks.
0093    //!Does not throw.
0094    void swap(file_lock &other) BOOST_NOEXCEPT
0095    {
0096       file_handle_t tmp = m_file_hnd;
0097       m_file_hnd = other.m_file_hnd;
0098       other.m_file_hnd = tmp;
0099    }
0100 
0101    //Exclusive locking
0102 
0103    //!Requires: The calling thread does not own the mutex.
0104    //!
0105    //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
0106    //!   and if another thread has exclusive, or sharable ownership of
0107    //!   the mutex, it waits until it can obtain the ownership.
0108    //!Throws: interprocess_exception on error.
0109    //!
0110    //!Note: A program may deadlock if the thread that has ownership calls 
0111    //!   this function. If the implementation can detect the deadlock,
0112    //!   an exception could be thrown.
0113    void lock();
0114 
0115    //!Requires: The calling thread does not own the mutex.
0116    //!
0117    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
0118    //!   without waiting. If no other thread has exclusive, or sharable
0119    //!   ownership of the mutex this succeeds.
0120    //!Returns: If it can acquire exclusive ownership immediately returns true.
0121    //!   If it has to wait, returns false.
0122    //!Throws: interprocess_exception on error.
0123    //! 
0124    //!Note: A program may deadlock if the thread that has ownership calls 
0125    //!   this function. If the implementation can detect the deadlock,
0126    //!   an exception could be thrown.
0127    bool try_lock();
0128 
0129    //!Requires: The calling thread does not own the mutex.
0130    //!
0131    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
0132    //!   waiting if necessary until no other thread has exclusive, or sharable
0133    //!   ownership of the mutex or abs_time is reached.
0134    //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
0135    //!Throws: interprocess_exception on error.
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    template<class TimePoint>
0141    bool timed_lock(const TimePoint &abs_time);
0142 
0143    //!Same as `timed_lock`, but this function is modeled after the
0144    //!standard library interface.
0145    template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0146    {  return this->timed_lock(abs_time);  }
0147 
0148    //!Same as `timed_lock`, but this function is modeled after the
0149    //!standard library interface.
0150    template<class Duration>  bool try_lock_for(const Duration &dur)
0151    {  return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0152 
0153    //!Precondition: The thread must have exclusive ownership of the mutex.
0154    //!Effects: The calling thread releases the exclusive ownership of the mutex.
0155    //!Throws: An exception derived from interprocess_exception on error.
0156    void unlock();
0157 
0158    //Sharable locking
0159 
0160    //!Requires: The calling thread does not own the mutex.
0161    //!
0162    //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
0163    //!   and if another thread has exclusive ownership of the mutex, waits until
0164    //!   it can obtain the ownership.
0165    //!Throws: interprocess_exception on error.
0166    //!
0167    //!Note: A program may deadlock if the thread that owns a mutex object calls 
0168    //!   this function. If the implementation can detect the deadlock,
0169    //!   an exception could be thrown.
0170    void lock_sharable();
0171 
0172    //!Same as `lock_sharable` but with a std-compatible interface
0173    //! 
0174    void lock_shared()
0175    {  this->lock_sharable();  }
0176 
0177    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
0178    //!   without waiting. If no other thread has exclusive ownership of the
0179    //!   mutex this succeeds.
0180    //!Returns: If it can acquire sharable ownership immediately returns true. If it
0181    //!   has to wait, returns false.
0182    //!Throws: interprocess_exception on error.
0183    bool try_lock_sharable();
0184 
0185    //!Same as `try_lock_sharable` but with a std-compatible interface
0186    //! 
0187    bool try_lock_shared()
0188    {  return this->try_lock_sharable();  }
0189 
0190    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
0191    //!   waiting if necessary until no other thread has exclusive ownership of
0192    //!   the mutex or abs_time is reached.
0193    //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
0194    //!Throws: interprocess_exception on error.
0195    template<class TimePoint>
0196    bool timed_lock_sharable(const TimePoint &abs_time);
0197 
0198    //!Same as `timed_lock_sharable`, but this function is modeled after the
0199    //!standard library interface.
0200    template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
0201    {  return this->timed_lock_sharable(abs_time);  }
0202 
0203    //!Same as `timed_lock_sharable`, but this function is modeled after the
0204    //!standard library interface.
0205    template<class Duration>  bool try_lock_shared_for(const Duration &dur)
0206    {  return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
0207 
0208    //!Precondition: The thread must have sharable ownership of the mutex.
0209    //!Effects: The calling thread releases the sharable ownership of the mutex.
0210    //!Throws: An exception derived from interprocess_exception on error.
0211    void unlock_sharable();
0212 
0213    //!Same as `unlock_sharable` but with a std-compatible interface
0214    //! 
0215    void unlock_shared()
0216    {  this->unlock_sharable();  }
0217 
0218    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0219    private:
0220    file_handle_t m_file_hnd;
0221 
0222    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0223 };
0224 
0225 inline file_lock::file_lock(const char *name)
0226 {
0227    m_file_hnd = ipcdetail::open_existing_file(name, read_write);
0228 
0229    if(m_file_hnd == ipcdetail::invalid_file()){
0230       error_info err(system_error_code());
0231       throw interprocess_exception(err);
0232    }
0233 }
0234 
0235 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0236 
0237 inline file_lock::file_lock(const wchar_t *name)
0238 {
0239    m_file_hnd = ipcdetail::open_existing_file(name, read_write);
0240 
0241    if(m_file_hnd == ipcdetail::invalid_file()){
0242       error_info err(system_error_code());
0243       throw interprocess_exception(err);
0244    }
0245 }
0246 
0247 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0248 
0249 inline file_lock::~file_lock()
0250 {
0251    if(m_file_hnd != ipcdetail::invalid_file()){
0252       ipcdetail::close_file(m_file_hnd);
0253       m_file_hnd = ipcdetail::invalid_file();
0254    }
0255 }
0256 
0257 inline void file_lock::lock()
0258 {
0259    if(!ipcdetail::acquire_file_lock(m_file_hnd)){
0260       error_info err(system_error_code());
0261       throw interprocess_exception(err);
0262    }
0263 }
0264 
0265 inline bool file_lock::try_lock()
0266 {
0267    bool result;
0268    if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
0269       error_info err(system_error_code());
0270       throw interprocess_exception(err);
0271    }
0272    return result;
0273 }
0274 
0275 template<class TimePoint>
0276 inline bool file_lock::timed_lock(const TimePoint &abs_time)
0277 {  return ipcdetail::try_based_timed_lock(*this, abs_time);   }
0278 
0279 inline void file_lock::unlock()
0280 {
0281    if(!ipcdetail::release_file_lock(m_file_hnd)){
0282       error_info err(system_error_code());
0283       throw interprocess_exception(err);
0284    }
0285 }
0286 
0287 inline void file_lock::lock_sharable()
0288 {
0289    if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
0290       error_info err(system_error_code());
0291       throw interprocess_exception(err);
0292    }
0293 }
0294 
0295 inline bool file_lock::try_lock_sharable()
0296 {
0297    bool result;
0298    if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
0299       error_info err(system_error_code());
0300       throw interprocess_exception(err);
0301    }
0302    return result;
0303 }
0304 
0305 template<class TimePoint>
0306 inline bool file_lock::timed_lock_sharable(const TimePoint &abs_time)
0307 {
0308    ipcdetail::lock_to_sharable<file_lock> lsh(*this);
0309    return ipcdetail::try_based_timed_lock(lsh, abs_time);
0310 }
0311 
0312 inline void file_lock::unlock_sharable()
0313 {
0314    if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
0315       error_info err(system_error_code());
0316       throw interprocess_exception(err);
0317    }
0318 }
0319 
0320 }  //namespace interprocess {
0321 }  //namespace boost {
0322 
0323 #include <boost/interprocess/detail/config_end.hpp>
0324 
0325 #endif   //BOOST_INTERPROCESS_FILE_LOCK_HPP