Logo ROOT   6.12/07
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 "TObject.h"
26 
27 #include <memory>
28 
30 
31 // Global mutex set in TThread::Init
33 
35 
36 public:
37  TVirtualMutex(Bool_t /* recursive */ = kFALSE) { }
38  virtual ~TVirtualMutex() { }
39 
40  virtual Int_t Lock() = 0;
41  virtual Int_t TryLock() = 0;
42  virtual Int_t UnLock() = 0;
43  virtual Int_t CleanUp() = 0;
44  Int_t Acquire() { return Lock(); }
45  Int_t Release() { return UnLock(); }
46 
47  virtual TVirtualMutex *Factory(Bool_t /*recursive*/ = kFALSE) = 0;
48 
49  ClassDef(TVirtualMutex, 0) // Virtual mutex lock class
50 };
51 
52 
53 //////////////////////////////////////////////////////////////////////////
54 // //
55 // TLockGuard //
56 // //
57 // This class provides mutex resource management in a guaranteed and //
58 // exception safe way. Use like this: //
59 // { //
60 // TLockGuard guard(mutex); //
61 // ... // do something //
62 // } //
63 // when guard goes out of scope the mutex is unlocked in the TLockGuard //
64 // destructor. The exception mechanism takes care of calling the dtors //
65 // of local objects so it is exception safe. //
66 // //
67 //////////////////////////////////////////////////////////////////////////
68 
69 class TLockGuard {
70 
71 private:
73 
74  TLockGuard(const TLockGuard&); // not implemented
75  TLockGuard& operator=(const TLockGuard&); // not implemented
76 
77 public:
79  : fMutex(mutex) { if (fMutex) fMutex->Lock(); }
81  if (!fMutex) return 0;
82  auto tmp = fMutex;
83  fMutex = 0;
84  return tmp->UnLock();
85  }
86  ~TLockGuard() { if (fMutex) fMutex->UnLock(); }
87 
88  ClassDefNV(TLockGuard,0) // Exception safe locking/unlocking of mutex
89 };
90 
91 // Zero overhead macros in case not compiled with thread support
92 #if defined (_REENTRANT) || defined (WIN32)
93 
94 #define R__LOCKGUARD(mutex) TLockGuard _R__UNIQUE_(R__guard)(mutex)
95 #define R__LOCKGUARD2(mutex) \
96  if (gGlobalMutex && !mutex) { \
97  gGlobalMutex->Lock(); \
98  if (!mutex) \
99  mutex = gGlobalMutex->Factory(kTRUE); \
100  gGlobalMutex->UnLock(); \
101  } \
102  R__LOCKGUARD(mutex)
103 #define R__LOCKGUARD_NAMED(name,mutex) TLockGuard _NAME2_(R__guard,name)(mutex)
104 #define R__LOCKGUARD_UNLOCK(name) _NAME2_(R__guard,name).UnLock()
105 #else
106 #define R__LOCKGUARD(mutex) (void)mutex; { }
107 #define R__LOCKGUARD_NAMED(name,mutex) (void)mutex; { }
108 #define R__LOCKGUARD2(mutex) (void)mutex; { }
109 #define R__LOCKGUARD_UNLOCK(name) { }
110 #endif
111 
112 #ifdef R__USE_IMT
113 #define R__LOCKGUARD_IMT(mutex) if (ROOT::Internal::IsParBranchProcessingEnabled()) R__LOCKGUARD(mutex)
114 #define R__LOCKGUARD_IMT2(mutex) if (ROOT::Internal::IsParBranchProcessingEnabled()) R__LOCKGUARD2(mutex)
115 #else
116 #define R__LOCKGUARD_IMT(mutex) { }
117 #define R__LOCKGUARD_IMT2(mutex) { }
118 #endif
119 
120 #ifdef R__USE_IMT
121 #define R__RWLOCK_ACQUIRE_READ(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.ReadLock();
122 #define R__RWLOCK_RELEASE_READ(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.ReadUnLock();
123 #define R__RWLOCK_ACQUIRE_WRITE(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.WriteLock();
124 #define R__RWLOCK_RELEASE_WRITE(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.WriteUnLock();
125 #else
126 #define R__RWLOCK_ACQUIRE_READ(rwlock) { }
127 #define R__RWLOCK_RELEASE_READ(rwlock) { }
128 #define R__RWLOCK_ACQUIRE_WRITE(rwlock) { }
129 #define R__RWLOCK_RELEASE_WRITE(rwlock) { }
130 #endif
131 
132 #endif
virtual TVirtualMutex * Factory(Bool_t=kFALSE)=0
TVirtualMutex * fMutex
Definition: TVirtualMutex.h:72
virtual Int_t UnLock()=0
virtual Int_t TryLock()=0
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
#define ClassDef(name, id)
Definition: Rtypes.h:320
virtual Int_t Lock()=0
Int_t Acquire()
Definition: TVirtualMutex.h:44
R__EXTERN TVirtualMutex * gGlobalMutex
Definition: TVirtualMutex.h:29
TLockGuard(TVirtualMutex *mutex)
Definition: TVirtualMutex.h:78
#define ClassDefNV(name, id)
Definition: Rtypes.h:328
const Bool_t kFALSE
Definition: RtypesCore.h:88
Int_t Release()
Definition: TVirtualMutex.h:45
TVirtualMutex(Bool_t=kFALSE)
Definition: TVirtualMutex.h:37
Binding & operator=(OUT(*fun)(void))
#define R__EXTERN
Definition: DllImport.h:27
virtual ~TVirtualMutex()
Definition: TVirtualMutex.h:38
virtual Int_t CleanUp()=0
Int_t UnLock()
Definition: TVirtualMutex.h:80