Skip to content

std.mutex: implement blocking mutexes on Linux #1463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions std/mutex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,31 @@ const builtin = @import("builtin");
const AtomicOrder = builtin.AtomicOrder;
const AtomicRmwOp = builtin.AtomicRmwOp;
const assert = std.debug.assert;
const linux = std.os.linux;

// Reading: Futexes Are Tricky by Ulrich Drepper https://www.akkadia.org/drepper/futex.pdf

// TODO robust mutexes https://www.kernel.org/doc/Documentation/robust-futexes.txt

/// TODO use syscalls instead of a spinlock
pub const Mutex = struct {
lock: u8, // TODO use a bool
// TODO: Windows and OSX with futex equivilents
// 0: unlocked
// 1: locked, no waiters
// 2: locked: one or more waiters
lock: u32, // futexs are 32-bits on all architectures

pub const Held = struct {
mutex: *Mutex,

pub fn release(self: Held) void {
assert(@atomicRmw(u8, &self.mutex.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
if (@atomicRmw(u32, &self.mutex.lock, AtomicRmwOp.Sub, 1, AtomicOrder.Release) != 1) {
self.mutex.lock = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't have a naked write racing with an atomic write (the cmpxchg on line 42).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't the @atomicRmw do an xchg and set the value to 0? You can still check if the previous value was 1.

if (builtin.os == builtin.Os.linux) {
_ = linux.futex_wake(@ptrToInt(&self.mutex.lock), linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1);
} else {
// spin-lock
}
}
}
};

Expand All @@ -21,7 +36,28 @@ pub const Mutex = struct {
}

pub fn acquire(self: *Mutex) Held {
while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
var c: u32 = undefined;
// This need not be strong because of the loop that follows.
// TODO implement mutex3 from https://www.akkadia.org/drepper/futex.pdf in x86 assembly.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that really better? Can we have an issue to discuss it rather than this TODO comment?

if (@cmpxchgWeak(u32, &self.lock, 0, 1, AtomicOrder.Acquire, AtomicOrder.Monotonic)) |value1| {
c = value1;
while (true) {
if (c == 2 or
@cmpxchgWeak(u32, &self.lock, 1, 2, AtomicOrder.Acquire, AtomicOrder.Monotonic) == null)
{
if (builtin.os == builtin.Os.linux) {
_ = linux.futex_wait(@ptrToInt(&self.lock), linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, 2, null);
} else {
// spin-lock
}
}
if (@cmpxchgWeak(u32, &self.lock, 0, 2, AtomicOrder.Acquire, AtomicOrder.Monotonic)) |value2| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this jump straight to the "locked with waiters" state? shouldn't it just go back and try the original cmpxchg again?

c = value2;
} else {
break;
}
}
}
return Held{ .mutex = self };
}
};