diff options
| author | Vexu <15308111+Vexu@users.noreply.github.com> | 2019-11-12 17:40:21 +0200 |
|---|---|---|
| committer | Vexu <15308111+Vexu@users.noreply.github.com> | 2019-11-12 17:55:54 +0200 |
| commit | 5194fc57d1c206d71654b4f3e43bfcb300bf43c5 (patch) | |
| tree | 02f1b897c19fbc22af9161af9f1974561c0d4b7d /lib/std/event/future.zig | |
| parent | 7e5b234b8b3a7e675a7b11eebcdaf504f2686749 (diff) | |
| download | zig-5194fc57d1c206d71654b4f3e43bfcb300bf43c5.tar.gz zig-5194fc57d1c206d71654b4f3e43bfcb300bf43c5.zip | |
use enum with atomics in std lib
Diffstat (limited to 'lib/std/event/future.zig')
| -rw-r--r-- | lib/std/event/future.zig | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/lib/std/event/future.zig b/lib/std/event/future.zig index 356d9cac79..43593b348a 100644 --- a/lib/std/event/future.zig +++ b/lib/std/event/future.zig @@ -12,12 +12,13 @@ pub fn Future(comptime T: type) type { return struct { lock: Lock, data: T, + available: Available, - /// TODO make this an enum - /// 0 - not started - /// 1 - started - /// 2 - finished - available: u8, + const Available = enum(u8) { + NotStarted, + Started, + Finished, + }; const Self = @This(); const Queue = std.atomic.Queue(anyframe); @@ -34,7 +35,7 @@ pub fn Future(comptime T: type) type { /// available. /// Thread-safe. pub async fn get(self: *Self) *T { - if (@atomicLoad(u8, &self.available, .SeqCst) == 2) { + if (@atomicLoad(Available, &self.available, .SeqCst) == .Finished) { return &self.data; } const held = self.lock.acquire(); @@ -46,7 +47,7 @@ pub fn Future(comptime T: type) type { /// Gets the data without waiting for it. If it's available, a pointer is /// returned. Otherwise, null is returned. pub fn getOrNull(self: *Self) ?*T { - if (@atomicLoad(u8, &self.available, .SeqCst) == 2) { + if (@atomicLoad(Available, &self.available, .SeqCst) == .Finished) { return &self.data; } else { return null; @@ -59,7 +60,7 @@ pub fn Future(comptime T: type) type { /// It's not required to call start() before resolve() but it can be useful since /// this method is thread-safe. pub async fn start(self: *Self) ?*T { - const state = @cmpxchgStrong(u8, &self.available, 0, 1, .SeqCst, .SeqCst) orelse return null; + const state = @cmpxchgStrong(Available, &self.available, .NotStarted, .Started, .SeqCst, .SeqCst) orelse return null; switch (state) { 1 => { const held = self.lock.acquire(); @@ -74,8 +75,8 @@ pub fn Future(comptime T: type) type { /// Make the data become available. May be called only once. /// Before calling this, modify the `data` property. pub fn resolve(self: *Self) void { - const prev = @atomicRmw(u8, &self.available, .Xchg, 2, .SeqCst); - assert(prev == 0 or prev == 1); // resolve() called twice + const prev = @atomicRmw(Available, &self.available, .Xchg, .Finished, .SeqCst); + assert(prev != .Finished); // resolve() called twice Lock.Held.release(Lock.Held{ .lock = &self.lock }); } }; |
