ROOT
master
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
28
TRWLock::TRWLock
() : fLockFree(&fMutex)
29
{
30
fReaders
= 0;
31
fWriters
= 0;
32
}
33
34
////////////////////////////////////////////////////////////////////////////////
35
/// Obtain a reader lock. Returns always 0.
36
37
Int_t
TRWLock::ReadLock
()
38
{
39
fMutex
.
Lock
();
40
41
while
(
fWriters
)
42
fLockFree
.
Wait
();
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
55
Int_t
TRWLock::ReadUnLock
()
56
{
57
fMutex
.
Lock
();
58
59
if
(
fReaders
== 0) {
60
fMutex
.
UnLock
();
61
return
-1;
62
}
else
{
63
fReaders
--;
64
if
(
fReaders
== 0)
65
fLockFree
.
Signal
();
66
fMutex
.
UnLock
();
67
return
0;
68
}
69
}
70
71
////////////////////////////////////////////////////////////////////////////////
72
/// Obtain a writer lock. Returns always 0.
73
74
Int_t
TRWLock::WriteLock
()
75
{
76
fMutex
.
Lock
();
77
78
while
(
fWriters
||
fReaders
)
79
fLockFree
.
Wait
();
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
92
Int_t
TRWLock::WriteUnLock
()
93
{
94
fMutex
.
Lock
();
95
96
if
(
fWriters
== 0) {
97
fMutex
.
UnLock
();
98
return
-1;
99
}
else
{
100
fWriters
= 0;
101
fLockFree
.
Broadcast
();
102
fMutex
.
UnLock
();
103
return
0;
104
}
105
}
TRWLock.h
TCondition::Broadcast
Int_t Broadcast()
Definition
TCondition.h:54
TCondition::Signal
Int_t Signal()
Definition
TCondition.h:53
TCondition::Wait
Int_t Wait()
Wait to be signaled.
Definition
TCondition.cxx:74
TMutex::UnLock
Int_t UnLock() override
Unlock the mutex.
Definition
TMutex.cxx:67
TMutex::Lock
Int_t Lock() override
Lock the mutex.
Definition
TMutex.cxx:45
TRWLock::ReadLock
Int_t ReadLock()
Obtain a reader lock. Returns always 0.
Definition
TRWLock.cxx:37
TRWLock::TRWLock
TRWLock()
Create reader/write lock.
Definition
TRWLock.cxx:28
TRWLock::fMutex
TMutex fMutex
Definition
TRWLock.h:36
TRWLock::WriteLock
Int_t WriteLock()
Obtain a writer lock. Returns always 0.
Definition
TRWLock.cxx:74
TRWLock::WriteUnLock
Int_t WriteUnLock()
Unlock writer lock.
Definition
TRWLock.cxx:92
TRWLock::fLockFree
TCondition fLockFree
Definition
TRWLock.h:37
TRWLock::ReadUnLock
Int_t ReadUnLock()
Unlock reader lock.
Definition
TRWLock.cxx:55
TRWLock::fReaders
Int_t fReaders
Definition
TRWLock.h:34
TRWLock::fWriters
Int_t fWriters
Definition
TRWLock.h:35
int
core
thread
src
TRWLock.cxx
ROOT master - Reference Guide Generated on Tue Oct 7 2025 04:42:00 (GVA Time) using Doxygen 1.10.0