| author | |
| committer | |
| log | 4530adbd3314c28e2cfe831e7a20b154614e2c9c |
| tree | 3c2e31a1017db85db193e317c31007056e13c6d0 |
| parent | 913f7d045020f7f3301201cf69e1d788439d8886 |
6 files changed, 55 insertions(+), 77 deletions(-)
lib/std/event/future.zig+5-12| ... | ... | @@ -3,7 +3,6 @@ const assert = std.debug.assert; |
| 3 | 3 | const testing = std.testing; |
| 4 | 4 | const builtin = @import("builtin"); |
| 5 | 5 | const Lock = std.event.Lock; |
| 6 | const Loop = std.event.Loop; | |
| 7 | 6 | |
| 8 | 7 | /// This is a value that starts out unavailable, until resolve() is called |
| 9 | 8 | /// While it is unavailable, functions suspend when they try to get() it, |
| ... | ... | @@ -23,9 +22,9 @@ pub fn Future(comptime T: type) type { |
| 23 | 22 | const Self = @This(); |
| 24 | 23 | const Queue = std.atomic.Queue(anyframe); |
| 25 | 24 | |
| 26 | pub fn init(loop: *Loop) Self { | |
| 25 | pub fn init() Self { | |
| 27 | 26 | return Self{ |
| 28 | .lock = Lock.initLocked(loop), | |
| 27 | .lock = Lock.initLocked(), | |
| 29 | 28 | .available = 0, |
| 30 | 29 | .data = undefined, |
| 31 | 30 | }; |
| ... | ... | @@ -90,17 +89,11 @@ test "std.event.Future" { |
| 90 | 89 | // TODO provide a way to run tests in evented I/O mode |
| 91 | 90 | if (!std.io.is_async) return error.SkipZigTest; |
| 92 | 91 | |
| 93 | var loop: Loop = undefined; | |
| 94 | try loop.initMultiThreaded(); | |
| 95 | defer loop.deinit(); | |
| 96 | ||
| 97 | const handle = async testFuture(&loop); | |
| 98 | ||
| 99 | loop.run(); | |
| 92 | const handle = async testFuture(); | |
| 100 | 93 | } |
| 101 | 94 | |
| 102 | fn testFuture(loop: *Loop) void { | |
| 103 | var future = Future(i32).init(loop); | |
| 95 | fn testFuture() void { | |
| 96 | var future = Future(i32).init(); | |
| 104 | 97 | |
| 105 | 98 | var a = async waitOnFuture(&future); |
| 106 | 99 | var b = async waitOnFuture(&future); |
lib/std/event/group.zig+11-15| ... | ... | @@ -1,8 +1,8 @@ |
| 1 | 1 | const std = @import("../std.zig"); |
| 2 | 2 | const builtin = @import("builtin"); |
| 3 | 3 | const Lock = std.event.Lock; |
| 4 | const Loop = std.event.Loop; | |
| 5 | 4 | const testing = std.testing; |
| 5 | const Allocator = std.mem.Allocator; | |
| 6 | 6 | |
| 7 | 7 | /// ReturnType must be `void` or `E!void` |
| 8 | 8 | pub fn Group(comptime ReturnType: type) type { |
| ... | ... | @@ -10,6 +10,7 @@ pub fn Group(comptime ReturnType: type) type { |
| 10 | 10 | frame_stack: Stack, |
| 11 | 11 | alloc_stack: Stack, |
| 12 | 12 | lock: Lock, |
| 13 | allocator: *Allocator, | |
| 13 | 14 | |
| 14 | 15 | const Self = @This(); |
| 15 | 16 | |
| ... | ... | @@ -19,17 +20,18 @@ pub fn Group(comptime ReturnType: type) type { |
| 19 | 20 | }; |
| 20 | 21 | const Stack = std.atomic.Stack(anyframe->ReturnType); |
| 21 | 22 | |
| 22 | pub fn init(loop: *Loop) Self { | |
| 23 | pub fn init(allocator: *Allocator) Self { | |
| 23 | 24 | return Self{ |
| 24 | 25 | .frame_stack = Stack.init(), |
| 25 | 26 | .alloc_stack = Stack.init(), |
| 26 | .lock = Lock.init(loop), | |
| 27 | .lock = Lock.init(), | |
| 28 | .allocator = allocator, | |
| 27 | 29 | }; |
| 28 | 30 | } |
| 29 | 31 | |
| 30 | 32 | /// Add a frame to the group. Thread-safe. |
| 31 | 33 | pub fn add(self: *Self, handle: anyframe->ReturnType) (error{OutOfMemory}!void) { |
| 32 | const node = try self.lock.loop.allocator.create(Stack.Node); | |
| 34 | const node = try self.allocator.create(Stack.Node); | |
| 33 | 35 | node.* = Stack.Node{ |
| 34 | 36 | .next = undefined, |
| 35 | 37 | .data = handle, |
| ... | ... | @@ -66,7 +68,7 @@ pub fn Group(comptime ReturnType: type) type { |
| 66 | 68 | } |
| 67 | 69 | while (self.alloc_stack.pop()) |node| { |
| 68 | 70 | const handle = node.data; |
| 69 | self.lock.loop.allocator.destroy(node); | |
| 71 | self.allocator.destroy(node); | |
| 70 | 72 | if (Error == void) { |
| 71 | 73 | await handle; |
| 72 | 74 | } else { |
| ... | ... | @@ -87,18 +89,12 @@ test "std.event.Group" { |
| 87 | 89 | // TODO provide a way to run tests in evented I/O mode |
| 88 | 90 | if (!std.io.is_async) return error.SkipZigTest; |
| 89 | 91 | |
| 90 | var loop: Loop = undefined; | |
| 91 | try loop.initMultiThreaded(); | |
| 92 | defer loop.deinit(); | |
| 93 | ||
| 94 | const handle = async testGroup(&loop); | |
| 95 | ||
| 96 | loop.run(); | |
| 92 | const handle = async testGroup(std.heap.direct_allocator); | |
| 97 | 93 | } |
| 98 | 94 | |
| 99 | async fn testGroup(loop: *Loop) void { | |
| 95 | async fn testGroup(allocator: *Allocator) void { | |
| 100 | 96 | var count: usize = 0; |
| 101 | var group = Group(void).init(loop); | |
| 97 | var group = Group(void).init(allocator); | |
| 102 | 98 | var sleep_a_little_frame = async sleepALittle(&count); |
| 103 | 99 | group.add(&sleep_a_little_frame) catch @panic("memory"); |
| 104 | 100 | var increase_by_ten_frame = async increaseByTen(&count); |
| ... | ... | @@ -106,7 +102,7 @@ async fn testGroup(loop: *Loop) void { |
| 106 | 102 | group.wait(); |
| 107 | 103 | testing.expect(count == 11); |
| 108 | 104 | |
| 109 | var another = Group(anyerror!void).init(loop); | |
| 105 | var another = Group(anyerror!void).init(allocator); | |
| 110 | 106 | var something_else_frame = async somethingElse(); |
| 111 | 107 | another.add(&something_else_frame) catch @panic("memory"); |
| 112 | 108 | var something_that_fails_frame = async doSomethingThatFails(); |
lib/std/event/lock.zig+13-18| ... | ... | @@ -11,20 +11,22 @@ const Loop = std.event.Loop; |
| 11 | 11 | /// Allows only one actor to hold the lock. |
| 12 | 12 | /// TODO: make this API also work in blocking I/O mode. |
| 13 | 13 | pub const Lock = struct { |
| 14 | loop: *Loop, | |
| 15 | 14 | shared_bit: u8, // TODO make this a bool |
| 16 | 15 | queue: Queue, |
| 17 | 16 | queue_empty_bit: u8, // TODO make this a bool |
| 18 | 17 | |
| 19 | 18 | const Queue = std.atomic.Queue(anyframe); |
| 20 | 19 | |
| 20 | const global_event_loop = Loop.instance orelse | |
| 21 | @compileError("std.event.Lock currently only works with event-based I/O"); | |
| 22 | ||
| 21 | 23 | pub const Held = struct { |
| 22 | 24 | lock: *Lock, |
| 23 | 25 | |
| 24 | 26 | pub fn release(self: Held) void { |
| 25 | 27 | // Resume the next item from the queue. |
| 26 | 28 | if (self.lock.queue.get()) |node| { |
| 27 | self.lock.loop.onNextTick(node); | |
| 29 | global_event_loop.onNextTick(node); | |
| 28 | 30 | return; |
| 29 | 31 | } |
| 30 | 32 | |
| ... | ... | @@ -49,7 +51,7 @@ pub const Lock = struct { |
| 49 | 51 | |
| 50 | 52 | // Resume the next item from the queue. |
| 51 | 53 | if (self.lock.queue.get()) |node| { |
| 52 | self.lock.loop.onNextTick(node); | |
| 54 | global_event_loop.onNextTick(node); | |
| 53 | 55 | return; |
| 54 | 56 | } |
| 55 | 57 | |
| ... | ... | @@ -65,18 +67,16 @@ pub const Lock = struct { |
| 65 | 67 | } |
| 66 | 68 | }; |
| 67 | 69 | |
| 68 | pub fn init(loop: *Loop) Lock { | |
| 70 | pub fn init() Lock { | |
| 69 | 71 | return Lock{ |
| 70 | .loop = loop, | |
| 71 | 72 | .shared_bit = 0, |
| 72 | 73 | .queue = Queue.init(), |
| 73 | 74 | .queue_empty_bit = 1, |
| 74 | 75 | }; |
| 75 | 76 | } |
| 76 | 77 | |
| 77 | pub fn initLocked(loop: *Loop) Lock { | |
| 78 | pub fn initLocked() Lock { | |
| 78 | 79 | return Lock{ |
| 79 | .loop = loop, | |
| 80 | 80 | .shared_bit = 1, |
| 81 | 81 | .queue = Queue.init(), |
| 82 | 82 | .queue_empty_bit = 1, |
| ... | ... | @@ -126,27 +126,22 @@ test "std.event.Lock" { |
| 126 | 126 | // TODO provide a way to run tests in evented I/O mode |
| 127 | 127 | if (!std.io.is_async) return error.SkipZigTest; |
| 128 | 128 | |
| 129 | var loop: Loop = undefined; | |
| 130 | try loop.initMultiThreaded(); | |
| 131 | defer loop.deinit(); | |
| 132 | ||
| 133 | var lock = Lock.init(&loop); | |
| 129 | var lock = Lock.init(); | |
| 134 | 130 | defer lock.deinit(); |
| 135 | 131 | |
| 136 | _ = async testLock(&loop, &lock); | |
| 137 | loop.run(); | |
| 132 | _ = async testLock(&lock); | |
| 138 | 133 | |
| 139 | 134 | testing.expectEqualSlices(i32, [1]i32{3 * @intCast(i32, shared_test_data.len)} ** shared_test_data.len, shared_test_data); |
| 140 | 135 | } |
| 141 | 136 | |
| 142 | async fn testLock(loop: *Loop, lock: *Lock) void { | |
| 137 | async fn testLock(lock: *Lock) void { | |
| 143 | 138 | var handle1 = async lockRunner(lock); |
| 144 | 139 | var tick_node1 = Loop.NextTickNode{ |
| 145 | 140 | .prev = undefined, |
| 146 | 141 | .next = undefined, |
| 147 | 142 | .data = &handle1, |
| 148 | 143 | }; |
| 149 | loop.onNextTick(&tick_node1); | |
| 144 | Loop.instance.?.onNextTick(&tick_node1); | |
| 150 | 145 | |
| 151 | 146 | var handle2 = async lockRunner(lock); |
| 152 | 147 | var tick_node2 = Loop.NextTickNode{ |
| ... | ... | @@ -154,7 +149,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void { |
| 154 | 149 | .next = undefined, |
| 155 | 150 | .data = &handle2, |
| 156 | 151 | }; |
| 157 | loop.onNextTick(&tick_node2); | |
| 152 | Loop.instance.?.onNextTick(&tick_node2); | |
| 158 | 153 | |
| 159 | 154 | var handle3 = async lockRunner(lock); |
| 160 | 155 | var tick_node3 = Loop.NextTickNode{ |
| ... | ... | @@ -162,7 +157,7 @@ async fn testLock(loop: *Loop, lock: *Lock) void { |
| 162 | 157 | .next = undefined, |
| 163 | 158 | .data = &handle3, |
| 164 | 159 | }; |
| 165 | loop.onNextTick(&tick_node3); | |
| 160 | Loop.instance.?.onNextTick(&tick_node3); | |
| 166 | 161 | |
| 167 | 162 | await handle1; |
| 168 | 163 | await handle2; |
lib/std/event/locked.zig+3-4| ... | ... | @@ -1,6 +1,5 @@ |
| 1 | 1 | const std = @import("../std.zig"); |
| 2 | 2 | const Lock = std.event.Lock; |
| 3 | const Loop = std.event.Loop; | |
| 4 | 3 | |
| 5 | 4 | /// Thread-safe async/await lock that protects one piece of data. |
| 6 | 5 | /// Functions which are waiting for the lock are suspended, and |
| ... | ... | @@ -21,9 +20,9 @@ pub fn Locked(comptime T: type) type { |
| 21 | 20 | } |
| 22 | 21 | }; |
| 23 | 22 | |
| 24 | pub fn init(loop: *Loop, data: T) Self { | |
| 23 | pub fn init(data: T) Self { | |
| 25 | 24 | return Self{ |
| 26 | .lock = Lock.init(loop), | |
| 25 | .lock = Lock.init(), | |
| 27 | 26 | .private_data = data, |
| 28 | 27 | }; |
| 29 | 28 | } |
| ... | ... | @@ -35,7 +34,7 @@ pub fn Locked(comptime T: type) type { |
| 35 | 34 | pub async fn acquire(self: *Self) HeldLock { |
| 36 | 35 | return HeldLock{ |
| 37 | 36 | // TODO guaranteed allocation elision |
| 38 | .held = await (async self.lock.acquire() catch unreachable), | |
| 37 | .held = self.lock.acquire(), | |
| 39 | 38 | .value = &self.private_data, |
| 40 | 39 | }; |
| 41 | 40 | } |
lib/std/event/rwlock.zig+19-23| ... | ... | @@ -13,7 +13,6 @@ const Loop = std.event.Loop; |
| 13 | 13 | /// When a write lock is held, it will not be released until the writer queue is empty. |
| 14 | 14 | /// TODO: make this API also work in blocking I/O mode |
| 15 | 15 | pub const RwLock = struct { |
| 16 | loop: *Loop, | |
| 17 | 16 | shared_state: u8, // TODO make this an enum |
| 18 | 17 | writer_queue: Queue, |
| 19 | 18 | reader_queue: Queue, |
| ... | ... | @@ -29,6 +28,9 @@ pub const RwLock = struct { |
| 29 | 28 | |
| 30 | 29 | const Queue = std.atomic.Queue(anyframe); |
| 31 | 30 | |
| 31 | const global_event_loop = Loop.instance orelse | |
| 32 | @compileError("std.event.RwLock currently only works with event-based I/O"); | |
| 33 | ||
| 32 | 34 | pub const HeldRead = struct { |
| 33 | 35 | lock: *RwLock, |
| 34 | 36 | |
| ... | ... | @@ -55,7 +57,7 @@ pub const RwLock = struct { |
| 55 | 57 | // See if we can leave it locked for writing, and pass the lock to the next writer |
| 56 | 58 | // in the queue to grab the lock. |
| 57 | 59 | if (self.lock.writer_queue.get()) |node| { |
| 58 | self.lock.loop.onNextTick(node); | |
| 60 | global_event_loop.onNextTick(node); | |
| 59 | 61 | return; |
| 60 | 62 | } |
| 61 | 63 | |
| ... | ... | @@ -64,7 +66,7 @@ pub const RwLock = struct { |
| 64 | 66 | // Switch to a read lock. |
| 65 | 67 | _ = @atomicRmw(u8, &self.lock.shared_state, .Xchg, State.ReadLock, .SeqCst); |
| 66 | 68 | while (self.lock.reader_queue.get()) |node| { |
| 67 | self.lock.loop.onNextTick(node); | |
| 69 | global_event_loop.onNextTick(node); | |
| 68 | 70 | } |
| 69 | 71 | return; |
| 70 | 72 | } |
| ... | ... | @@ -76,9 +78,8 @@ pub const RwLock = struct { |
| 76 | 78 | } |
| 77 | 79 | }; |
| 78 | 80 | |
| 79 | pub fn init(loop: *Loop) RwLock { | |
| 81 | pub fn init() RwLock { | |
| 80 | 82 | return RwLock{ |
| 81 | .loop = loop, | |
| 82 | 83 | .shared_state = State.Unlocked, |
| 83 | 84 | .writer_queue = Queue.init(), |
| 84 | 85 | .writer_queue_empty_bit = 1, |
| ... | ... | @@ -120,7 +121,7 @@ pub const RwLock = struct { |
| 120 | 121 | // Give out all the read locks. |
| 121 | 122 | if (self.reader_queue.get()) |first_node| { |
| 122 | 123 | while (self.reader_queue.get()) |node| { |
| 123 | self.loop.onNextTick(node); | |
| 124 | global_event_loop.onNextTick(node); | |
| 124 | 125 | } |
| 125 | 126 | resume first_node.data; |
| 126 | 127 | } |
| ... | ... | @@ -171,7 +172,7 @@ pub const RwLock = struct { |
| 171 | 172 | } |
| 172 | 173 | // If there's an item in the writer queue, give them the lock, and we're done. |
| 173 | 174 | if (self.writer_queue.get()) |node| { |
| 174 | self.loop.onNextTick(node); | |
| 175 | global_event_loop.onNextTick(node); | |
| 175 | 176 | return; |
| 176 | 177 | } |
| 177 | 178 | // Release the lock again. |
| ... | ... | @@ -187,9 +188,9 @@ pub const RwLock = struct { |
| 187 | 188 | } |
| 188 | 189 | // If there are any items in the reader queue, give out all the reader locks, and we're done. |
| 189 | 190 | if (self.reader_queue.get()) |first_node| { |
| 190 | self.loop.onNextTick(first_node); | |
| 191 | global_event_loop.onNextTick(first_node); | |
| 191 | 192 | while (self.reader_queue.get()) |node| { |
| 192 | self.loop.onNextTick(node); | |
| 193 | global_event_loop.onNextTick(node); | |
| 193 | 194 | } |
| 194 | 195 | return; |
| 195 | 196 | } |
| ... | ... | @@ -216,46 +217,41 @@ test "std.event.RwLock" { |
| 216 | 217 | // TODO provide a way to run tests in evented I/O mode |
| 217 | 218 | if (!std.io.is_async) return error.SkipZigTest; |
| 218 | 219 | |
| 219 | var loop: Loop = undefined; | |
| 220 | try loop.initMultiThreaded(); | |
| 221 | defer loop.deinit(); | |
| 222 | ||
| 223 | var lock = RwLock.init(&loop); | |
| 220 | var lock = RwLock.init(); | |
| 224 | 221 | defer lock.deinit(); |
| 225 | 222 | |
| 226 | const handle = testLock(&loop, &lock); | |
| 227 | loop.run(); | |
| 223 | const handle = testLock(std.heap.direct_allocator, &lock); | |
| 228 | 224 | |
| 229 | 225 | const expected_result = [1]i32{shared_it_count * @intCast(i32, shared_test_data.len)} ** shared_test_data.len; |
| 230 | 226 | testing.expectEqualSlices(i32, expected_result, shared_test_data); |
| 231 | 227 | } |
| 232 | 228 | |
| 233 | async fn testLock(loop: *Loop, lock: *RwLock) void { | |
| 229 | async fn testLock(allocator: *Allocator, lock: *RwLock) void { | |
| 234 | 230 | var read_nodes: [100]Loop.NextTickNode = undefined; |
| 235 | 231 | for (read_nodes) |*read_node| { |
| 236 | const frame = loop.allocator.create(@Frame(readRunner)) catch @panic("memory"); | |
| 232 | const frame = allocator.create(@Frame(readRunner)) catch @panic("memory"); | |
| 237 | 233 | read_node.data = frame; |
| 238 | 234 | frame.* = async readRunner(lock); |
| 239 | loop.onNextTick(read_node); | |
| 235 | Loop.instance.?.onNextTick(read_node); | |
| 240 | 236 | } |
| 241 | 237 | |
| 242 | 238 | var write_nodes: [shared_it_count]Loop.NextTickNode = undefined; |
| 243 | 239 | for (write_nodes) |*write_node| { |
| 244 | const frame = loop.allocator.create(@Frame(writeRunner)) catch @panic("memory"); | |
| 240 | const frame = allocator.create(@Frame(writeRunner)) catch @panic("memory"); | |
| 245 | 241 | write_node.data = frame; |
| 246 | 242 | frame.* = async writeRunner(lock); |
| 247 | loop.onNextTick(write_node); | |
| 243 | Loop.instance.?.onNextTick(write_node); | |
| 248 | 244 | } |
| 249 | 245 | |
| 250 | 246 | for (write_nodes) |*write_node| { |
| 251 | 247 | const casted = @ptrCast(*const @Frame(writeRunner), write_node.data); |
| 252 | 248 | await casted; |
| 253 | loop.allocator.destroy(casted); | |
| 249 | allocator.destroy(casted); | |
| 254 | 250 | } |
| 255 | 251 | for (read_nodes) |*read_node| { |
| 256 | 252 | const casted = @ptrCast(*const @Frame(readRunner), read_node.data); |
| 257 | 253 | await casted; |
| 258 | loop.allocator.destroy(casted); | |
| 254 | allocator.destroy(casted); | |
| 259 | 255 | } |
| 260 | 256 | } |
| 261 | 257 |
lib/std/event/rwlocked.zig+4-5| ... | ... | @@ -1,6 +1,5 @@ |
| 1 | 1 | const std = @import("../std.zig"); |
| 2 | 2 | const RwLock = std.event.RwLock; |
| 3 | const Loop = std.event.Loop; | |
| 4 | 3 | |
| 5 | 4 | /// Thread-safe async/await RW lock that protects one piece of data. |
| 6 | 5 | /// Functions which are waiting for the lock are suspended, and |
| ... | ... | @@ -30,9 +29,9 @@ pub fn RwLocked(comptime T: type) type { |
| 30 | 29 | } |
| 31 | 30 | }; |
| 32 | 31 | |
| 33 | pub fn init(loop: *Loop, data: T) Self { | |
| 32 | pub fn init(data: T) Self { | |
| 34 | 33 | return Self{ |
| 35 | .lock = RwLock.init(loop), | |
| 34 | .lock = RwLock.init(), | |
| 36 | 35 | .locked_data = data, |
| 37 | 36 | }; |
| 38 | 37 | } |
| ... | ... | @@ -43,14 +42,14 @@ pub fn RwLocked(comptime T: type) type { |
| 43 | 42 | |
| 44 | 43 | pub async fn acquireRead(self: *Self) HeldReadLock { |
| 45 | 44 | return HeldReadLock{ |
| 46 | .held = await (async self.lock.acquireRead() catch unreachable), | |
| 45 | .held = self.lock.acquireRead(), | |
| 47 | 46 | .value = &self.locked_data, |
| 48 | 47 | }; |
| 49 | 48 | } |
| 50 | 49 | |
| 51 | 50 | pub async fn acquireWrite(self: *Self) HeldWriteLock { |
| 52 | 51 | return HeldWriteLock{ |
| 53 | .held = await (async self.lock.acquireWrite() catch unreachable), | |
| 52 | .held = self.lock.acquireWrite(), | |
| 54 | 53 | .value = &self.locked_data, |
| 55 | 54 | }; |
| 56 | 55 | } |