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 +----------------------------------------------------------------------+
19 #include "hphp/util/mutex.h"
20 #include "hphp/util/synchronizable.h"
21 #include "hphp/util/synchronizable-multi.h"
24 ///////////////////////////////////////////////////////////////////////////////
27 * Lock instrumentation for mutex stats.
30 typedef void (*PFUNC_PROFILE
)(const std::string
&stack
, int64_t elapsed_us
);
31 static PFUNC_PROFILE s_pfunc_profile
;
32 static bool s_profile
;
33 static int s_profile_sampling
;
35 explicit LockProfiler(bool profile
);
43 ///////////////////////////////////////////////////////////////////////////////
45 template <typename MutexT
>
46 struct BaseConditionalLock
{
47 BaseConditionalLock(MutexT
&mutex
, bool condition
, bool profile
= true)
48 : m_profiler(profile
), m_mutex(mutex
), m_acquired(false) {
50 m_mutex
.lock(); // must not throw
54 ~BaseConditionalLock() {
56 m_mutex
.unlock(); // must not throw
60 LockProfiler m_profiler
;
65 struct ConditionalLock
: BaseConditionalLock
<Mutex
> {
66 ConditionalLock(Mutex
&mutex
,
67 bool condition
, bool profile
= true)
68 : BaseConditionalLock
<Mutex
>(mutex
, condition
, profile
)
70 ConditionalLock(Synchronizable
*obj
,
71 bool condition
, bool profile
= true)
72 : BaseConditionalLock
<Mutex
>(obj
->getMutex(), condition
, profile
)
74 ConditionalLock(SynchronizableMulti
*obj
,
75 bool condition
, bool profile
= true)
76 : BaseConditionalLock
<Mutex
>(obj
->getMutex(), condition
, profile
)
81 * Just a helper class that automatically unlocks a mutex when it goes out of
89 struct Lock
: ConditionalLock
{
90 explicit Lock(Mutex
&mutex
, bool profile
= true)
91 : ConditionalLock(mutex
, true, profile
) {}
92 explicit Lock(Synchronizable
*obj
, bool profile
= true)
93 : ConditionalLock(obj
, true, profile
) {}
94 explicit Lock(SynchronizableMulti
*obj
, bool profile
= true)
95 : ConditionalLock(obj
, true, profile
) {}
99 explicit ScopedUnlock(Mutex
&mutex
) : m_mutex(mutex
) {
102 explicit ScopedUnlock(Synchronizable
*obj
) : m_mutex(obj
->getMutex()) {
105 explicit ScopedUnlock(SynchronizableMulti
*obj
) : m_mutex(obj
->getMutex()) {
117 struct SimpleConditionalLock
: BaseConditionalLock
<SimpleMutex
> {
118 SimpleConditionalLock(SimpleMutex
&mutex
,
119 bool condition
, bool profile
= true)
120 : BaseConditionalLock
<SimpleMutex
>(mutex
, condition
, profile
)
123 mutex
.assertOwnedBySelf();
128 struct SimpleLock
: SimpleConditionalLock
{
129 explicit SimpleLock(SimpleMutex
&mutex
, bool profile
= true)
130 : SimpleConditionalLock(mutex
, true, profile
) {}
133 ///////////////////////////////////////////////////////////////////////////////
136 explicit ReadLock(ReadWriteMutex
& mutex
, bool profile
= true)
137 : m_profiler(profile
)
140 m_mutex
.acquireRead();
143 ReadLock(const ReadLock
&) = delete;
144 ReadLock
& operator=(const ReadLock
&) = delete;
151 LockProfiler m_profiler
;
152 ReadWriteMutex
& m_mutex
;
156 explicit WriteLock(ReadWriteMutex
& mutex
, bool profile
= true)
157 : m_profiler(profile
)
160 m_mutex
.acquireWrite();
163 WriteLock(const WriteLock
&) = delete;
164 WriteLock
& operator=(const WriteLock
&) = delete;
171 LockProfiler m_profiler
;
172 ReadWriteMutex
& m_mutex
;
175 ///////////////////////////////////////////////////////////////////////////////