123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- #ifndef BOOST_INTERPROCESS_FILE_LOCK_HPP
- #define BOOST_INTERPROCESS_FILE_LOCK_HPP
- #ifndef BOOST_CONFIG_HPP
- # include <boost/config.hpp>
- #endif
- #
- #if defined(BOOST_HAS_PRAGMA_ONCE)
- # pragma once
- #endif
- #include <boost/interprocess/detail/config_begin.hpp>
- #include <boost/interprocess/detail/workaround.hpp>
- #include <boost/interprocess/exceptions.hpp>
- #include <boost/interprocess/timed_utils.hpp>
- #include <boost/interprocess/detail/os_file_functions.hpp>
- #include <boost/interprocess/detail/os_thread_functions.hpp>
- #include <boost/interprocess/sync/detail/common_algorithms.hpp>
- #include <boost/interprocess/sync/detail/locks.hpp>
- #include <boost/move/utility_core.hpp>
- namespace boost {
- namespace interprocess {
- class file_lock
- {
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
-
- BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock)
- #endif
- public:
-
-
- file_lock() BOOST_NOEXCEPT
- : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
- {}
-
-
- file_lock(const char *name);
- #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
-
-
-
-
-
- file_lock(const wchar_t *name);
- #endif
-
-
-
- file_lock(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
- : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
- { this->swap(moved); }
-
-
-
- file_lock &operator=(BOOST_RV_REF(file_lock) moved) BOOST_NOEXCEPT
- {
- file_lock tmp(boost::move(moved));
- this->swap(tmp);
- return *this;
- }
-
- ~file_lock();
-
-
- void swap(file_lock &other) BOOST_NOEXCEPT
- {
- file_handle_t tmp = m_file_hnd;
- m_file_hnd = other.m_file_hnd;
- other.m_file_hnd = tmp;
- }
-
-
-
-
-
-
-
-
-
-
-
- void lock();
-
-
-
-
-
-
-
-
-
-
-
-
- bool try_lock();
-
-
-
-
-
-
-
-
-
-
-
- template<class TimePoint>
- bool timed_lock(const TimePoint &abs_time);
-
-
- template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
- { return this->timed_lock(abs_time); }
-
-
- template<class Duration> bool try_lock_for(const Duration &dur)
- { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
-
-
-
- void unlock();
-
-
-
-
-
-
-
-
-
-
-
- void lock_sharable();
-
-
- void lock_shared()
- { this->lock_sharable(); }
-
-
-
-
-
-
- bool try_lock_sharable();
-
-
- bool try_lock_shared()
- { return this->try_lock_sharable(); }
-
-
-
-
-
- template<class TimePoint>
- bool timed_lock_sharable(const TimePoint &abs_time);
-
-
- template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
- { return this->timed_lock_sharable(abs_time); }
-
-
- template<class Duration> bool try_lock_shared_for(const Duration &dur)
- { return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
-
-
-
- void unlock_sharable();
-
-
- void unlock_shared()
- { this->unlock_sharable(); }
- #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- private:
- file_handle_t m_file_hnd;
- #endif
- };
- inline file_lock::file_lock(const char *name)
- {
- m_file_hnd = ipcdetail::open_existing_file(name, read_write);
- if(m_file_hnd == ipcdetail::invalid_file()){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- }
- #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
- inline file_lock::file_lock(const wchar_t *name)
- {
- m_file_hnd = ipcdetail::open_existing_file(name, read_write);
- if(m_file_hnd == ipcdetail::invalid_file()){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- }
- #endif
- inline file_lock::~file_lock()
- {
- if(m_file_hnd != ipcdetail::invalid_file()){
- ipcdetail::close_file(m_file_hnd);
- m_file_hnd = ipcdetail::invalid_file();
- }
- }
- inline void file_lock::lock()
- {
- if(!ipcdetail::acquire_file_lock(m_file_hnd)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- }
- inline bool file_lock::try_lock()
- {
- bool result;
- if(!ipcdetail::try_acquire_file_lock(m_file_hnd, result)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- return result;
- }
- template<class TimePoint>
- inline bool file_lock::timed_lock(const TimePoint &abs_time)
- { return ipcdetail::try_based_timed_lock(*this, abs_time); }
- inline void file_lock::unlock()
- {
- if(!ipcdetail::release_file_lock(m_file_hnd)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- }
- inline void file_lock::lock_sharable()
- {
- if(!ipcdetail::acquire_file_lock_sharable(m_file_hnd)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- }
- inline bool file_lock::try_lock_sharable()
- {
- bool result;
- if(!ipcdetail::try_acquire_file_lock_sharable(m_file_hnd, result)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- return result;
- }
- template<class TimePoint>
- inline bool file_lock::timed_lock_sharable(const TimePoint &abs_time)
- {
- ipcdetail::lock_to_sharable<file_lock> lsh(*this);
- return ipcdetail::try_based_timed_lock(lsh, abs_time);
- }
- inline void file_lock::unlock_sharable()
- {
- if(!ipcdetail::release_file_lock_sharable(m_file_hnd)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- }
- }
- }
- #include <boost/interprocess/detail/config_end.hpp>
- #endif
|