#ifndef BOOST_THREAD_WIN32_READ_WRITE_MUTEX_HPP #define BOOST_THREAD_WIN32_READ_WRITE_MUTEX_HPP // (C) Copyright 2006 Anthony Williams // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include namespace boost { class read_write_mutex { private: struct state_data { unsigned shared_count:10; unsigned shared_waiting:10; unsigned exclusive:1; unsigned exclusive_waiting:10; unsigned exclusive_waiting_blocked:1; friend bool operator==(state_data const& lhs,state_data const& rhs) { return *reinterpret_cast(&lhs)==*reinterpret_cast(&rhs); } }; state_data state; boost::mutex state_change; boost::condition shared_cond; boost::condition exclusive_cond; void release_waiters(state_data old_state) { if(old_state.exclusive_waiting) { exclusive_cond.notify_one(); } if(old_state.shared_waiting || old_state.exclusive_waiting) { shared_cond.notify_all(); } } public: read_write_mutex() { state_data state_={0}; state=state_; } ~read_write_mutex() { } void lock_shareable() { boost::mutex::scoped_lock lock(state_change); while(true) { state_data old_state=state; state_data new_state=old_state; if(new_state.exclusive || new_state.exclusive_waiting_blocked) { ++new_state.shared_waiting; } else { ++new_state.shared_count; } state=new_state; if(!(old_state.exclusive| old_state.exclusive_waiting_blocked)) { return; } shared_cond.wait(lock); } } void unlock_shareable() { boost::mutex::scoped_lock lock(state_change); state_data old_state=state; state_data new_state=old_state; bool const last_reader=!--new_state.shared_count; if(last_reader) { if(new_state.exclusive_waiting) { --new_state.exclusive_waiting; new_state.exclusive_waiting_blocked=false; } new_state.shared_waiting=0; } state=new_state; if(last_reader) { release_waiters(old_state); } } void lock() { boost::mutex::scoped_lock lock(state_change); while(true) { state_data old_state=state; state_data new_state=old_state; if(new_state.shared_count || new_state.exclusive) { ++new_state.exclusive_waiting; new_state.exclusive_waiting_blocked=true; } else { new_state.exclusive=true; } state=new_state; if(!old_state.shared_count && !old_state.exclusive) { break; } exclusive_cond.wait(lock); } } void unlock() { boost::mutex::scoped_lock lock(state_change); state_data old_state=state; state_data new_state=old_state; new_state.exclusive=false; if(new_state.exclusive_waiting) { --new_state.exclusive_waiting; new_state.exclusive_waiting_blocked=false; } new_state.shared_waiting=0; state=new_state; release_waiters(old_state); } class scoped_read_lock { read_write_mutex& m; public: scoped_read_lock(read_write_mutex& m_): m(m_) { m.lock_shareable(); } ~scoped_read_lock() { m.unlock_shareable(); } }; class scoped_write_lock { read_write_mutex& m; bool locked; public: scoped_write_lock(read_write_mutex& m_): m(m_),locked(false) { lock(); } void lock() { m.lock(); locked=true; } void unlock() { m.unlock(); locked=false; } ~scoped_write_lock() { if(locked) { unlock(); } } }; }; } #endif