Skip to content

Commit 16778eb

Browse files
remove spinlock
1 parent 0f7be65 commit 16778eb

File tree

2 files changed

+7
-29
lines changed

2 files changed

+7
-29
lines changed

lib/fuzzer/InputPoolPosix.zig

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ buffer: MemoryMappedList(u8),
3333
///
3434
/// layout of the meta file (v0):
3535
/// [0] [3]u8 file signature + u8 format version
36-
/// [1] u32 for xcmpchg (mutex in the stdlib is not FUTEX_SHARED)
36+
/// [1] u64 for a lock
3737
/// [2] u32 deleted bytes
3838
/// [3] u32 number of strings
3939
/// [4..] data (string end offsets)
4040
meta: MemoryMappedList(u32),
4141

4242
const MetaHeader = packed struct {
4343
signature_version: u32,
44-
lock: u32,
44+
lock: std.Thread.Mutex,
4545
deleted_bytes: u32,
4646
number_of_string: u32,
4747
};
@@ -83,7 +83,7 @@ pub fn init(dir: std.fs.Dir, pc_digest: u64) InputPoolPosix {
8383
if (meta.items.len == 0) {
8484
const header: MetaHeader = .{
8585
.signature_version = SignatureVersion,
86-
.lock = Unlocked,
86+
.lock = std.Thread.Mutex{},
8787
.deleted_bytes = 0,
8888
.number_of_string = 0,
8989
};
@@ -97,7 +97,6 @@ pub fn init(dir: std.fs.Dir, pc_digest: u64) InputPoolPosix {
9797
} else {
9898
const header = getHeader(meta);
9999
assert(header.signature_version == SignatureVersion);
100-
assert(header.lock == Unlocked or header.lock == Locked);
101100
}
102101

103102
return .{
@@ -111,31 +110,10 @@ pub fn deinit(ip: *InputPoolPosix) void {
111110
ip.meta.deinit();
112111
}
113112

114-
// Primitive spin lock implementation. There is basically no contention on it.
115-
const Locked: u32 = 1;
116-
const Unlocked: u32 = 0;
117-
118-
fn lock(ip: *InputPoolPosix) void {
119-
const lck: *volatile u32 = &getHeader(ip.meta).lock;
120-
while (true) {
121-
const res = @cmpxchgWeak(u32, lck, Unlocked, Locked, .acquire, .monotonic);
122-
if (res) |v| {
123-
assert(v == Locked);
124-
} else {
125-
return;
126-
}
127-
}
128-
}
129-
130-
fn unlock(ip: *InputPoolPosix) void {
131-
const lck: *volatile u32 = &getHeader(ip.meta).lock;
132-
const res = @atomicRmw(u32, lck, .Xchg, Unlocked, .release);
133-
assert(res == Locked);
134-
}
135-
136113
pub fn insertString(ip: *InputPoolPosix, str: []const u8) void {
137-
ip.lock();
138-
defer ip.unlock();
114+
const header = getHeader(ip.meta);
115+
header.lock.lock();
116+
defer header.lock.unlock();
139117

140118
assert(ip.buffer.items.len + str.len < std.math.maxInt(Index));
141119

lib/std/Thread/Futex.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ const LinuxImpl = struct {
264264

265265
const rc = linux.futex_wait(
266266
@as(*const i32, @ptrCast(&ptr.raw)),
267-
linux.FUTEX.PRIVATE_FLAG | linux.FUTEX.WAIT,
267+
linux.FUTEX.WAIT,
268268
@as(i32, @bitCast(expect)),
269269
if (timeout != null) &ts else null,
270270
);

0 commit comments

Comments
 (0)