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 = nullptr;
85 return tmp->UnLock();
86 }
88
89 ClassDefNV(TLockGuard,0) // Exception safe locking/unlocking of mutex
90};
91
92// Use with a trailing semicolon and pass a pointer as argument, e.g.:
93// TMutex m; R__LOCKGUARD(&m);
94
95#define R__LOCKGUARD(mutex) TLockGuard _R__UNIQUE_(R__guard)(mutex)
96#define R__LOCKGUARD2(mutex) \
97 if (gGlobalMutex && !mutex) { \
98 gGlobalMutex->Lock(); \
99 if (!mutex) \
100 mutex = gGlobalMutex->Factory(kTRUE); \
101 gGlobalMutex->UnLock(); \
102 } \
103 R__LOCKGUARD(mutex)
104#define R__LOCKGUARD_NAMED(name,mutex) TLockGuard _NAME2_(R__guard,name)(mutex)
105#define R__LOCKGUARD_UNLOCK(name) _NAME2_(R__guard,name).UnLock()
106
107#ifdef R__USE_IMT
108#define R__LOCKGUARD_IMT(mutex) R__LOCKGUARD(ROOT::Internal::IsParBranchProcessingEnabled() ? mutex : nullptr)
109#define R__LOCKGUARD_IMT2(mutex) \
110 if (gGlobalMutex && !mutex && ROOT::Internal::IsParBranchProcessingEnabled()) { \
111 gGlobalMutex->Lock(); \
112 if (!mutex) \
113 mutex = gGlobalMutex->Factory(kTRUE); \
114 gGlobalMutex->UnLock(); \
115 } \
116 R__LOCKGUARD_IMT(mutex)
117#else
118#define R__LOCKGUARD_IMT(mutex) { }
119#define R__LOCKGUARD_IMT2(mutex) { }
120#endif
121
122#endif
#define R__EXTERN
Definition DllImport.h:26
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
#define ClassDef(name, id)
Definition Rtypes.h:342
#define ClassDefNV(name, id)
Definition Rtypes.h:350
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