| ... | ... | @@ -1,3 +1,10 @@ |
| 1 | const builtin = @import("builtin"); |
| 2 | |
| 3 | const std = @import("std.zig"); |
| 4 | const AtomicOrder = std.builtin.AtomicOrder; |
| 5 | const testing = std.testing; |
| 6 | const assert = std.debug.assert; |
| 7 | |
| 1 | 8 | /// This is a thin wrapper around a primitive value to prevent accidental data races. |
| 2 | 9 | pub fn Value(comptime T: type) type { |
| 3 | 10 | return extern struct { |
| ... | ... | @@ -496,7 +503,17 @@ test "current CPU has a cache line size" { |
| 496 | 503 | _ = cache_line; |
| 497 | 504 | } |
| 498 | 505 | |
| 499 | | const std = @import("std.zig"); |
| 500 | | const builtin = @import("builtin"); |
| 501 | | const AtomicOrder = std.builtin.AtomicOrder; |
| 502 | | const testing = std.testing; |
| 506 | /// A lock-free single-owner resource. |
| 507 | pub const Mutex = enum(u8) { |
| 508 | unlocked, |
| 509 | locked, |
| 510 | |
| 511 | pub fn tryLock(m: *Mutex) bool { |
| 512 | return @cmpxchgWeak(Mutex, m, .unlocked, .locked, .acquire, .monotonic) == null; |
| 513 | } |
| 514 | |
| 515 | pub fn unlock(m: *Mutex) void { |
| 516 | assert(m.* == .locked); |
| 517 | @atomicStore(Mutex, m, .unlocked, .release); |
| 518 | } |
| 519 | }; |