TRWLock.cxx

Go to the documentation of this file.
00001 // @(#)root/thread:$Id: TRWLock.cxx 20882 2007-11-19 11:31:26Z rdm $
00002 // Author: Fons Rademakers   04/01/2000
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TRWLock                                                              //
00015 //                                                                      //
00016 // This class implements a reader/writer lock. A rwlock allows          //
00017 // a resource to be accessed by multiple reader threads but only        //
00018 // one writer thread.                                                   //
00019 //                                                                      //
00020 //////////////////////////////////////////////////////////////////////////
00021 
00022 #include "TRWLock.h"
00023 
00024 ClassImp(TRWLock)
00025 
00026 //______________________________________________________________________________
00027 TRWLock::TRWLock() : fLockFree(&fMutex)
00028 {
00029    // Create reader/write lock.
00030 
00031    fReaders = 0;
00032    fWriters = 0;
00033 }
00034 
00035 //______________________________________________________________________________
00036 Int_t TRWLock::ReadLock()
00037 {
00038    // Obtain a reader lock. Returns always 0.
00039 
00040    fMutex.Lock();
00041 
00042    while (fWriters)
00043       fLockFree.Wait();
00044 
00045    fReaders++;
00046 
00047    fMutex.UnLock();
00048 
00049    return 0;
00050 }
00051 
00052 //______________________________________________________________________________
00053 Int_t TRWLock::ReadUnLock()
00054 {
00055    // Unlock reader lock. Returns -1 if thread was not locked,
00056    // 0 if everything ok.
00057 
00058    fMutex.Lock();
00059 
00060    if (fReaders == 0) {
00061       fMutex.UnLock();
00062       return -1;
00063    } else {
00064       fReaders--;
00065       if (fReaders == 0)
00066          fLockFree.Signal();
00067       fMutex.UnLock();
00068       return 0;
00069    }
00070 }
00071 
00072 //______________________________________________________________________________
00073 Int_t TRWLock::WriteLock()
00074 {
00075    // Obtain a writer lock. Returns always 0.
00076 
00077    fMutex.Lock();
00078 
00079    while (fWriters || fReaders)
00080       fLockFree.Wait();
00081 
00082    fWriters++;
00083 
00084    fMutex.UnLock();
00085 
00086    return 0;
00087 }
00088 
00089 //______________________________________________________________________________
00090 Int_t TRWLock::WriteUnLock()
00091 {
00092    // Unlock writer lock. Returns -1 if thread was not locked,
00093    // 0 if everything ok.
00094 
00095    fMutex.Lock();
00096 
00097    if (fWriters == 0) {
00098       fMutex.UnLock();
00099       return -1;
00100    } else {
00101       fWriters = 0;
00102       fLockFree.Broadcast();
00103       fMutex.UnLock();
00104       return 0;
00105    }
00106 }

Generated on Tue Jul 5 14:12:02 2011 for ROOT_528-00b_version by  doxygen 1.5.1