mirror of
https://github.com/ziglang/zig.git
synced 2024-12-04 02:48:50 +00:00
8139c5a516
Reverted #1628 and changed the grammar+parser of the language to not allow certain expr where types are expected
33 lines
868 B
Zig
33 lines
868 B
Zig
const std = @import("index.zig");
|
|
const builtin = @import("builtin");
|
|
const AtomicOrder = builtin.AtomicOrder;
|
|
const AtomicRmwOp = builtin.AtomicRmwOp;
|
|
const assert = std.debug.assert;
|
|
|
|
pub const SpinLock = struct {
|
|
lock: u8, // TODO use a bool or enum
|
|
|
|
pub const Held = struct {
|
|
spinlock: *SpinLock,
|
|
|
|
pub fn release(self: Held) void {
|
|
assert(@atomicRmw(u8, &self.spinlock.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1);
|
|
}
|
|
};
|
|
|
|
pub fn init() SpinLock {
|
|
return SpinLock{ .lock = 0 };
|
|
}
|
|
|
|
pub fn acquire(self: *SpinLock) Held {
|
|
while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {}
|
|
return Held{ .spinlock = self };
|
|
}
|
|
};
|
|
|
|
test "spinlock" {
|
|
var lock = SpinLock.init();
|
|
const held = lock.acquire();
|
|
defer held.release();
|
|
}
|