1/*
2 * Copyright (C) 2015-2016 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include <wtf/Atomics.h>
29#include <wtf/Compiler.h>
30#include <wtf/Locker.h>
31#include <wtf/Noncopyable.h>
32
33namespace TestWebKitAPI {
34struct LockInspector;
35};
36
37namespace WTF {
38
39// A WordLock is a fully adaptive mutex that uses sizeof(void*) storage. It has a fast path that is
40// similar to a spinlock, and a slow path that is similar to std::mutex. In most cases, you should use
41// Lock instead. WordLock sits lower in the stack and is used to implement Lock, so Lock is the main
42// client of WordLock.
43
44// NOTE: This is also a great lock to use if you are very low in the stack. For example,
45// PrintStream uses this so that ParkingLot and Lock can use PrintStream. This means that if you
46// try to use dataLog to debug this code, you will have a bad time.
47
48class WordLock {
49 WTF_MAKE_NONCOPYABLE(WordLock);
50 WTF_MAKE_FAST_ALLOCATED;
51public:
52 constexpr WordLock() = default;
53
54 void lock()
55 {
56 if (LIKELY(m_word.compareExchangeWeak(0, isLockedBit, std::memory_order_acquire))) {
57 // WordLock acquired!
58 return;
59 }
60
61 lockSlow();
62 }
63
64 void unlock()
65 {
66 if (LIKELY(m_word.compareExchangeWeak(isLockedBit, 0, std::memory_order_release))) {
67 // WordLock released, and nobody was waiting!
68 return;
69 }
70
71 unlockSlow();
72 }
73
74 bool isHeld() const
75 {
76 return m_word.load(std::memory_order_acquire) & isLockedBit;
77 }
78
79 bool isLocked() const
80 {
81 return isHeld();
82 }
83
84protected:
85 friend struct TestWebKitAPI::LockInspector;
86
87 static const uintptr_t isLockedBit = 1;
88 static const uintptr_t isQueueLockedBit = 2;
89 static const uintptr_t queueHeadMask = 3;
90
91 WTF_EXPORT_PRIVATE void lockSlow();
92 WTF_EXPORT_PRIVATE void unlockSlow();
93
94 // Method used for testing only.
95 bool isFullyReset() const
96 {
97 return !m_word.load();
98 }
99
100 Atomic<uintptr_t> m_word { 0 };
101};
102
103using WordLockHolder = Locker<WordLock>;
104
105} // namespace WTF
106
107using WTF::WordLock;
108using WTF::WordLockHolder;
109