Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
24
25////////////////////////////////////////////////////////////////////////////////
26/// Create reader/write lock.
27
28TRWLock::TRWLock() : fLockFree(&fMutex)
29{
30 fReaders = 0;
31 fWriters = 0;
32}
33
34////////////////////////////////////////////////////////////////////////////////
35/// Obtain a reader lock. Returns always 0.
36
38{
39 fMutex.Lock();
40
41 while (fWriters)
43
44 fReaders++;
45
46 fMutex.UnLock();
47
48 return 0;
49}
50
51////////////////////////////////////////////////////////////////////////////////
52/// Unlock reader lock. Returns -1 if thread was not locked,
53/// 0 if everything ok.
54
56{
57 fMutex.Lock();
58
59 if (fReaders == 0) {
60 fMutex.UnLock();
61 return -1;
62 } else {
63 fReaders--;
64 if (fReaders == 0)
66 fMutex.UnLock();
67 return 0;
68 }
69}
70
71////////////////////////////////////////////////////////////////////////////////
72/// Obtain a writer lock. Returns always 0.
73
75{
76 fMutex.Lock();
77
78 while (fWriters || fReaders)
80
81 fWriters++;
82
83 fMutex.UnLock();
84
85 return 0;
86}
87
88////////////////////////////////////////////////////////////////////////////////
89/// Unlock writer lock. Returns -1 if thread was not locked,
90/// 0 if everything ok.
91
93{
94 fMutex.Lock();
95
96 if (fWriters == 0) {
97 fMutex.UnLock();
98 return -1;
99 } else {
100 fWriters = 0;
102 fMutex.UnLock();
103 return 0;
104 }
105}
Int_t Broadcast()
Definition TCondition.h:54
Int_t Signal()
Definition TCondition.h:53
Int_t Wait()
Wait to be signaled.
Int_t UnLock() override
Unlock the mutex.
Definition TMutex.cxx:67
Int_t Lock() override
Lock the mutex.
Definition TMutex.cxx:45
Int_t ReadLock()
Obtain a reader lock. Returns always 0.
Definition TRWLock.cxx:37
TRWLock()
Create reader/write lock.
Definition TRWLock.cxx:28
TMutex fMutex
Definition TRWLock.h:36
Int_t WriteLock()
Obtain a writer lock. Returns always 0.
Definition TRWLock.cxx:74
Int_t WriteUnLock()
Unlock writer lock.
Definition TRWLock.cxx:92
TCondition fLockFree
Definition TRWLock.h:37
Int_t ReadUnLock()
Unlock reader lock.
Definition TRWLock.cxx:55
Int_t fReaders
Definition TRWLock.h:34
Int_t fWriters
Definition TRWLock.h:35