#ifndef READ_WRITE_MUTEX_HPP #define READ_WRITE_MUTEX_HPP // read_write_mutex.hpp // // (C) Copyright 2005 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 namespace boost { #define BOOST_RW_MUTEX_INIT {BOOST_ONCE_INIT} class read_write_mutex { static const long unlocked_state=0; static const long reading_state=1; static const long writing_state=2; static const long upgrading_state=3; static const long reading_with_upgradeable_state=4; static const long acquiring_reading_state=5; static const long releasing_reading_state=6; static const long releasing_reading_with_upgradeable_state=7; public: boost::once_flag flag; long active_pending_flag; void* active_pending; long mutex_state_flag; void* mutex_state_sem; long reader_count; private: struct initializer { read_write_mutex* self; initializer(read_write_mutex* self_): self(self_) {} void operator()() { self->active_pending_flag=0; self->active_pending=CreateSemaphore(NULL,0,1,NULL); self->mutex_state_sem=CreateSemaphore(NULL,0,1,NULL); self->reader_count=0; self->mutex_state_flag=unlocked_state; } }; void initialize() { boost::call_once(initializer(this),flag); } void release_mutex_state_sem() { ReleaseSemaphore(mutex_state_sem,1,NULL); } void acquire_mutex_state_sem() { BOOST_WAIT_FOR_SINGLE_OBJECT(mutex_state_sem,BOOST_INFINITE); } template long enter_new_state(long const (&old_states)[array_size],long new_state) { bool release_if_blocked=false; while(true) { for(unsigned i=0;i