| 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 | #ifndef WTF_WordLock_h |
|---|
| 27 | #define WTF_WordLock_h |
|---|
| 28 | |
|---|
| 29 | #include <wtf/Atomics.h> |
|---|
| 30 | #include <wtf/Compiler.h> |
|---|
| 31 | #include <wtf/Locker.h> |
|---|
| 32 | #include <wtf/Noncopyable.h> |
|---|
| 33 | |
|---|
| 34 | namespace TestWebKitAPI { |
|---|
| 35 | struct LockInspector; |
|---|
| 36 | }; |
|---|
| 37 | |
|---|
| 38 | namespace WTF { |
|---|
| 39 | |
|---|
| 40 | // A WordLock is a fully adaptive mutex that uses sizeof(void*) storage. It has a fast path that is |
|---|
| 41 | // similar to a spinlock, and a slow path that is similar to std::mutex. In most cases, you should use |
|---|
| 42 | // Lock instead. WordLock sits lower in the stack and is used to implement Lock, so Lock is the main |
|---|
| 43 | // client of WordLock. |
|---|
| 44 | |
|---|
| 45 | struct WordLockBase { |
|---|
| 46 | void lock() |
|---|
| 47 | { |
|---|
| 48 | if (LIKELY(m_word.compareExchangeWeak(0, isLockedBit, std::memory_order_acquire))) { |
|---|
| 49 | // WordLock acquired! |
|---|
| 50 | return; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | lockSlow(); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void unlock() |
|---|
| 57 | { |
|---|
| 58 | if (LIKELY(m_word.compareExchangeWeak(isLockedBit, 0, std::memory_order_release))) { |
|---|
| 59 | // WordLock released, and nobody was waiting! |
|---|
| 60 | return; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | unlockSlow(); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | bool isHeld() const |
|---|
| 67 | { |
|---|
| 68 | return m_word.load(std::memory_order_acquire) & isLockedBit; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | bool isLocked() const |
|---|
| 72 | { |
|---|
| 73 | return isHeld(); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | protected: |
|---|
| 77 | friend struct TestWebKitAPI::LockInspector; |
|---|
| 78 | |
|---|
| 79 | static const uintptr_t isLockedBit = 1; |
|---|
| 80 | static const uintptr_t isQueueLockedBit = 2; |
|---|
| 81 | static const uintptr_t queueHeadMask = 3; |
|---|
| 82 | |
|---|
| 83 | WTF_EXPORT_PRIVATE void lockSlow(); |
|---|
| 84 | WTF_EXPORT_PRIVATE void unlockSlow(); |
|---|
| 85 | |
|---|
| 86 | // Method used for testing only. |
|---|
| 87 | bool isFullyReset() const |
|---|
| 88 | { |
|---|
| 89 | return !m_word.load(); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | Atomic<uintptr_t> m_word; |
|---|
| 93 | }; |
|---|
| 94 | |
|---|
| 95 | class WordLock : public WordLockBase { |
|---|
| 96 | WTF_MAKE_NONCOPYABLE(WordLock); |
|---|
| 97 | public: |
|---|
| 98 | WordLock() |
|---|
| 99 | { |
|---|
| 100 | m_word.store(0, std::memory_order_relaxed); |
|---|
| 101 | } |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | typedef WordLockBase StaticWordLock; |
|---|
| 105 | typedef Locker<WordLockBase> WordLockHolder; |
|---|
| 106 | |
|---|
| 107 | } // namespace WTF |
|---|
| 108 | |
|---|
| 109 | using WTF::WordLock; |
|---|
| 110 | using WTF::WordLockHolder; |
|---|
| 111 | using WTF::StaticWordLock; |
|---|
| 112 | |
|---|
| 113 | #endif // WTF_WordLock_h |
|---|
| 114 | |
|---|