123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- #ifndef BOOST_INTERPROCESS_FILE_LOCK_HPP
- #define BOOST_INTERPROCESS_FILE_LOCK_HPP
- #if (defined _MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/interprocess/detail/config_begin.hpp>
- #include <boost/interprocess/detail/workaround.hpp>
- #include <boost/interprocess/exceptions.hpp>
- #include <boost/interprocess/detail/os_file_functions.hpp>
- #include <boost/interprocess/detail/os_thread_functions.hpp>
- #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
- #include <boost/move/move.hpp>
- namespace boost {
- namespace interprocess {
- class file_lock
- {
-
-
- BOOST_MOVABLE_BUT_NOT_COPYABLE(file_lock)
-
- public:
-
-
- file_lock()
- : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
- {}
-
-
- file_lock(const char *name);
-
-
-
- file_lock(BOOST_RV_REF(file_lock) moved)
- : m_file_hnd(file_handle_t(ipcdetail::invalid_file()))
- { this->swap(moved); }
-
-
-
- file_lock &operator=(BOOST_RV_REF(file_lock) moved)
- {
- file_lock tmp(boost::move(moved));
- this->swap(tmp);
- return *this;
- }
-
- ~file_lock();
-
-
- void swap(file_lock &other)
- {
- file_handle_t tmp = m_file_hnd;
- m_file_hnd = other.m_file_hnd;
- other.m_file_hnd = tmp;
- }
-
-
-
-
-
- void lock();
-
-
-
-
-
-
- bool try_lock();
-
-
-
-
-
- bool timed_lock(const boost::posix_time::ptime &abs_time);
-
-
-
- void unlock();
-
-
-
-
-
- void lock_sharable();
-
-
-
-
-
-
- bool try_lock_sharable();
-
-
-
-
-
- bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
-
-
-
- void unlock_sharable();
-
- private:
- file_handle_t m_file_hnd;
- bool timed_acquire_file_lock
- (file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time)
- {
-
- boost::posix_time::ptime now = microsec_clock::universal_time();
- using namespace boost::detail;
- if(now >= abs_time) return false;
- do{
- if(!ipcdetail::try_acquire_file_lock(hnd, acquired))
- return false;
- if(acquired)
- return true;
- else{
- now = microsec_clock::universal_time();
- if(now >= abs_time){
- acquired = false;
- return true;
- }
-
- ipcdetail::thread_yield();
- }
- }while (true);
- }
- bool timed_acquire_file_lock_sharable
- (file_handle_t hnd, bool &acquired, const boost::posix_time::ptime &abs_time)
- {
-
- boost::posix_time::ptime now = microsec_clock::universal_time();
- using namespace boost::detail;
- if(now >= abs_time) return false;
- do{
- if(!ipcdetail::try_acquire_file_lock_sharable(hnd, acquired))
- return false;
- if(acquired)
- return true;
- else{
- now = microsec_clock::universal_time();
- if(now >= abs_time){
- acquired = false;
- return true;
- }
-
- ipcdetail::thread_yield();
- }
- }while (true);
- }
-
- };
- 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);
- }
- }
- 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;
- }
- inline bool file_lock::timed_lock(const boost::posix_time::ptime &abs_time)
- {
- if(abs_time == boost::posix_time::pos_infin){
- this->lock();
- return true;
- }
- bool result;
- if(!this->timed_acquire_file_lock(m_file_hnd, result, abs_time)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- return result;
- }
- 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;
- }
- inline bool file_lock::timed_lock_sharable(const boost::posix_time::ptime &abs_time)
- {
- if(abs_time == boost::posix_time::pos_infin){
- this->lock_sharable();
- return true;
- }
- bool result;
- if(!this->timed_acquire_file_lock_sharable(m_file_hnd, result, abs_time)){
- error_info err(system_error_code());
- throw interprocess_exception(err);
- }
- return result;
- }
- 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
|