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);
38 
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 
56  /// State of gCoreMutex when the first interpreter-related function was invoked.
57  std::unique_ptr<ROOT::TVirtualRWMutex::State> fState;
58 
59  /// Interpreter-related functions will push the "entry" lock state to *this.
60  /// Recursive calls will do that, too - but we must only forget about the lock
61  /// state once this recursion count went to 0.
63 
64  operator bool() const { return (bool)fState; }
65  };
66 
67  /// \class StateDelta
68  /// State as returned by `GetStateDelta()` that can be passed to
69  /// `Restore()`
70  struct StateDelta {
71  virtual ~StateDelta(); // implemented in TVirtualMutex.cxx
72  };
73 
74  virtual Hint_t *ReadLock() = 0;
75  virtual void ReadUnLock(Hint_t *) = 0;
76  virtual Hint_t *WriteLock() = 0;
77  virtual void WriteUnLock(Hint_t *) = 0;
78 
79  Int_t Lock() override { WriteLock(); return 1; }
80  Int_t TryLock() override { WriteLock(); return 1; }
81  Int_t UnLock() override { WriteUnLock(nullptr); return 1; }
82  Int_t CleanUp() override { WriteUnLock(nullptr); return 1; }
83 
84  virtual std::unique_ptr<State> GetStateBefore() = 0;
85  virtual std::unique_ptr<StateDelta> Rewind(const State& earlierState) = 0;
86  virtual void Apply(std::unique_ptr<StateDelta> &&delta) = 0;
87 
88  TVirtualRWMutex *Factory(Bool_t /*recursive*/ = kFALSE) override = 0;
89 
90  ClassDefOverride(TVirtualRWMutex, 0) // Virtual mutex lock class
91 };
92 
93 //////////////////////////////////////////////////////////////////////////
94 // //
95 // TReadLockGuard //
96 // //
97 // This class provides RW mutex resource management in a guaranteed and //
98 // exception safe way. Use like this: //
99 // { //
100 // TReadLockGuard guard(mutex); //
101 // ... // read something //
102 // } //
103 // when guard goes out of scope the mutex is unlocked in the TLockGuard //
104 // destructor. The exception mechanism takes care of calling the dtors //
105 // of local objects so it is exception safe. //
106 // //
107 //////////////////////////////////////////////////////////////////////////
108 
110 
111 private:
113  TVirtualRWMutex::Hint_t *fHint;
114 
115  TReadLockGuard(const TReadLockGuard&) = delete;
117 
118 public:
119  TReadLockGuard(TVirtualRWMutex *mutex) : fMutex(mutex), fHint(nullptr) {
120  if (fMutex) fHint = fMutex->ReadLock();
121  }
122 
124 
125  ClassDefNV(TReadLockGuard,0) // Exception safe read locking/unlocking of mutex
126 };
127 
129 
130 private:
132  TVirtualRWMutex::Hint_t *fHint;
133 
136 
137 public:
138  TWriteLockGuard(TVirtualRWMutex *mutex) : fMutex(mutex), fHint(nullptr) {
139  if (fMutex) fHint = fMutex->WriteLock();
140  }
141 
143 
144  ClassDefNV(TWriteLockGuard,0) // Exception safe read locking/unlocking of mutex
145 };
146 
147 } // namespace ROOT.
148 
149 // Zero overhead macros in case not compiled with thread support
150 #if defined (_REENTRANT) || defined (WIN32)
151 
152 #define R__READ_LOCKGUARD(mutex) ::ROOT::TReadLockGuard _R__UNIQUE_(R__readguard)(mutex)
153 #define R__READ_LOCKGUARD_NAMED(name,mutex) ::ROOT::TReadLockGuard _NAME2_(R__readguard,name)(mutex)
154 
155 #define R__WRITE_LOCKGUARD(mutex) ::ROOT::TWriteLockGuard _R__UNIQUE_(R__readguard)(mutex)
156 #define R__WRITE_LOCKGUARD_NAMED(name,mutex) ::ROOT::TWriteLockGuard _NAME2_(R__readguard,name)(mutex)
157 
158 #else
159 
160 #define R__READ_LOCKGUARD(mutex) (void)mutex
161 #define R__READ_LOCKGUARD_NAMED(name,mutex) (void)mutex
162 
163 #define R__WRITE_LOCKGUARD(mutex) (void)mutex
164 #define R__WRITE_LOCKGUARD_NAMED(name,mutex) (void)mutex
165 
166 #endif
167 
168 
169 #endif
ROOT::TVirtualRWMutex::Rewind
virtual std::unique_ptr< StateDelta > Rewind(const State &earlierState)=0
ROOT::TReadLockGuard
Definition: TVirtualRWMutex.h:109
ROOT::TVirtualRWMutex::StateDelta
State as returned by GetStateDelta() that can be passed to Restore()
Definition: TVirtualRWMutex.h:70
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:112
ROOT::TVirtualRWMutex::StateAndRecurseCount
Definition: TVirtualRWMutex.h:55
TVirtualMutex
This class implements a mutex interface.
Definition: TVirtualMutex.h:32
ROOT::TReadLockGuard::TReadLockGuard
TReadLockGuard(TVirtualRWMutex *mutex)
Definition: TVirtualRWMutex.h:119
ROOT::TReadLockGuard::~TReadLockGuard
~TReadLockGuard()
Definition: TVirtualRWMutex.h:123
ROOT::gCoreMutex
R__EXTERN TVirtualRWMutex * gCoreMutex
Definition: TVirtualRWMutex.h:32
ROOT::TVirtualRWMutex::StateAndRecurseCount::fState
std::unique_ptr< ROOT::TVirtualRWMutex::State > fState
State of gCoreMutex when the first interpreter-related function was invoked.
Definition: TVirtualRWMutex.h:57
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:39
ROOT::TVirtualRWMutex::TryLock
Int_t TryLock() override
Definition: TVirtualRWMutex.h:80
ROOT::TWriteLockGuard
Definition: TVirtualRWMutex.h:128
ROOT::TVirtualRWMutex::Lock
Int_t Lock() override
Definition: TVirtualRWMutex.h:79
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:132
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
ROOT::TReadLockGuard::operator=
TReadLockGuard & operator=(const TReadLockGuard &)=delete
ROOT::TVirtualRWMutex::StateDelta::~StateDelta
virtual ~StateDelta()
ROOT::TWriteLockGuard::TWriteLockGuard
TWriteLockGuard(TVirtualRWMutex *mutex)
Definition: TVirtualRWMutex.h:138
TVirtualMutex.h
ROOT::TVirtualRWMutex::ReadLock
virtual Hint_t * ReadLock()=0
ROOT::TVirtualRWMutex::UnLock
Int_t UnLock() override
Definition: TVirtualRWMutex.h:81
ROOT::TVirtualRWMutex::CleanUp
Int_t CleanUp() override
Definition: TVirtualRWMutex.h:82
ROOT::TVirtualRWMutex::State::~State
virtual ~State()
ROOT::TVirtualRWMutex::State
Earlier lock state as returned by GetState() that can be passed to Restore()
Definition: TVirtualRWMutex.h:51
ROOT::TVirtualRWMutex::StateAndRecurseCount::fRecurseCount
Int_t fRecurseCount
Interpreter-related functions will push the "entry" lock state to *this.
Definition: TVirtualRWMutex.h:62
R__EXTERN
#define R__EXTERN
Definition: DllImport.h:27
ROOT::TWriteLockGuard::fMutex
TVirtualRWMutex *const fMutex
Definition: TVirtualRWMutex.h:131
ROOT::TWriteLockGuard::~TWriteLockGuard
~TWriteLockGuard()
Definition: TVirtualRWMutex.h:142
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:113
int
ClassDefNV
#define ClassDefNV(name, id)
Definition: Rtypes.h:333