| ... | @@ -1,3 +1,10 @@ | ... | @@ -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 | /// This is a thin wrapper around a primitive value to prevent accidental data races. | 8 | /// This is a thin wrapper around a primitive value to prevent accidental data races. |
| 2 | pub fn Value(comptime T: type) type { | 9 | pub fn Value(comptime T: type) type { |
| 3 | return extern struct { | 10 | return extern struct { |
| ... | @@ -496,7 +503,17 @@ test "current CPU has a cache line size" { | ... | @@ -496,7 +503,17 @@ test "current CPU has a cache line size" { |
| 496 | _ = cache_line; | 503 | _ = cache_line; |
| 497 | } | 504 | } |
| 498 | | 505 | |
| 499 | const std = @import("std.zig"); | 506 | /// A lock-free single-owner resource. |
| 500 | const builtin = @import("builtin"); | 507 | pub const Mutex = enum(u8) { |
| 501 | const AtomicOrder = std.builtin.AtomicOrder; | 508 | unlocked, |
| 502 | const testing = std.testing; | 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 | }; |