Logo ROOT  
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
27#include <memory>
28
29class TVirtualMutex;
30
31// Global mutex set in TThread::Init
33
35
36public:
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
70
71private:
73
74 TLockGuard(const TLockGuard&); // not implemented
75 TLockGuard& operator=(const TLockGuard&); // not implemented
76
77public:
79 : fMutex(mutex) { if (fMutex) fMutex->Lock(); }
81 if (!fMutex) return 0;
82 auto tmp = fMutex;
83 fMutex = 0;
84 return tmp->UnLock();
85 }
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) (void)(mutex); { }
107#define R__LOCKGUARD_NAMED(name,mutex) (void)(mutex); { }
108#define R__LOCKGUARD2(mutex) (void)(mutex); { }
109#define R__LOCKGUARD_UNLOCK(name) { }
110#endif
111
112#ifdef R__USE_IMT
113#define R__LOCKGUARD_IMT(mutex) R__LOCKGUARD(ROOT::Internal::IsParBranchProcessingEnabled() ? mutex : nullptr)
114#define R__LOCKGUARD_IMT2(mutex) \
115 if (gGlobalMutex && !mutex && ROOT::Internal::IsParBranchProcessingEnabled()) { \
116 gGlobalMutex->Lock(); \
117 if (!mutex) \
118 mutex = gGlobalMutex->Factory(kTRUE); \
119 gGlobalMutex->UnLock(); \
120 } \
121 R__LOCKGUARD_IMT(mutex)
122#else
123#define R__LOCKGUARD_IMT(mutex) { }
124#define R__LOCKGUARD_IMT2(mutex) { }
125#endif
126
127#ifdef R__USE_IMT
128#define R__RWLOCK_ACQUIRE_READ(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.ReadLock();
129#define R__RWLOCK_RELEASE_READ(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.ReadUnLock();
130#define R__RWLOCK_ACQUIRE_WRITE(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.WriteLock();
131#define R__RWLOCK_RELEASE_WRITE(rwlock) if (ROOT::Internal::IsParTreeProcessingEnabled()) rwlock.WriteUnLock();
132#else
133#define R__RWLOCK_ACQUIRE_READ(rwlock) { }
134#define R__RWLOCK_RELEASE_READ(rwlock) { }
135#define R__RWLOCK_ACQUIRE_WRITE(rwlock) { }
136#define R__RWLOCK_RELEASE_WRITE(rwlock) { }
137#endif
138
139#endif
#define R__EXTERN
Definition: DllImport.h:27
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
#define ClassDef(name, id)
Definition: Rtypes.h:326
#define ClassDefNV(name, id)
Definition: Rtypes.h:334
R__EXTERN TVirtualMutex * gGlobalMutex
Definition: TVirtualMutex.h:29
TLockGuard & operator=(const TLockGuard &)
TLockGuard(const TLockGuard &)
Int_t UnLock()
Definition: TVirtualMutex.h:80
TLockGuard(TVirtualMutex *mutex)
Definition: TVirtualMutex.h:78
TVirtualMutex * fMutex
Definition: TVirtualMutex.h:72
This class implements a mutex interface.
Definition: TVirtualMutex.h:34
Int_t Release()
Definition: TVirtualMutex.h:45
virtual ~TVirtualMutex()
Definition: TVirtualMutex.h:38
TVirtualMutex(Bool_t=kFALSE)
Definition: TVirtualMutex.h:37
virtual Int_t UnLock()=0
virtual Int_t Lock()=0
virtual Int_t CleanUp()=0
Int_t Acquire()
Definition: TVirtualMutex.h:44
virtual Int_t TryLock()=0
virtual TVirtualMutex * Factory(Bool_t=kFALSE)=0