2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_LOCK_H_
18 #define incl_HPHP_LOCK_H_
20 #include "hphp/util/mutex.h"
21 #include "hphp/util/synchronizable.h"
22 #include "hphp/util/synchronizable-multi.h"
25 ///////////////////////////////////////////////////////////////////////////////
28 * Lock instrumentation for mutex stats.
31 typedef void (*PFUNC_PROFILE
)(const std::string
&stack
, int64_t elapsed_us
);
32 static PFUNC_PROFILE s_pfunc_profile
;
33 static bool s_profile
;
34 static int s_profile_sampling
;
36 explicit LockProfiler(bool profile
);
44 ///////////////////////////////////////////////////////////////////////////////
46 template <typename MutexT
>
47 struct BaseConditionalLock
{
48 BaseConditionalLock(MutexT
&mutex
, bool condition
, bool profile
= true)
49 : m_profiler(profile
), m_mutex(mutex
), m_acquired(false) {
51 m_mutex
.lock(); // must not throw
55 ~BaseConditionalLock() {
57 m_mutex
.unlock(); // must not throw
61 LockProfiler m_profiler
;
66 struct ConditionalLock
: BaseConditionalLock
<Mutex
> {
67 ConditionalLock(Mutex
&mutex
,
68 bool condition
, bool profile
= true)
69 : BaseConditionalLock
<Mutex
>(mutex
, condition
, profile
)
71 ConditionalLock(Synchronizable
*obj
,
72 bool condition
, bool profile
= true)
73 : BaseConditionalLock
<Mutex
>(obj
->getMutex(), condition
, profile
)
75 ConditionalLock(SynchronizableMulti
*obj
,
76 bool condition
, bool profile
= true)
77 : BaseConditionalLock
<Mutex
>(obj
->getMutex(), condition
, profile
)
82 * Just a helper class that automatically unlocks a mutex when it goes out of
90 struct Lock
: ConditionalLock
{
91 explicit Lock(Mutex
&mutex
, bool profile
= true)
92 : ConditionalLock(mutex
, true, profile
) {}
93 explicit Lock(Synchronizable
*obj
, bool profile
= true)
94 : ConditionalLock(obj
, true, profile
) {}
95 explicit Lock(SynchronizableMulti
*obj
, bool profile
= true)
96 : ConditionalLock(obj
, true, profile
) {}
100 explicit ScopedUnlock(Mutex
&mutex
) : m_mutex(mutex
) {
103 explicit ScopedUnlock(Synchronizable
*obj
) : m_mutex(obj
->getMutex()) {
106 explicit ScopedUnlock(SynchronizableMulti
*obj
) : m_mutex(obj
->getMutex()) {
118 struct SimpleConditionalLock
: BaseConditionalLock
<SimpleMutex
> {
119 SimpleConditionalLock(SimpleMutex
&mutex
,
120 bool condition
, bool profile
= true)
121 : BaseConditionalLock
<SimpleMutex
>(mutex
, condition
, profile
)
124 mutex
.assertOwnedBySelf();
129 struct SimpleLock
: SimpleConditionalLock
{
130 explicit SimpleLock(SimpleMutex
&mutex
, bool profile
= true)
131 : SimpleConditionalLock(mutex
, true, profile
) {}
134 ///////////////////////////////////////////////////////////////////////////////
137 explicit ReadLock(ReadWriteMutex
& mutex
, bool profile
= true)
138 : m_profiler(profile
)
141 m_mutex
.acquireRead();
144 ReadLock(const ReadLock
&) = delete;
145 ReadLock
& operator=(const ReadLock
&) = delete;
152 LockProfiler m_profiler
;
153 ReadWriteMutex
& m_mutex
;
157 explicit WriteLock(ReadWriteMutex
& mutex
, bool profile
= true)
158 : m_profiler(profile
)
161 m_mutex
.acquireWrite();
164 WriteLock(const WriteLock
&) = delete;
165 WriteLock
& operator=(const WriteLock
&) = delete;
172 LockProfiler m_profiler
;
173 ReadWriteMutex
& m_mutex
;
176 ///////////////////////////////////////////////////////////////////////////////
179 #endif // incl_HPHP_LOCK_H_