Logo ROOT   6.08/07
Reference Guide
TRWLock.cxx
Go to the documentation of this file.
1 // @(#)root/thread:$Id$
2 // Author: Fons Rademakers 04/01/2000
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 //////////////////////////////////////////////////////////////////////////
13 // //
14 // TRWLock //
15 // //
16 // This class implements a reader/writer lock. A rwlock allows //
17 // a resource to be accessed by multiple reader threads but only //
18 // one writer thread. //
19 // //
20 //////////////////////////////////////////////////////////////////////////
21 
22 #include "TRWLock.h"
23 
25 
26 ////////////////////////////////////////////////////////////////////////////////
27 /// Create reader/write lock.
28 
29 TRWLock::TRWLock() : fLockFree(&fMutex)
30 {
31  fReaders = 0;
32  fWriters = 0;
33 }
34 
35 ////////////////////////////////////////////////////////////////////////////////
36 /// Obtain a reader lock. Returns always 0.
37 
39 {
40  fMutex.Lock();
41 
42  while (fWriters)
43  fLockFree.Wait();
44 
45  fReaders++;
46 
47  fMutex.UnLock();
48 
49  return 0;
50 }
51 
52 ////////////////////////////////////////////////////////////////////////////////
53 /// Unlock reader lock. Returns -1 if thread was not locked,
54 /// 0 if everything ok.
55 
57 {
58  fMutex.Lock();
59 
60  if (fReaders == 0) {
61  fMutex.UnLock();
62  return -1;
63  } else {
64  fReaders--;
65  if (fReaders == 0)
66  fLockFree.Signal();
67  fMutex.UnLock();
68  return 0;
69  }
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Obtain a writer lock. Returns always 0.
74 
76 {
77  fMutex.Lock();
78 
79  while (fWriters || fReaders)
80  fLockFree.Wait();
81 
82  fWriters++;
83 
84  fMutex.UnLock();
85 
86  return 0;
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Unlock writer lock. Returns -1 if thread was not locked,
91 /// 0 if everything ok.
92 
94 {
95  fMutex.Lock();
96 
97  if (fWriters == 0) {
98  fMutex.UnLock();
99  return -1;
100  } else {
101  fWriters = 0;
103  fMutex.UnLock();
104  return 0;
105  }
106 }
Int_t WriteUnLock()
Unlock writer lock.
Definition: TRWLock.cxx:93
Int_t WriteLock()
Obtain a writer lock. Returns always 0.
Definition: TRWLock.cxx:75
Int_t ReadLock()
Obtain a reader lock. Returns always 0.
Definition: TRWLock.cxx:38
Int_t fReaders
Definition: TRWLock.h:40
int Int_t
Definition: RtypesCore.h:41
Int_t UnLock()
Unlock the mutex.
Definition: TMutex.cxx:68
Int_t Broadcast()
Definition: TCondition.h:58
Int_t fWriters
Definition: TRWLock.h:41
Int_t Wait()
Wait to be signaled.
Definition: TCondition.cxx:75
TMutex fMutex
Definition: TRWLock.h:42
TCondition fLockFree
Definition: TRWLock.h:43
#define ClassImp(name)
Definition: Rtypes.h:279
Int_t Lock()
Lock the mutex.
Definition: TMutex.cxx:46
Int_t ReadUnLock()
Unlock reader lock.
Definition: TRWLock.cxx:56
Int_t Signal()
Definition: TCondition.h:57