/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ #ifndef RWMONITOR #define RWMONITOR #include /*********************************************** * Joseph Gleason * This is a set of functions that handles implements * a Reader Writer Monitor. It allows unlimited readers * or one writer at a time. * The data/objects/whatever actually protected by this * monitor are expected to be stored elsewhere. This * is a purely volentary on the part of the programmer * protection method. If you need more strict protection * use a language that does real OOP. */ struct rw_monitor { int WaitingReaders; int RunningReaders; int WaitingWriters; int RunningWriters; pthread_mutex_t Mutex; pthread_cond_t Cond; }; typedef struct rw_monitor rw_monitor_t; /*Fills in the data structure with inital values and creates the pthread things */ void rw_monitor_init(rw_monitor_t* R); /*Deallocated the pthread things*/ void rw_monitor_destroy(rw_monitor_t *R); /*Tells the monitor that the calling thread wishes to read. Does not return until it is safe to read.*/ void rw_monitor_read(rw_monitor_t* R); /*Tells the monitor that the calling thread is done reading. */ void rw_monitor_unread(rw_monitor_t* R); /*Tells the monitor that the calling thread wishes to write Does not return until it is safe to write.*/ void rw_monitor_write(rw_monitor_t* R); /*Tells the monitor that the calling thread is done writing.*/ void rw_monitor_unwrite(rw_monitor_t* R); #endif