authorgravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2019-11-23 14:04:31-06:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-26 20:40:04-05:00
logef208fee3cf7eb25ae08e1a896eba58b91e56d50
treea12918583436add3b756045dd3b96e0e0fdb3482
parent9bce97a479e70f2d8e09047c9a0c93690cd8fd99
signature Commit is signed but in an unrecognized format.

Definition fixups & ResetEvent test cases


2 files changed, 237 insertions(+), 151 deletions(-)

lib/std/c.zig+1-1
......@@ -220,7 +220,7 @@ pub extern "c" fn pthread_mutex_destroy(mutex: *pthread_mutex_t) c_int;
220220
221221pub const PTHREAD_COND_INITIALIZER = pthread_cond_t{};
222222pub extern "c" fn pthread_cond_wait(noalias cond: *pthread_cond_t, noalias mutex: *pthread_mutex_t) c_int;
223pub extern "c" fn pthread_cond_timedwait(noalias: cond: *pthread_cond_t, noalias: mutex: *pthread_mutex_t, noalias abstime: *const timespec) c_int;
223pub extern "c" fn pthread_cond_timedwait(noalias cond: *pthread_cond_t, noalias mutex: *pthread_mutex_t, noalias abstime: *const timespec) c_int;
224224pub extern "c" fn pthread_cond_signal(cond: *pthread_cond_t) c_int;
225225pub extern "c" fn pthread_cond_destroy(cond: *pthread_cond_t) c_int;
226226
lib/std/reset_event.zig+236-150
......@@ -4,9 +4,10 @@ const testing = std.testing;
44const assert = std.debug.assert;
55const Backoff = std.SpinLock.Backoff;
66const c = std.c;
7const os = std.os;
78const time = std.time;
8const linux = std.os.linux;
9const windows = std.os.windows;
9const linux = os.linux;
10const windows = os.windows;
1011
1112/// A resource object which supports blocking until signaled.
1213/// Once finished, the `deinit()` method should be called for correctness.
......@@ -23,15 +24,15 @@ pub const ResetEvent = struct {
2324 }
2425
2526 /// Returns whether or not the event is currenetly set
26 pub fn isSet(self: *const ResetEvent) bool {
27 pub fn isSet(self: *ResetEvent) bool {
2728 return self.os_event.isSet();
2829 }
2930
3031 /// Sets the event if not already set and
3132 /// wakes up AT LEAST one thread waiting the event.
3233 /// Returns whether or not a thread was woken up.
33 pub fn set(self: *ResetEvent) bool {
34 return self.os_event.set();
34 pub fn set(self: *ResetEvent, auto_reset: bool) bool {
35 return self.os_event.set(auto_reset);
3536 }
3637
3738 /// Resets the event to its original, unset state.
......@@ -73,15 +74,15 @@ const DebugEvent = struct {
7374 self.* = undefined;
7475 }
7576
76 pub fn isSet(self: *const DebugEvent) bool {
77 pub fn isSet(self: *DebugEvent) bool {
7778 if (!std.debug.runtime_safety)
7879 return true;
7980 return self.is_set;
8081 }
8182
82 pub fn set(self: *DebugEvent) bool {
83 pub fn set(self: *DebugEvent, auto_reset: bool) bool {
8384 if (std.debug.runtime_safety)
84 self.is_set = true;
85 self.is_set = !auto_reset;
8586 return false;
8687 }
8788
......@@ -100,102 +101,87 @@ const DebugEvent = struct {
100101 }
101102};
102103
103fn EventState(comptime TagType: type) type {
104 return enum(TagType) {
105 Empty,
106 Waiting,
107 Signaled,
108 };
109}
104fn AtomicEvent(comptime FutexImpl: type) type {
105 return struct {
106 state: u32,
110107
111const SpinEvent = struct {
112 state: State,
108 const IS_SET: u32 = 1 << 0;
109 const WAIT_MASK = ~IS_SET;
113110
114 const State = EventState(u8);
111 pub const Self = @This();
112 pub const Futex = FutexImpl;
115113
116 pub fn init() SpinEvent {
117 return SpinEvent{ .state = .Empty };
118 }
114 pub fn init() Self {
115 return Self{ .state = 0 };
116 }
119117
120 pub fn deinit(self: *SpinEvent) void {
121 self.* = undefined;
122 }
118 pub fn deinit(self: *Self) void {
119 self.* = undefined;
120 }
123121
124 pub fn isSet(self: *const SpinEvent) bool {
125 return @atomicLoad(State, &self.state, .Acquire) == .Signaled;
126 }
122 pub fn isSet(self: *const Self) bool {
123 const state = @atomicLoad(u32, &self.state, .Acquire);
124 return (state & IS_SET) != 0;
125 }
127126
128 pub fn set(self: *SpinEvent) bool {
129 return @atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) == .Waiting;
130 }
127 pub fn reset(self: *Self) bool {
128 const old_state = @atomicRmw(u32, &self.state, .Xchg, 0, .Monotonic);
129 return (old_state & IS_SET) != 0;
130 }
131131
132 pub fn reset(self: *SpinEvent) bool {
133 return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled;
134 }
132 pub fn set(self: *Self, auto_reset: bool) bool {
133 const new_state = if (auto_reset) 0 else IS_SET;
134 const old_state = @atomicRmw(u32, &self.state, .Xchg, new_state, .Release);
135 if ((old_state & WAIT_MASK) == 0) {
136 return false;
137 }
135138
136 pub fn wait(self: *SpinEvent, timeout: ?u64) ResetEvent.WaitError!bool {
137 var state = @atomicLoad(State, &self.state, .Monotonic);
138 while (true) {
139 switch (state) {
140 .Empty => state = @cmpxchgWeak(State, &self.state, state, .Waiting, .Acquire, .Monotonic) orelse break,
141 .Waiting => break,
142 .Signaled => return false,
139 Futex.wake(&self.state);
140 return true;
141 }
142
143 pub fn wait(self: *Self, timeout: ?u64) ResetEvent.WaitError!bool {
144 var dummy_value: u32 = undefined;
145 const wait_token = @truncate(u32, @ptrToInt(&dummy_value));
146
147 var state = @atomicLoad(u32, &self.state, .Monotonic);
148 while (true) {
149 if ((state & IS_SET) != 0)
150 return false;
151 state = @cmpxchgWeak(u32, &self.state, state, wait_token, .Acquire, .Monotonic) orelse break;
143152 }
153
154 try Futex.wait(&self.state, wait_token, timeout);
155 return true;
144156 }
157 };
158}
159
160const SpinEvent = AtomicEvent(struct {
161 fn wake(ptr: *const u32) void {}
145162
146 // TODO: handle case for time.Timer.start() fails
163 fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void {
164 // TODO: handle platforms where time.Timer.start() fails
147165 var spin = Backoff.init();
148166 var timer = if (timeout == null) null else time.Timer.start() catch unreachable;
149 while (@atomicLoad(State, &self.state, .Monotonic) == .Waiting) {
167 while (@atomicLoad(u32, ptr, .Acquire) == expected) {
150168 spin.yield();
151169 if (timeout) |timeout_ns| {
152170 if (timer.?.read() > timeout_ns)
153171 return ResetEvent.WaitError.TimedOut;
154172 }
155173 }
156 return true;
157 }
158};
159
160const LinuxEvent = struct {
161 state: State,
162
163 const State = EventState(i32);
164
165 pub fn init() LinuxEvent {
166 return LinuxEvent{ .state = .Empty };
167 }
168
169 pub fn deinit(self: *LinuxEvent) void {
170 self.* = undefined;
171 }
172
173 pub fn isSet(self: *const LinuxEvent) bool {
174 return @atomicLoad(State, &self.state, .Acquire) == .Signaled;
175174 }
175});
176176
177 pub fn set(self: *LinuxEvent) bool {
178 if (@atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) != .Waiting)
179 return false;
180 const rc = linux.futex_wake(@ptrCast(*const i32, &self.state), linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1);
177const LinuxEvent = AtomicEvent(struct {
178 fn wake(ptr: *const u32) void {
179 const key = @ptrCast(*const i32, ptr);
180 const rc = linux.futex_wake(key, linux.FUTEX_WAKE | linux.FUTEX_PRIVATE_FLAG, 1);
181181 assert(linux.getErrno(rc) == 0);
182 return true;
183182 }
184183
185 pub fn reset(self: *LinuxEvent) bool {
186 return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled;
187 }
188
189 pub fn wait(self: *LinuxEvent, timeout: ?u64) ResetEvent.WaitError!bool {
190 var state = @atomicLoad(State, &self.state, .Monotonic);
191 while (true) {
192 switch (state) {
193 .Empty => state = @cmpxchgWeak(State, &self.state, .Empty, .Waiting, .Acquire, .Monotonic) orelse break,
194 .Waiting => break,
195 .Signaled => return false,
196 }
197 }
198
184 fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void {
199185 var ts: linux.timespec = undefined;
200186 var ts_ptr: ?*linux.timespec = null;
201187 if (timeout) |timeout_ns| {
......@@ -204,28 +190,94 @@ const LinuxEvent = struct {
204190 ts.tv_nsec = @intCast(isize, timeout_ns % time.ns_per_s);
205191 }
206192
207 while (@atomicLoad(State, &self.state, .Monotonic) == .Waiting) {
208 const rc = linux.futex_wait(@ptrCast(*const i32, &self.state), linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, @enumToInt(State.Waiting), ts_ptr);
193 const key = @ptrCast(*const i32, ptr);
194 const key_expect = @bitCast(i32, expected);
195 while (@atomicLoad(i32, key, .Acquire) == key_expect) {
196 const rc = linux.futex_wait(key, linux.FUTEX_WAIT | linux.FUTEX_PRIVATE_FLAG, key_expect, ts_ptr);
209197 switch (linux.getErrno(rc)) {
210 0, linux.EINTR => continue,
211 linux.EAGAIN => break,
198 0, linux.EAGAIN => break,
199 linux.EINTR => continue,
212200 linux.ETIMEDOUT => return ResetEvent.WaitError.TimedOut,
213201 else => unreachable,
214202 }
215203 }
216204 }
217};
205});
206
207const WindowsEvent = AtomicEvent(struct {
208 fn wake(ptr: *const u32) void {
209 if (getEventHandle()) |handle| {
210 const key = @ptrCast(*const c_void, ptr);
211 const rc = windows.ntdll.NtReleaseKeyedEvent(handle, key, windows.FALSE, null);
212 assert(rc == 0);
213 }
214 }
215
216 fn wait(ptr: *const u32, expected: u32, timeout: ?u64) ResetEvent.WaitError!void {
217 // fallback to spinlock if NT Keyed Events arent available
218 const handle = getEventHandle() orelse {
219 return SpinEvent.Futex.wait(ptr, expected, timeout);
220 };
221
222 var timeout_ptr: ?*windows.LARGE_INTEGER = null;
223 var timeout_value: windows.LARGE_INTEGER = undefined;
224 if (timeout) |timeout_ns| {
225 timeout_ptr = &timeout_value;
226 timeout_value = @intCast(windows.LARGE_INTEGER, @divFloor(timeout_ns, time.millisecond));
227 }
228
229 const key = @ptrCast(*const c_void, ptr);
230 while (@atomicLoad(u32, ptr, .Acquire) == expected) {
231 const rc = windows.ntdll.NtWaitForKeyedEvent(handle, key, windows.FALSE, timeout_ptr);
232 assert(rc == 0);
233 }
234 }
235
236 var keyed_state = State.Uninitialized;
237 var keyed_handle: ?windows.HANDLE = null;
238
239 const State = enum(u8) {
240 Uninitialized,
241 Intializing,
242 Initialized,
243 };
244
245 fn getEventHandle() ?windows.HANDLE {
246 var spin = Backoff.init();
247 var state = @atomicLoad(State, &keyed_state, .Monotonic);
248
249 while (true) {
250 switch (state) {
251 .Initialized => {
252 return keyed_handle;
253 },
254 .Intializing => {
255 spin.yield();
256 state = @atomicLoad(State, &keyed_state, .Acquire);
257 },
258 .Uninitialized => state = @cmpxchgWeak(State, &keyed_state, state, .Intializing, .Acquire, .Monotonic) orelse {
259 var handle: windows.HANDLE = undefined;
260 const access_mask = windows.GENERIC_READ | windows.GENERIC_WRITE;
261 if (windows.ntdll.NtCreateKeyedEvent(&handle, access_mask, null, 0) == 0)
262 keyed_handle = handle;
263 @atomicStore(State, &keyed_state, .Initialized, .Release);
264 return keyed_handle;
265 },
266 }
267 }
268 }
269});
218270
219271const PosixEvent = struct {
220 state: State,
272 state: u32,
221273 cond: c.pthread_cond_t,
222274 mutex: c.pthread_mutex_t,
223275
224 const State = EventState(u8);
276 const IS_SET: u32 = 1;
225277
226278 pub fn init() PosixEvent {
227279 return PosixEvent{
228 .state = .Empty,
280 .state = .0,
229281 .cond = c.PTHREAD_COND_INITIALIZER,
230282 .mutex = c.PTHREAD_MUTEX_INITIALIZER,
231283 };
......@@ -234,107 +286,141 @@ const PosixEvent = struct {
234286 pub fn deinit(self: *PosixEvent) void {
235287 // On dragonfly, the destroy functions return EINVAL if they were initialized statically.
236288 const retm = c.pthread_mutex_destroy(&self.mutex);
237 assert(retm == 0 or retm == (if (builtin.os == .dragonfly) std.os.EINVAL else 0));
289 assert(retm == 0 or retm == (if (builtin.os == .dragonfly) os.EINVAL else 0));
238290 const retc = c.pthread_cond_destroy(&self.cond);
239 assert(retc == 0 or retc == (if (builtin.os == .dragonfly) std.os.EINVAL else 0));
240 self.* = undefined;
291 assert(retc == 0 or retc == (if (builtin.os == .dragonfly) os.EINVAL else 0));
241292 }
242293
243 pub fn isSet(self: *const PosixEvent) bool {
294 pub fn isSet(self: *PosixEvent) bool {
244295 assert(c.pthread_mutex_lock(&self.mutex) == 0);
245296 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
246297
247 return self.state == .Signaled;
298 return self.state == IS_SET;
248299 }
249300
250 pub fn set(self: *PosixEvent) bool {
301 pub fn reset(self: *PosixEvent) bool {
251302 assert(c.pthread_mutex_lock(&self.mutex) == 0);
252303 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
253304
254 const woken = self.state == .Waiting;
255 self.state = .Signaled;
256 return woken;
305 const was_set = self.state == IS_SET;
306 self.state = 0;
307 return was_set;
257308 }
258309
259 pub fn reset(self: *PosixEvent) bool {
310 pub fn set(self: *PosixEvent, auto_reset: bool) bool {
260311 assert(c.pthread_mutex_lock(&self.mutex) == 0);
261312 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
262313
263 const was_set = self.state == .Signaled;
264 self.state = .Empty;
265 return was_set;
314 const had_waiter = self.state > IS_SET;
315 self.state = if (auto_reset) 0 else IS_SET;
316 if (had_waiter) {
317 assert(c.pthread_cond_signal(&self.cond) == 0);
318 }
319 return had_waiter;
266320 }
267321
268322 pub fn wait(self: *PosixEvent, timeout: ?u64) ResetEvent.WaitError!bool {
269323 assert(c.pthread_mutex_lock(&self.mutex) == 0);
270324 defer assert(c.pthread_mutex_unlock(&self.mutex) == 0);
271325
272 if (self.state == .Signaled)
326 if (self.state == IS_SET)
273327 return false;
274328
275 var ts: std.os.timespec = undefined;
329 var ts: os.timespec = undefined;
276330 var ts_ptr = &ts;
277331 if (timeout) |timeout_ns| {
278 var tv: std.os.timeval = undefined;
332 var tv: os.timeval = undefined;
279333 assert(c.gettimeofday(&tv, null) == 0);
280 ts.tv_sec = @intCast(isize, tv.tv_sec + (timeout_ns / time.ns_per_s));
281 ts.tv_nsec = @intCast(isize, (tv.tv_usec * time.microsecond) + (timeout_ns % time.ns_per_s));
334 ts.tv_sec = tv.tv_sec + @intCast(isize, timeout_ns / time.ns_per_s);
335 ts.tv_nsec = (tv.tv_usec * time.microsecond) + @intCast(isize, timeout_ns % time.ns_per_s);
282336 }
283337
284 self.state = .Waiting;
285 while (self.state == .Waiting) {
338 var dummy_value: u32 = undefined;
339 var wait_token = @truncate(u32, @ptrToInt(&dummy_value));
340 self.state = wait_token;
341
342 while (self.state == wait_token) {
286343 const rc = switch (timeout == null) {
287344 true => c.pthread_cond_wait(&self.cond, &self.mutex),
288345 else => c.pthread_cond_timedwait(&self.cond, &self.mutex, ts_ptr),
289346 };
290 assert(rc == 0);
347 // TODO: rc appears to be the positive error code making os.errno() always return 0 on linux
348 switch (std.math.max(@as(c_int, os.errno(rc)), rc)) {
349 0 => {},
350 os.ETIMEDOUT => return ResetEvent.WaitError.TimedOut,
351 os.EINVAL => unreachable,
352 os.EPERM => unreachable,
353 else => unreachable,
354 }
291355 }
356 return true;
292357 }
293358};
294359
295const WindowsEvent = struct {
296 state: State,
297
298 const State = EventState(u32);
299
300 pub fn init() WindowsEvent {
301 return WindowsEvent{ .state = .Empty };
302 }
303
304 pub fn deinit(self: *WindowsEvent) void {
305 self.* = undefined;
306 }
307
308 pub fn isSet(self: *const WindowsEvent) bool {
309 return @atomicLoad(State, &self.state, .Acquire) == .Signaled;
310 }
311
312 pub fn set(self: *WindowsEvent) bool {
313 if (@atomicRmw(State, &self.state, .Xchg, .Signaled, .Release) != .Waiting)
314 return false;
315
316 if (getEventHandle()) |handle| {
317 const key = @ptrCast(*const c_void, &self.state);
318 const rc = windows.ntdll.NtReleaseKeyedEvent(handle, key, windows.FALSE, null);
319 assert(rc == 0);
360test "std.ResetEvent" {
361 // TODO
362 if (builtin.single_threaded)
363 return error.SkipZigTest;
364
365 var event = ResetEvent.init();
366 defer event.deinit();
367
368 // test event setting
369 testing.expect(event.isSet() == false);
370 testing.expect(event.set(false) == false);
371 testing.expect(event.isSet() == true);
372
373 // test event resetting
374 testing.expect(event.reset() == true);
375 testing.expect(event.isSet() == false);
376 testing.expect(event.reset() == false);
377
378 // test waiting timeout
379 const delay = 100 * time.millisecond;
380 var timer = time.Timer.start() catch unreachable;
381 testing.expectError(ResetEvent.WaitError.TimedOut, event.wait(delay));
382 const elapsed = timer.read();
383 testing.expect(elapsed >= delay and elapsed < delay * 2);
384
385 // test cross thread signaling
386 const Context = struct {
387 event: ResetEvent,
388 value: u128,
389
390 fn receiver(self: *@This()) void {
391 // wait for the sender to notify us with updated value
392 assert(self.value == 0);
393 assert((self.event.wait(1 * time.second) catch unreachable) == true);
394 assert(self.value == 1);
395
396 // wait for sender to sleep, then notify it of new value
397 time.sleep(50 * time.millisecond);
398 self.value = 2;
399 assert(self.event.set(false) == true);
320400 }
321 return true;
322 }
323401
324 pub fn reset(self: *WindowsEvent) bool {
325 return @atomicRmw(State, &self.state, .Xchg, .Empty, .Monotonic) == .Signaled;
326 }
402 fn sender(self: *@This()) !void {
403 // wait for the receiver() to start wait()'ing
404 time.sleep(50 * time.millisecond);
327405
328 pub fn wait(self: *WindowsEvent, timeout: ?u64) ResetEvent.WaitError!bool {
329 var state = @atomicLoad(State, &self.state, .Monotonic);
330 while (true) {
331 switch (state) {
332 .Empty => state = @cmpxchgWeak(State, &self.state, .Empty, .Waiting, .Acquire, .Monotonic) orelse break,
333 .Waiting => break,
334 .Signaled => return false,
335 }
406 // update value to 1 and notify the receiver()
407 assert(self.value == 0);
408 self.value = 1;
409 assert(self.event.set(true) == true);
410
411 // wait for the receiver to update the value & notify us
412 assert((try self.event.wait(1 * time.second)) == true);
413 assert(self.value == 2);
336414 }
415 };
337416
338 const timeout_ms = if (timeout @intCast(windows.LARGE_INTEGER, )
339 }
340};
417 _ = event.reset();
418 var context = Context{
419 .event = event,
420 .value = 0,
421 };
422
423 var receiver = try std.Thread.spawn(&context, Context.receiver);
424 defer receiver.wait();
425 try context.sender();
426}
\ No newline at end of file