Skip to content

Commit 2b5d8a2

Browse files
committed
[WTF] LockAlgorithm's unlockSlow should not assume LockType is one byte
https://bugs.webkit.org/show_bug.cgi?id=327826 rdar://190219540 Reviewed by Marcus Plutowski. The rest of LockAlgorithm uses LockType directly, do the same in unlockSlow. Also, add some static_asserts and a std::integral constraint to LockType as drive by fixes. Test: Tools/TestWebKitAPI/Tests/WTF/Lock.cpp Canonical link: https://commits.webkit.org/323977@main
1 parent 5c2be47 commit 2b5d8a2

3 files changed

Lines changed: 32 additions & 12 deletions

File tree

‎Source/WTF/wtf/LockAlgorithm.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525

2626
#pragma once
2727

28+
#include <concepts>
2829
#include <wtf/Atomics.h>
2930
#include <wtf/Compiler.h>
31+
#include <wtf/MathExtras.h>
3032

3133
namespace WTF {
3234

@@ -42,8 +44,12 @@ struct EmptyLockHooks {
4244
static LockType handoffHook(LockType value) { return value; }
4345
};
4446

45-
template<typename LockType, LockType isHeldBit, LockType hasParkedBit, typename Hooks = EmptyLockHooks<LockType>>
47+
template<std::integral LockType, LockType isHeldBit, LockType hasParkedBit, typename Hooks = EmptyLockHooks<LockType>>
4648
class LockAlgorithm {
49+
static_assert(hasOneBitSet(isHeldBit));
50+
static_assert(hasOneBitSet(hasParkedBit));
51+
static_assert(isHeldBit != hasParkedBit);
52+
4753
static constexpr bool verbose = false;
4854
static constexpr LockType mask = isHeldBit | hasParkedBit;
4955

‎Source/WTF/wtf/LockAlgorithmInlines.h‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
namespace WTF {
4141

42-
template<typename LockType, LockType isHeldBit, LockType hasParkedBit, typename Hooks>
42+
template<std::integral LockType, LockType isHeldBit, LockType hasParkedBit, typename Hooks>
4343
void LockAlgorithm<LockType, isHeldBit, hasParkedBit, Hooks>::lockSlow(Atomic<LockType>& lock)
4444
{
4545
SpinBackoff backoff;
@@ -99,22 +99,22 @@ void LockAlgorithm<LockType, isHeldBit, hasParkedBit, Hooks>::lockSlow(Atomic<Lo
9999
}
100100
}
101101

102-
template<typename LockType, LockType isHeldBit, LockType hasParkedBit, typename Hooks>
102+
template<std::integral LockType, LockType isHeldBit, LockType hasParkedBit, typename Hooks>
103103
void LockAlgorithm<LockType, isHeldBit, hasParkedBit, Hooks>::unlockSlow(Atomic<LockType>& lock, Fairness fairness)
104104
{
105105
// We could get here because the weak CAS in unlock() failed spuriously, or because there is
106106
// someone parked. So, we need a CAS loop: even if right now the lock is just held, it could
107107
// be held and parked if someone attempts to lock just as we are unlocking.
108108
for (;;) {
109-
uint8_t oldByteValue = lock.load();
110-
if ((oldByteValue & mask) != isHeldBit
111-
&& (oldByteValue & mask) != (isHeldBit | hasParkedBit)) {
112-
dataLog("Invalid value for lock: ", oldByteValue, "\n");
113-
CRASH_WITH_INFO(oldByteValue);
109+
LockType oldValue = lock.load();
110+
if ((oldValue & mask) != isHeldBit
111+
&& (oldValue & mask) != (isHeldBit | hasParkedBit)) {
112+
dataLog("Invalid value for lock: ", oldValue, "\n");
113+
CRASH_WITH_INFO(oldValue);
114114
}
115-
116-
if ((oldByteValue & mask) == isHeldBit) {
117-
if (lock.compareExchangeWeak(oldByteValue, Hooks::unlockHook(oldByteValue & ~isHeldBit)))
115+
116+
if ((oldValue & mask) == isHeldBit) {
117+
if (lock.compareExchangeWeak(oldValue, Hooks::unlockHook(oldValue & ~isHeldBit)))
118118
return;
119119
continue;
120120
}
@@ -123,7 +123,7 @@ void LockAlgorithm<LockType, isHeldBit, hasParkedBit, Hooks>::unlockSlow(Atomic<
123123
// directly, or we will unlock the lock at the same time as we unpark to allow for barging.
124124
// When we unlock, we may leave the parked bit set if there is a chance that there are still
125125
// other threads parked.
126-
ASSERT((oldByteValue & mask) == (isHeldBit | hasParkedBit));
126+
ASSERT((oldValue & mask) == (isHeldBit | hasParkedBit));
127127
ParkingLot::unparkOne(
128128
&lock,
129129
[&] (ParkingLot::UnparkResult result) -> intptr_t {

‎Tools/TestWebKitAPI/Tests/WTF/Lock.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "config.h"
2727
#include <wtf/Lock.h>
28+
#include <wtf/LockAlgorithmInlines.h>
2829
#include <wtf/Threading.h>
2930
#include <wtf/ThreadingPrimitives.h>
3031
#include <wtf/UniqueArray.h>
@@ -225,4 +226,17 @@ TEST(WTF_Lock, Basic)
225226
v.maybeSetOtherValue(34);
226227
}
227228

229+
TEST(WTF_Lock, UnlockSlowPreservesBitsAboveLowByte)
230+
{
231+
using Algorithm = LockAlgorithm<uint32_t, 1, 2>;
232+
constexpr uint32_t otherBits = 0x12345600;
233+
234+
Atomic<uint32_t> word { otherBits };
235+
Algorithm::lock(word);
236+
EXPECT_EQ(otherBits | 1, word.load());
237+
238+
Algorithm::unlockSlow(word);
239+
EXPECT_EQ(otherBits, word.load());
240+
}
241+
228242
} // namespace TestWebKitAPI

0 commit comments

Comments
 (0)