Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
27class TVirtualMutex;
28
29// Global mutex set in TThread::Init
31
33
34public:
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// where mutex is a pointer to a TMutex object. //
62// When guard goes out of scope the mutex is unlocked in the TLockGuard //
63// destructor. The exception mechanism takes care of calling the dtors //
64// of local objects so it is exception safe. //
65// In contrast to std::lock_guard, TLockGuard constructor expects a //
66// pointer, not the mutex object itself. //
67// //
68//////////////////////////////////////////////////////////////////////////
69
71
72private:
74
75 TLockGuard(const TLockGuard&) = delete;
76 TLockGuard& operator=(const TLockGuard&) = delete;
77
78public:
80 : fMutex(mutex) { if (fMutex) fMutex->Lock(); }
82 if (!fMutex) return 0;
83 auto tmp = fMutex;
84 fMutex = 0;
85 return tmp->UnLock();
86 }
88
89 ClassDefNV(TLockGuard,0) // Exception safe locking/unlocking of mutex
90};
91
92// Zero overhead macros in case not compiled with thread support (-pthread)
93// Use with a trailing semicolon and pass a pointer as argument, e.g.:
94// TMutex m; R__LOCKGUARD(&m);
95// Warning: if program is compiled without pthread support, _REENTRANT will
96// be undefined and the macro has (silently) no effect, no locks are performed.
97#if defined (_REENTRANT) || defined (WIN32)
98
99#define R__LOCKGUARD(mutex) TLockGuard _R__UNIQUE_(R__guard)(mutex)
100#define R__LOCKGUARD2(mutex) \
101 if (gGlobalMutex && !mutex) { \
102 gGlobalMutex->Lock(); \
103 if (!mutex) \
104 mutex = gGlobalMutex->Factory(kTRUE); \
105 gGlobalMutex->UnLock(); \
106 } \
107 R__LOCKGUARD(mutex)
108#define R__LOCKGUARD_NAMED(name,mutex) TLockGuard _NAME2_(R__guard,name)(mutex)
109#define R__LOCKGUARD_UNLOCK(name) _NAME2_(R__guard,name).UnLock()
110#else
111//@todo: mutex is not checked to be of type TVirtualMutex*.
112#define R__LOCKGUARD(mutex) (void)(mutex); { }
113#define R__LOCKGUARD_NAMED(name,mutex) (void)(mutex); { }
114#define R__LOCKGUARD2(mutex) (void)(mutex); { }
115#define R__LOCKGUARD_UNLOCK(name) { }
116#endif
117
118#ifdef R__USE_IMT
119#define R__LOCKGUARD_IMT(mutex) R__LOCKGUARD(ROOT::Internal::IsParBranchProcessingEnabled() ? mutex : nullptr)
120#define R__LOCKGUARD_IMT2(mutex) \
121 if (gGlobalMutex && !mutex && ROOT::Internal::IsParBranchProcessingEnabled()) { \
122 gGlobalMutex->Lock(); \
123 if (!mutex) \
124 mutex = gGlobalMutex->Factory(kTRUE); \
125 gGlobalMutex->UnLock(); \
126 } \
127 R__LOCKGUARD_IMT(mutex)
128#else
129#define R__LOCKGUARD_IMT(mutex) { }
130#define R__LOCKGUARD_IMT2(mutex) { }
131#endif
132
133#endif
#define R__EXTERN
Definition DllImport.h:27
const Bool_t kFALSE
Definition RtypesCore.h:101
#define ClassDef(name, id)
Definition Rtypes.h:325
#define ClassDefNV(name, id)
Definition Rtypes.h:333
R__EXTERN TVirtualMutex * gGlobalMutex
TLockGuard & operator=(const TLockGuard &)=delete
Int_t UnLock()
TLockGuard(const TLockGuard &)=delete
TLockGuard(TVirtualMutex *mutex)
TVirtualMutex * fMutex
This class implements a mutex interface.
virtual ~TVirtualMutex()
TVirtualMutex(Bool_t=kFALSE)
virtual Int_t UnLock()=0
virtual Int_t Lock()=0
virtual Int_t CleanUp()=0
virtual Int_t TryLock()=0
virtual TVirtualMutex * Factory(Bool_t=kFALSE)=0