Logo ROOT  
Reference Guide
TVirtualRWMutex.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Philippe Canal, 2017
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2017, 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 #ifndef ROOT_TVirtualRWMutex
13 #define ROOT_TVirtualRWMutex
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TVirtualRWMutex //
19 // //
20 // This class implements a read-write mutex interface. The actual work //
21 // is done via TRWSpinLock which is available as soon as the thread //
22 // library is loaded. //
23 // //
24 //////////////////////////////////////////////////////////////////////////
25 
26 #include "TVirtualMutex.h"
27 
28 #include <memory>
29 
30 namespace ROOT {
31 
32 class TVirtualRWMutex;
33 
34 // Global mutex set in TThread::Init
35 // Use either R__READ_LOCKGUARD(ROOT::gCoreMutex);
36 // or R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
37 R__EXTERN TVirtualRWMutex *gCoreMutex;
38 
39 class TVirtualRWMutex : public TVirtualMutex {
40 
41 public:
42  // The following are opaque type and are never really declared
43  // The specific implementation of TInterpreter will cast the
44  // value of pointer to this types to the correct type (possibly
45  // distinct from these)
46  class Hint_t;
47 
48  /// \class State
49  /// Earlier lock state as returned by `GetState()` that can be passed to
50  /// `Restore()`
51  struct State {
52  virtual ~State(); // implemented in TVirtualMutex.cxx
53  };
54 
55  /// \class StateDelta
56  /// State as returned by `GetStateDelta()` that can be passed to
57  /// `Restore()`
58  struct StateDelta {
59  virtual ~StateDelta(); // implemented in TVirtualMutex.cxx
60  };
61 
62  virtual Hint_t *ReadLock() = 0;
63  virtual void ReadUnLock(Hint_t *) = 0;
64  virtual Hint_t *WriteLock() = 0;
65  virtual void WriteUnLock(Hint_t *) = 0;
66 
67  Int_t Lock() override { WriteLock(); return 1; }
68  Int_t TryLock() override { WriteLock(); return 1; }
69  Int_t UnLock() override { WriteUnLock(nullptr); return 1; }
70  Int_t CleanUp() override { WriteUnLock(nullptr); return 1; }
71 
72  virtual std::unique_ptr<State> GetStateBefore() = 0;
73  virtual std::unique_ptr<StateDelta> Rewind(const State& earlierState) = 0;
74  virtual void Apply(std::unique_ptr<StateDelta> &&delta) = 0;
75 
76  TVirtualRWMutex *Factory(Bool_t /*recursive*/ = kFALSE) override = 0;
77 
78  ClassDefOverride(TVirtualRWMutex, 0) // Virtual mutex lock class
79 };
80 
81 //////////////////////////////////////////////////////////////////////////
82 // //
83 // TReadLockGuard //
84 // //
85 // This class provides RW mutex resource management in a guaranteed and //
86 // exception safe way. Use like this: //
87 // { //
88 // TReadLockGuard guard(mutex); //
89 // ... // read something //
90 // } //
91 // when guard goes out of scope the mutex is unlocked in the TLockGuard //
92 // destructor. The exception mechanism takes care of calling the dtors //
93 // of local objects so it is exception safe. //
94 // //
95 //////////////////////////////////////////////////////////////////////////
96 
97 class TReadLockGuard {
98 
99 private:
100  TVirtualRWMutex *const fMutex;
101  TVirtualRWMutex::Hint_t *fHint;
102 
103  TReadLockGuard(const TReadLockGuard&) = delete;
104  TReadLockGuard& operator=(const TReadLockGuard&) = delete;
105 
106 public:
107  TReadLockGuard(TVirtualRWMutex *mutex) : fMutex(mutex), fHint(nullptr) {
108  if (fMutex) fHint = fMutex->ReadLock();
109  }
110 
112 
113  ClassDefNV(TReadLockGuard,0) // Exception safe read locking/unlocking of mutex
114 };
115 
116 class TWriteLockGuard {
117 
118 private:
119  TVirtualRWMutex *const fMutex;
120  TVirtualRWMutex::Hint_t *fHint;
121 
123  TWriteLockGuard& operator=(const TWriteLockGuard&) = delete;
124 
125 public:
126  TWriteLockGuard(TVirtualRWMutex *mutex) : fMutex(mutex), fHint(nullptr) {
127  if (fMutex) fHint = fMutex->WriteLock();
128  }
129 
131 
132  ClassDefNV(TWriteLockGuard,0) // Exception safe read locking/unlocking of mutex
133 };
134 
135 } // namespace ROOT.
136 
137 // Zero overhead macros in case not compiled with thread support
138 #if defined (_REENTRANT) || defined (WIN32)
139 
140 #define R__READ_LOCKGUARD(mutex) ::ROOT::TReadLockGuard _R__UNIQUE_(R__readguard)(mutex)
141 #define R__READ_LOCKGUARD_NAMED(name,mutex) ::ROOT::TReadLockGuard _NAME2_(R__readguard,name)(mutex)
142 
143 #define R__WRITE_LOCKGUARD(mutex) ::ROOT::TWriteLockGuard _R__UNIQUE_(R__readguard)(mutex)
144 #define R__WRITE_LOCKGUARD_NAMED(name,mutex) ::ROOT::TWriteLockGuard _NAME2_(R__readguard,name)(mutex)
145 
146 #else
147 
148 #define R__READ_LOCKGUARD(mutex) (void)mutex
149 #define R__READ_LOCKGUARD_NAMED(name,mutex) (void)mutex
150 
151 #define R__WRITE_LOCKGUARD(mutex) (void)mutex
152 #define R__WRITE_LOCKGUARD_NAMED(name,mutex) (void)mutex
153 
154 #endif
155 
156 
157 #endif
ROOT::TVirtualRWMutex::Rewind
virtual std::unique_ptr< StateDelta > Rewind(const State &earlierState)=0
ROOT::TReadLockGuard
Definition: TVirtualRWMutex.h:103
ROOT::TVirtualRWMutex::StateDelta
Definition: TVirtualRWMutex.h:64
ROOT::TVirtualRWMutex::ReadUnLock
virtual void ReadUnLock(Hint_t *)=0
ROOT::TVirtualRWMutex::WriteUnLock
virtual void WriteUnLock(Hint_t *)=0
ROOT::TVirtualRWMutex::Apply
virtual void Apply(std::unique_ptr< StateDelta > &&delta)=0
ROOT::TWriteLockGuard::operator=
TWriteLockGuard & operator=(const TWriteLockGuard &)=delete
ROOT::TReadLockGuard::fMutex
TVirtualRWMutex *const fMutex
Definition: TVirtualRWMutex.h:106
Int_t
int Int_t
Definition: RtypesCore.h:45
TVirtualMutex
Definition: TVirtualMutex.h:32
ROOT::TReadLockGuard::~TReadLockGuard
~TReadLockGuard()
Definition: TVirtualRWMutex.h:117
ROOT::gCoreMutex
R__EXTERN TVirtualRWMutex * gCoreMutex
Definition: TVirtualRWMutex.h:38
ClassDefOverride
#define ClassDefOverride(name, id)
Definition: Rtypes.h:329
bool
ROOT::TReadLockGuard::TReadLockGuard
TReadLockGuard(const TReadLockGuard &)=delete
ROOT::TWriteLockGuard::TWriteLockGuard
TWriteLockGuard(const TWriteLockGuard &)=delete
ROOT::TVirtualRWMutex
Definition: TVirtualRWMutex.h:45
ROOT::TVirtualRWMutex::TryLock
Int_t TryLock() override
Definition: TVirtualRWMutex.h:74
ROOT::TWriteLockGuard
Definition: TVirtualRWMutex.h:122
ROOT::TVirtualRWMutex::Lock
Int_t Lock() override
Definition: TVirtualRWMutex.h:73
ROOT::TVirtualRWMutex::WriteLock
virtual Hint_t * WriteLock()=0
ROOT::TVirtualRWMutex::GetStateBefore
virtual std::unique_ptr< State > GetStateBefore()=0
ROOT::TWriteLockGuard::fHint
TVirtualRWMutex::Hint_t * fHint
Definition: TVirtualRWMutex.h:126
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
ROOT::TReadLockGuard::operator=
TReadLockGuard & operator=(const TReadLockGuard &)=delete
ROOT::TVirtualRWMutex::StateDelta::~StateDelta
virtual ~StateDelta()
TVirtualMutex.h
ROOT::TVirtualRWMutex::ReadLock
virtual Hint_t * ReadLock()=0
ROOT::TVirtualRWMutex::UnLock
Int_t UnLock() override
Definition: TVirtualRWMutex.h:75
ROOT::TVirtualRWMutex::CleanUp
Int_t CleanUp() override
Definition: TVirtualRWMutex.h:76
ROOT::TVirtualRWMutex::State::~State
virtual ~State()
ROOT::TVirtualRWMutex::State
Definition: TVirtualRWMutex.h:57
R__EXTERN
#define R__EXTERN
Definition: DllImport.h:27
ROOT::TWriteLockGuard::fMutex
TVirtualRWMutex *const fMutex
Definition: TVirtualRWMutex.h:125
ROOT::TWriteLockGuard::~TWriteLockGuard
~TWriteLockGuard()
Definition: TVirtualRWMutex.h:136
ROOT::TVirtualRWMutex::Factory
TVirtualRWMutex * Factory(Bool_t=kFALSE) override=0
ROOT
VSD Structures.
Definition: StringConv.hxx:21
ROOT::TReadLockGuard::fHint
TVirtualRWMutex::Hint_t * fHint
Definition: TVirtualRWMutex.h:107
int
ClassDefNV
#define ClassDefNV(name, id)
Definition: Rtypes.h:333