Logo ROOT  
Reference Guide
TVirtualMutex.h
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Fons Rademakers 14/07/2002
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2002, 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_TVirtualMutex
13 #define ROOT_TVirtualMutex
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TVirtualMutex //
19 // //
20 // This class implements a mutex interface. The actual work is done via //
21 // TMutex which is available as soon as the thread library is loaded. //
22 // //
23 //////////////////////////////////////////////////////////////////////////
24 
25 #include "Rtypes.h"
26 
27 class TVirtualMutex;
28 
29 // Global mutex set in TThread::Init
31 
33 
34 public:
35  TVirtualMutex(Bool_t /* recursive */ = kFALSE) { }
36  virtual ~TVirtualMutex() { }
37 
38  virtual Int_t Lock() = 0;
39  virtual Int_t TryLock() = 0;
40  virtual Int_t UnLock() = 0;
41  virtual Int_t CleanUp() = 0;
42  Int_t Acquire() { return Lock(); }
43  Int_t Release() { return UnLock(); }
44 
45  virtual TVirtualMutex *Factory(Bool_t /*recursive*/ = kFALSE) = 0;
46 
47  ClassDef(TVirtualMutex, 0) // Virtual mutex lock class
48 };
49 
50 
51 //////////////////////////////////////////////////////////////////////////
52 // //
53 // TLockGuard //
54 // //
55 // This class provides mutex resource management in a guaranteed and //
56 // exception safe way. Use like this: //
57 // { //
58 // TLockGuard guard(mutex); //
59 // ... // do something //
60 // } //
61 // when guard goes out of scope the mutex is unlocked in the TLockGuard //
62 // destructor. The exception mechanism takes care of calling the dtors //
63 // of local objects so it is exception safe. //
64 // //
65 //////////////////////////////////////////////////////////////////////////
66 
67 class TLockGuard {
68 
69 private:
71 
72  TLockGuard(const TLockGuard&); // not implemented
73  TLockGuard& operator=(const TLockGuard&); // not implemented
74 
75 public:
77  : fMutex(mutex) { if (fMutex) fMutex->Lock(); }
79  if (!fMutex) return 0;
80  auto tmp = fMutex;
81  fMutex = 0;
82  return tmp->UnLock();
83  }
85 
86  ClassDefNV(TLockGuard,0) // Exception safe locking/unlocking of mutex
87 };
88 
89 // Zero overhead macros in case not compiled with thread support
90 #if defined (_REENTRANT) || defined (WIN32)
91 
92 #define R__LOCKGUARD(mutex) TLockGuard _R__UNIQUE_(R__guard)(mutex)
93 #define R__LOCKGUARD2(mutex) \
94  if (gGlobalMutex && !mutex) { \
95  gGlobalMutex->Lock(); \
96  if (!mutex) \
97  mutex = gGlobalMutex->Factory(kTRUE); \
98  gGlobalMutex->UnLock(); \
99  } \
100  R__LOCKGUARD(mutex)
101 #define R__LOCKGUARD_NAMED(name,mutex) TLockGuard _NAME2_(R__guard,name)(mutex)
102 #define R__LOCKGUARD_UNLOCK(name) _NAME2_(R__guard,name).UnLock()
103 #else
104 #define R__LOCKGUARD(mutex) (void)(mutex); { }
105 #define R__LOCKGUARD_NAMED(name,mutex) (void)(mutex); { }
106 #define R__LOCKGUARD2(mutex) (void)(mutex); { }
107 #define R__LOCKGUARD_UNLOCK(name) { }
108 #endif
109 
110 #ifdef R__USE_IMT
111 #define R__LOCKGUARD_IMT(mutex) R__LOCKGUARD(ROOT::Internal::IsParBranchProcessingEnabled() ? mutex : nullptr)
112 #define R__LOCKGUARD_IMT2(mutex) \
113  if (gGlobalMutex && !mutex && ROOT::Internal::IsParBranchProcessingEnabled()) { \
114  gGlobalMutex->Lock(); \
115  if (!mutex) \
116  mutex = gGlobalMutex->Factory(kTRUE); \
117  gGlobalMutex->UnLock(); \
118  } \
119  R__LOCKGUARD_IMT(mutex)
120 #else
121 #define R__LOCKGUARD_IMT(mutex) { }
122 #define R__LOCKGUARD_IMT2(mutex) { }
123 #endif
124 
125 #endif
TVirtualMutex::Factory
virtual TVirtualMutex * Factory(Bool_t=kFALSE)=0
TLockGuard
Definition: TVirtualMutex.h:67
TVirtualMutex::Acquire
Int_t Acquire()
Definition: TVirtualMutex.h:42
TVirtualMutex::TryLock
virtual Int_t TryLock()=0
TVirtualMutex
This class implements a mutex interface.
Definition: TVirtualMutex.h:32
TVirtualMutex::~TVirtualMutex
virtual ~TVirtualMutex()
Definition: TVirtualMutex.h:36
gGlobalMutex
R__EXTERN TVirtualMutex * gGlobalMutex
Definition: TVirtualMutex.h:27
TVirtualMutex::TVirtualMutex
TVirtualMutex(Bool_t=kFALSE)
Definition: TVirtualMutex.h:35
bool
TLockGuard::fMutex
TVirtualMutex * fMutex
Definition: TVirtualMutex.h:70
TLockGuard::TLockGuard
TLockGuard(const TLockGuard &)
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TLockGuard::~TLockGuard
~TLockGuard()
Definition: TVirtualMutex.h:84
TVirtualMutex::Lock
virtual Int_t Lock()=0
TLockGuard::TLockGuard
TLockGuard(TVirtualMutex *mutex)
Definition: TVirtualMutex.h:76
TVirtualMutex::Release
Int_t Release()
Definition: TVirtualMutex.h:43
TVirtualMutex::UnLock
virtual Int_t UnLock()=0
TLockGuard::operator=
TLockGuard & operator=(const TLockGuard &)
ClassDef
#define ClassDef(name, id)
Definition: Rtypes.h:325
R__EXTERN
#define R__EXTERN
Definition: DllImport.h:27
TVirtualMutex::CleanUp
virtual Int_t CleanUp()=0
Rtypes.h
TLockGuard::UnLock
Int_t UnLock()
Definition: TVirtualMutex.h:78
int
ClassDefNV
#define ClassDefNV(name, id)
Definition: Rtypes.h:333