1/*
2 * Copyright (C) 2008-2017 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#pragma once
30
31#include <wtf/Assertions.h>
32#include <wtf/Atomics.h>
33#include <wtf/Noncopyable.h>
34
35namespace WTF {
36
37enum NoLockingNecessaryTag { NoLockingNecessary };
38
39class AbstractLocker {
40 WTF_MAKE_NONCOPYABLE(AbstractLocker);
41public:
42 AbstractLocker(NoLockingNecessaryTag)
43 {
44 }
45
46protected:
47 AbstractLocker()
48 {
49 }
50};
51
52template <typename T> class Locker : public AbstractLocker {
53public:
54 explicit Locker(T& lockable) : m_lockable(&lockable) { lock(); }
55 explicit Locker(T* lockable) : m_lockable(lockable) { lock(); }
56
57 // You should be wary of using this constructor. It's only applicable
58 // in places where there is a locking protocol for a particular object
59 // but it's not necessary to engage in that protocol yet. For example,
60 // this often happens when an object is newly allocated and it can not
61 // be accessed concurrently.
62 Locker(NoLockingNecessaryTag) : m_lockable(nullptr) { }
63
64 Locker(int) = delete;
65
66 ~Locker()
67 {
68 compilerFence();
69 if (m_lockable)
70 m_lockable->unlock();
71 }
72
73 static Locker tryLock(T& lockable)
74 {
75 Locker result(NoLockingNecessary);
76 if (lockable.tryLock()) {
77 result.m_lockable = &lockable;
78 return result;
79 }
80 return result;
81 }
82
83 explicit operator bool() const { return !!m_lockable; }
84
85 void unlockEarly()
86 {
87 m_lockable->unlock();
88 m_lockable = 0;
89 }
90
91 // It's great to be able to pass lockers around. It enables custom locking adaptors like
92 // JSC::LockDuringMarking.
93 Locker(Locker&& other)
94 : m_lockable(other.m_lockable)
95 {
96 other.m_lockable = nullptr;
97 }
98
99 Locker& operator=(Locker&& other)
100 {
101 if (m_lockable)
102 m_lockable->unlock();
103 m_lockable = other.m_lockable;
104 other.m_lockable = nullptr;
105 return *this;
106 }
107
108private:
109 void lock()
110 {
111 if (m_lockable)
112 m_lockable->lock();
113 compilerFence();
114 }
115
116 T* m_lockable;
117};
118
119// Use this lock scope like so:
120// auto locker = holdLock(lock);
121template<typename LockType>
122Locker<LockType> holdLock(LockType& lock)
123{
124 return Locker<LockType>(lock);
125}
126
127template<typename LockType>
128Locker<LockType> holdLockIf(LockType& lock, bool predicate)
129{
130 return Locker<LockType>(predicate ? &lock : nullptr);
131}
132
133template<typename LockType>
134Locker<LockType> tryHoldLock(LockType& lock)
135{
136 return Locker<LockType>::tryLock(lock);
137}
138
139}
140
141using WTF::AbstractLocker;
142using WTF::Locker;
143using WTF::NoLockingNecessaryTag;
144using WTF::NoLockingNecessary;
145using WTF::holdLock;
146using WTF::holdLockIf;
147