Logo ROOT   6.10/09
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 
28 
29 // Global mutex set in TThread::Init
31 
32 class TVirtualMutex : public TObject {
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  }
84  ~TLockGuard() { if (fMutex) fMutex->UnLock(); }
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) if (mutex) { }
105 #define R__LOCKGUARD_NAMED(name,mutex) if (mutex) { }
106 #define R__LOCKGUARD2(mutex) if (mutex) { }
107 #define R__LOCKGUARD_UNLOCK(name) { }
108 #endif
109 
110 #ifdef R__USE_IMT
111 #define R__LOCKGUARD_IMT(mutex) if (ROOT::Internal::IsParBranchProcessingEnabled()) R__LOCKGUARD(mutex)
112 #define R__LOCKGUARD_IMT2(mutex) if (ROOT::Internal::IsParBranchProcessingEnabled()) R__LOCKGUARD2(mutex)
113 #else
114 #define R__LOCKGUARD_IMT(mutex) { }
115 #define R__LOCKGUARD_IMT2(mutex) { }
116 #endif
117 
118 #ifdef R__USE_IMT
119 #define R__RWLOCK_ACQUIRE_READ(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.ReadLock();
120 #define R__RWLOCK_RELEASE_READ(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.ReadUnLock();
121 #define R__RWLOCK_ACQUIRE_WRITE(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.WriteLock();
122 #define R__RWLOCK_RELEASE_WRITE(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.WriteUnLock();
123 #else
124 #define R__RWLOCK_ACQUIRE_READ(rwlock) { }
125 #define R__RWLOCK_RELEASE_READ(rwlock) { }
126 #define R__RWLOCK_ACQUIRE_WRITE(rwlock) { }
127 #define R__RWLOCK_RELEASE_WRITE(rwlock) { }
128 #endif
129 
130 #endif
virtual TVirtualMutex * Factory(Bool_t=kFALSE)=0
TVirtualMutex * fMutex
Definition: TVirtualMutex.h:70
virtual Int_t UnLock()=0
virtual Int_t TryLock()=0
This class implements a mutex interface.
Definition: TVirtualMutex.h:32
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
#define ClassDef(name, id)
Definition: Rtypes.h:297
virtual Int_t Lock()=0
Int_t Acquire()
Definition: TVirtualMutex.h:42
TObject & operator=(const TObject &rhs)
TObject assignment operator.
Definition: TObject.h:250
R__EXTERN TVirtualMutex * gGlobalMutex
Definition: TVirtualMutex.h:27
TLockGuard(TVirtualMutex *mutex)
Definition: TVirtualMutex.h:76
#define ClassDefNV(name, id)
Definition: Rtypes.h:305
const Bool_t kFALSE
Definition: RtypesCore.h:92
Int_t Release()
Definition: TVirtualMutex.h:43
TVirtualMutex(Bool_t=kFALSE)
Definition: TVirtualMutex.h:35
Mother of all ROOT objects.
Definition: TObject.h:37
#define R__EXTERN
Definition: DllImport.h:27
virtual ~TVirtualMutex()
Definition: TVirtualMutex.h:36
virtual Int_t CleanUp()=0
Int_t UnLock()
Definition: TVirtualMutex.h:78