ROOT  6.06/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 #ifndef ROOT_TObject
26 #include "TObject.h"
27 #endif
28 
30 
31 // Global mutex set in TThread::Init
33 
34 class TVirtualMutex : public TObject {
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) if (mutex) { }
107 #define R__LOCKGUARD_NAMED(name,mutex) if (mutex) { }
108 #define R__LOCKGUARD2(mutex) if (mutex) { }
109 #define R__LOCKGUARD_UNLOCK(name) { }
110 #endif
111 
112 #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
TLockGuard(const TLockGuard &)
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
#define ClassDef(name, id)
Definition: Rtypes.h:254
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:262
Int_t Release()
Definition: TVirtualMutex.h:45
TVirtualMutex(Bool_t=kFALSE)
Definition: TVirtualMutex.h:37
Mother of all ROOT objects.
Definition: TObject.h:58
TLockGuard & operator=(const TLockGuard &)
#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