Back to home page

EIC code displayed by LXR

 
 

    


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

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