rw_lock.cpp

Go to the documentation of this file.
00001 #ifndef RW_LOCK_IMPLEMENTATION_FILE
00002 #define RW_LOCK_IMPLEMENTATION_FILE
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : reader_writer_lock                                                *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1998-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "mechanisms_implementation_only.h"
00019 #include "rw_lock.h"
00020 
00021 #include <basis/function.h>
00022 #include <basis/mutex.h>
00023 
00024 //#define DEBUG_RW_LOCK
00025   // uncomment if you want the debugging version of the reader_writer_lock.
00026 
00027 //hmmm: implement logging...
00028 #undef LOG
00029 #ifdef DEBUG_RW_LOCK
00030   #define LOG(to_print) CLASS_EMERGENCY_LOG(program_wide_logger(), to_print)
00031 #else
00032   #define LOG(to_print) {}
00033 #endif
00034 
00035 reader_writer_lock::reader_writer_lock()
00036 : _write_lock(new mutex()),
00037   _reader_count(0),
00038   _variable_lock(new mutex())
00039 {
00040 }
00041 
00042 reader_writer_lock::~reader_writer_lock()
00043 {
00044   delete _write_lock;
00045   _write_lock = NIL;
00046   delete _variable_lock;
00047   _variable_lock = NIL;
00048 }
00049 
00050 void reader_writer_lock::begin_write()
00051 {
00052   FUNCDEF("begin_write");
00053   LOG(istring(istring::SPRINTF, "writer: wanting write lock with %d readers.",
00054       _reader_count));
00055   _write_lock->lock(); 
00056   LOG(istring(istring::SPRINTF, "writer: got write lock with %d readers.",
00057       _reader_count));
00058 }
00059 
00060 void reader_writer_lock::end_write()
00061 { 
00062   FUNCDEF("end_write");
00063   LOG("writer: undoing write lock");
00064   _write_lock->unlock();
00065 }
00066 
00067 void reader_writer_lock::begin_read()
00068 {
00069   _variable_lock->lock();
00070   LOG(istring(istring::SPRINTF, "reader: got variable lock with %d "
00071       "readers.", _reader_count));
00072   _reader_count++;
00073   if (_reader_count == 1) {
00074     LOG("reader: wanting write lock");
00075     _write_lock->lock();
00076     LOG("reader: got write lock");
00077   }
00078   LOG(istring(istring::SPRINTF, "++readers=%d", _reader_count));
00079   _variable_lock->unlock();
00080 }
00081 
00082 void reader_writer_lock::end_read()
00083 {
00084   _variable_lock->lock();
00085   LOG(istring(istring::SPRINTF, "reader: got variable lock with %d readers.",
00086       _reader_count));
00087   _reader_count--;
00088   LOG(istring(istring::SPRINTF, "--readers=%d", _reader_count));
00089   if (_reader_count == 0) {
00090     LOG("reader giving up write lock");
00091     _write_lock->unlock();
00092     LOG("after reader gave up write lock");
00093   }
00094   _variable_lock->unlock();
00095   LOG("after reader gave up variable lock");
00096 }
00097 
00098 
00099 #endif //RW_LOCK_IMPLEMENTATION_FILE
00100 

Generated on Fri Nov 21 04:29:50 2008 for HOOPLE Libraries by  doxygen 1.5.1