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// 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
68
69private:
71
72 TLockGuard(const TLockGuard&) = delete;
73 TLockGuard& operator=(const TLockGuard&) = delete;
74
75public:
77 : fMutex(mutex) { if (fMutex) fMutex->Lock(); }
79 if (!fMutex) return 0;
80 auto tmp = fMutex;
81 fMutex = 0;
82 return tmp->UnLock();
83 }
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) (void)(mutex); { }
105#define R__LOCKGUARD_NAMED(name,mutex) (void)(mutex); { }
106#define R__LOCKGUARD2(mutex) (void)(mutex); { }
107#define R__LOCKGUARD_UNLOCK(name) { }
108#endif
109
110#ifdef R__USE_IMT
111#define R__LOCKGUARD_IMT(mutex) R__LOCKGUARD(ROOT::Internal::IsParBranchProcessingEnabled() ? mutex : nullptr)
112#define R__LOCKGUARD_IMT2(mutex) \
113 if (gGlobalMutex && !mutex && ROOT::Internal::IsParBranchProcessingEnabled()) { \
114 gGlobalMutex->Lock(); \
115 if (!mutex) \
116 mutex = gGlobalMutex->Factory(kTRUE); \
117 gGlobalMutex->UnLock(); \
118 } \
119 R__LOCKGUARD_IMT(mutex)
120#else
121#define R__LOCKGUARD_IMT(mutex) { }
122#define R__LOCKGUARD_IMT2(mutex) { }
123#endif
124
125#endif
#define R__EXTERN
Definition DllImport.h:27
const Bool_t kFALSE
Definition RtypesCore.h:92
#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