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/Compiler.h>
34#include <wtf/Noncopyable.h>
35
36namespace WTF {
37
38enum NoLockingNecessaryTag { NoLockingNecessary };
39
40class AbstractLocker {
41 WTF_MAKE_NONCOPYABLE(AbstractLocker);
42public:
43 AbstractLocker(NoLockingNecessaryTag)
44 {
45 }
46
47protected:
48 AbstractLocker()
49 {
50 }
51};
52
53template <typename T> class Locker : public AbstractLocker {
54public:
55 explicit Locker(T& lockable) : m_lockable(&lockable) { lock(); }
56 explicit Locker(T* lockable) : m_lockable(lockable) { lock(); }
57
58 // You should be wary of using this constructor. It's only applicable
59 // in places where there is a locking protocol for a particular object
60 // but it's not necessary to engage in that protocol yet. For example,
61 // this often happens when an object is newly allocated and it can not
62 // be accessed concurrently.
63 Locker(NoLockingNecessaryTag) : m_lockable(nullptr) { }
64
65 Locker(int) = delete;
66
67 ~Locker()
68 {
69 compilerFence();
70 if (m_lockable)
71 m_lockable->unlock();
72 }
73
74 static Locker tryLock(T& lockable)
75 {
76 Locker result(NoLockingNecessary);
77 if (lockable.tryLock()) {
78 result.m_lockable = &lockable;
79 return result;
80 }
81 return result;
82 }
83
84 explicit operator bool() const { return !!m_lockable; }
85
86 void unlockEarly()
87 {
88 m_lockable->unlock();
89 m_lockable = 0;
90 }
91
92 // It's great to be able to pass lockers around. It enables custom locking adaptors like
93 // JSC::LockDuringMarking.
94 Locker(Locker&& other)
95 : m_lockable(other.m_lockable)
96 {
97 other.m_lockable = nullptr;
98 }
99
100 Locker& operator=(Locker&& other)
101 {
102 if (m_lockable)
103 m_lockable->unlock();
104 m_lockable = other.m_lockable;
105 other.m_lockable = nullptr;
106 return *this;
107 }
108
109private:
110 void lock()
111 {
112 if (m_lockable)
113 m_lockable->lock();
114 compilerFence();
115 }
116
117 T* m_lockable;
118};
119
120// Use this lock scope like so:
121// auto locker = holdLock(lock);
122template<typename LockType>
123Locker<LockType> holdLock(LockType&) WARN_UNUSED_RETURN;
124template<typename LockType>
125Locker<LockType> holdLock(LockType& lock)
126{
127 return Locker<LockType>(lock);
128}
129
130template<typename LockType>
131Locker<LockType> holdLockIf(LockType&, bool predicate) WARN_UNUSED_RETURN;
132template<typename LockType>
133Locker<LockType> holdLockIf(LockType& lock, bool predicate)
134{
135 return Locker<LockType>(predicate ? &lock : nullptr);
136}
137
138template<typename LockType>
139Locker<LockType> tryHoldLock(LockType&) WARN_UNUSED_RETURN;
140template<typename LockType>
141Locker<LockType> tryHoldLock(LockType& lock)
142{
143 return Locker<LockType>::tryLock(lock);
144}
145
146}
147
148using WTF::AbstractLocker;
149using WTF::Locker;
150using WTF::NoLockingNecessaryTag;
151using WTF::NoLockingNecessary;
152using WTF::holdLock;
153using WTF::holdLockIf;
154