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
25
26////////////////////////////////////////////////////////////////////////////////
27/// Create reader/write lock.
28
29TRWLock::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)
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)
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)
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}
#define ClassImp(name)
Definition Rtypes.h:382
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:68
Int_t Lock() override
Lock the mutex.
Definition TMutex.cxx:46
Int_t ReadLock()
Obtain a reader lock. Returns always 0.
Definition TRWLock.cxx:38
TRWLock()
Create reader/write lock.
Definition TRWLock.cxx:29
TMutex fMutex
Definition TRWLock.h:36
Int_t WriteLock()
Obtain a writer lock. Returns always 0.
Definition TRWLock.cxx:75
Int_t WriteUnLock()
Unlock writer lock.
Definition TRWLock.cxx:93
TCondition fLockFree
Definition TRWLock.h:37
Int_t ReadUnLock()
Unlock reader lock.
Definition TRWLock.cxx:56
Int_t fReaders
Definition TRWLock.h:34
Int_t fWriters
Definition TRWLock.h:35