interprocess_upgradable_mutex.hpp
来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 659 行 · 第 1/2 页
HPP
659 行
//The exclusive lock must block in the first gate //if an exclusive or upgradable lock has been acquired while (this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in){ if(!this->m_first_gate.timed_wait(lock, abs_time)) return !(this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in); } //Mark that exclusive lock has been acquired this->m_ctrl.exclusive_in = 1; //Prepare rollback exclusive_rollback rollback(this->m_ctrl, this->m_first_gate); //Now wait until all readers are gone while (this->m_ctrl.num_upr_shar){ if(!this->m_second_gate.timed_wait(lock, abs_time)){ return !(this->m_ctrl.num_upr_shar); } } rollback.release(); return true;}inline void interprocess_upgradable_mutex::unlock(){ scoped_lock_t lock(m_mut); this->m_ctrl.exclusive_in = 0; this->m_first_gate.notify_all();}//Upgradable lockinginline void interprocess_upgradable_mutex::lock_upgradable(){ scoped_lock_t lock(m_mut); //The upgradable lock must block in the first gate //if an exclusive or upgradable lock has been acquired //or there are too many sharable locks while(this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in || this->m_ctrl.num_upr_shar == constants::max_readers){ this->m_first_gate.wait(lock); } //Mark that upgradable lock has been acquired //And add upgradable to the sharable count this->m_ctrl.upgradable_in = 1; ++this->m_ctrl.num_upr_shar;}inline bool interprocess_upgradable_mutex::try_lock_upgradable(){ scoped_lock_t lock(m_mut, try_to_lock); //The upgradable lock must fail //if an exclusive or upgradable lock has been acquired //or there are too many sharable locks if(!lock.owns() || this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in || this->m_ctrl.num_upr_shar == constants::max_readers){ return false; } //Mark that upgradable lock has been acquired //And add upgradable to the sharable count this->m_ctrl.upgradable_in = 1; ++this->m_ctrl.num_upr_shar; return true;}inline bool interprocess_upgradable_mutex::timed_lock_upgradable (const boost::posix_time::ptime &abs_time){ if(abs_time == boost::posix_time::pos_infin){ this->lock_upgradable(); return true; } scoped_lock_t lock(m_mut, abs_time); if(!lock.owns()) return false; //The upgradable lock must block in the first gate //if an exclusive or upgradable lock has been acquired //or there are too many sharable locks while(this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in || this->m_ctrl.num_upr_shar == constants::max_readers){ if(!this->m_first_gate.timed_wait(lock, abs_time)){ return!(this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in || this->m_ctrl.num_upr_shar == constants::max_readers); } } //Mark that upgradable lock has been acquired //And add upgradable to the sharable count this->m_ctrl.upgradable_in = 1; ++this->m_ctrl.num_upr_shar; return true;}inline void interprocess_upgradable_mutex::unlock_upgradable(){ scoped_lock_t lock(m_mut); //Mark that upgradable lock has been acquired //And add upgradable to the sharable count this->m_ctrl.upgradable_in = 0; --this->m_ctrl.num_upr_shar; this->m_first_gate.notify_all();}//Sharable lockinginline void interprocess_upgradable_mutex::lock_sharable(){ scoped_lock_t lock(m_mut); //The sharable lock must block in the first gate //if an exclusive lock has been acquired //or there are too many sharable locks while(this->m_ctrl.exclusive_in || this->m_ctrl.num_upr_shar == constants::max_readers){ this->m_first_gate.wait(lock); } //Increment sharable count ++this->m_ctrl.num_upr_shar;}inline bool interprocess_upgradable_mutex::try_lock_sharable(){ scoped_lock_t lock(m_mut, try_to_lock); //The sharable lock must fail //if an exclusive lock has been acquired //or there are too many sharable locks if(!lock.owns() || this->m_ctrl.exclusive_in || this->m_ctrl.num_upr_shar == constants::max_readers){ return false; } //Increment sharable count ++this->m_ctrl.num_upr_shar; return true;}inline bool interprocess_upgradable_mutex::timed_lock_sharable (const boost::posix_time::ptime &abs_time){ if(abs_time == boost::posix_time::pos_infin){ this->lock_sharable(); return true; } scoped_lock_t lock(m_mut, abs_time); if(!lock.owns()) return false; //The sharable lock must block in the first gate //if an exclusive lock has been acquired //or there are too many sharable locks while (this->m_ctrl.exclusive_in || this->m_ctrl.num_upr_shar == constants::max_readers){ if(!this->m_first_gate.timed_wait(lock, abs_time)){ return!(this->m_ctrl.exclusive_in || this->m_ctrl.num_upr_shar == constants::max_readers); } } //Increment sharable count ++this->m_ctrl.num_upr_shar; return true;}inline void interprocess_upgradable_mutex::unlock_sharable(){ scoped_lock_t lock(m_mut); //Decrement sharable count --this->m_ctrl.num_upr_shar; if (this->m_ctrl.num_upr_shar == 0){ this->m_second_gate.notify_one(); } //Check if there are blocked sharables because of //there were too many sharables else if(this->m_ctrl.num_upr_shar == (constants::max_readers-1)){ this->m_first_gate.notify_all(); }}//Downgradinginline void interprocess_upgradable_mutex::unlock_and_lock_upgradable(){ scoped_lock_t lock(m_mut); //Unmark it as exclusive this->m_ctrl.exclusive_in = 0; //Mark it as upgradable this->m_ctrl.upgradable_in = 1; //The sharable count should be 0 so increment it this->m_ctrl.num_upr_shar = 1; //Notify readers that they can enter m_first_gate.notify_all();}inline void interprocess_upgradable_mutex::unlock_and_lock_sharable(){ scoped_lock_t lock(m_mut); //Unmark it as exclusive this->m_ctrl.exclusive_in = 0; //The sharable count should be 0 so increment it this->m_ctrl.num_upr_shar = 1; //Notify readers that they can enter m_first_gate.notify_all();}inline void interprocess_upgradable_mutex::unlock_upgradable_and_lock_sharable(){ scoped_lock_t lock(m_mut); //Unmark it as upgradable (we don't have to decrement count) this->m_ctrl.upgradable_in = 0; //Notify readers/upgradable that they can enter m_first_gate.notify_all();}//Upgradinginline void interprocess_upgradable_mutex::unlock_upgradable_and_lock(){ scoped_lock_t lock(m_mut); //Simulate unlock_upgradable() without //notifying sharables. this->m_ctrl.upgradable_in = 0; --this->m_ctrl.num_upr_shar; //Execute the second half of exclusive locking this->m_ctrl.exclusive_in = 1; //Prepare rollback upgradable_to_exclusive_rollback rollback(m_ctrl); while (this->m_ctrl.num_upr_shar){ this->m_second_gate.wait(lock); } rollback.release();}inline bool interprocess_upgradable_mutex::try_unlock_upgradable_and_lock(){ scoped_lock_t lock(m_mut, try_to_lock); //Check if there are no readers if(!lock.owns() || this->m_ctrl.num_upr_shar != 1){ return false; } //Now unlock upgradable and mark exclusive this->m_ctrl.upgradable_in = 0; --this->m_ctrl.num_upr_shar; this->m_ctrl.exclusive_in = 1; return true;}inline bool interprocess_upgradable_mutex::timed_unlock_upgradable_and_lock (const boost::posix_time::ptime &abs_time){ scoped_lock_t lock(m_mut, abs_time); if(!lock.owns()) return false; //Simulate unlock_upgradable() without //notifying sharables. this->m_ctrl.upgradable_in = 0; --this->m_ctrl.num_upr_shar; //Execute the second half of exclusive locking this->m_ctrl.exclusive_in = 1; //Prepare rollback upgradable_to_exclusive_rollback rollback(m_ctrl); while (this->m_ctrl.num_upr_shar){ if(!this->m_second_gate.timed_wait(lock, abs_time)){ return !(this->m_ctrl.num_upr_shar); } } rollback.release(); return true;}inline bool interprocess_upgradable_mutex::try_unlock_sharable_and_lock(){ scoped_lock_t lock(m_mut, try_to_lock); //If we can't lock or any has there is any exclusive, upgradable //or sharable mark return false; if(!lock.owns() || this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in || this->m_ctrl.num_upr_shar != 1){ return false; } this->m_ctrl.exclusive_in = 1; this->m_ctrl.num_upr_shar = 0; return true;}inline bool interprocess_upgradable_mutex::try_unlock_sharable_and_lock_upgradable(){ scoped_lock_t lock(m_mut, try_to_lock); //The upgradable lock must fail //if an exclusive or upgradable lock has been acquired if(!lock.owns() || this->m_ctrl.exclusive_in || this->m_ctrl.upgradable_in){ return false; } //Mark that upgradable lock has been acquired this->m_ctrl.upgradable_in = 1; return true;}/// @endcond} //namespace interprocess {} //namespace boost {#include <boost/interprocess/detail/config_end.hpp>#endif //BOOST_INTERPROCESS_UPGRADABLE_MUTEX_HPP
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?