authorgravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2021-06-19 21:31:43-05:00
committergravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2021-06-30 21:48:59-05:00
log0a1def7833882249563358f262e2210beb77492a
tree5441edaf05de9ff0d0d2b33f130adbddffdb9fcd
parente16d3d162a85a822e16ae181ecc6ddc507278126

changes to accomodate std.Thread update


16 files changed, 130 insertions(+), 128 deletions(-)

lib/std/Thread/AutoResetEvent.zig+4-4
......@@ -220,9 +220,9 @@ test "basic usage" {
220220 };
221221
222222 var context = Context{};
223 const send_thread = try std.Thread.spawn(Context.sender, &context);
224 const recv_thread = try std.Thread.spawn(Context.receiver, &context);
223 const send_thread = try std.Thread.spawn(.{}, Context.sender, .{&context});
224 const recv_thread = try std.Thread.spawn(.{}, Context.receiver, .{&context});
225225
226 send_thread.wait();
227 recv_thread.wait();
226 send_thread.join();
227 recv_thread.join();
228228}
lib/std/Thread/Futex.zig+24-36
......@@ -413,32 +413,27 @@ test "Futex - Signal" {
413413 }
414414 }
415415
416 const Thread = struct {
417 tx: *Self,
418 rx: *Self,
416 const start_value = 1;
419417
420 const start_value = 1;
421
422 fn run(self: Thread) void {
423 var iterations: u32 = start_value;
424 while (iterations < 10) : (iterations += 1) {
425 self.rx.recv(iterations);
426 self.tx.send(iterations);
427 }
418 fn runThread(rx: *Self, tx: *Self) void {
419 var iterations: u32 = start_value;
420 while (iterations < 10) : (iterations += 1) {
421 self.rx.recv(iterations);
422 self.tx.send(iterations);
428423 }
429 };
424 }
430425
431426 fn run() !void {
432427 var ping = Self{};
433428 var pong = Self{};
434429
435 const t1 = try std.Thread.spawn(Thread.run, .{ .rx = &ping, .tx = &pong });
436 defer t1.wait();
430 const t1 = try std.Thread.spawn(.{}, runThread, .{ &ping, &pong });
431 defer t1.join();
437432
438 const t2 = try std.Thread.spawn(Thread.run, .{ .rx = &pong, .tx = &ping });
439 defer t2.wait();
433 const t2 = try std.Thread.spawn(.{}, runThread, .{ &pong, &ping });
434 defer t2.join();
440435
441 ping.send(Thread.start_value);
436 ping.send(start_value);
442437 }
443438 }).run();
444439}
......@@ -507,7 +502,7 @@ test "Futex - Chain" {
507502 try (struct {
508503 completed: Signal = .{},
509504 threads: [10]struct {
510 thread: *std.Thread,
505 thread: std.Thread,
511506 signal: Signal,
512507 } = undefined,
513508
......@@ -531,39 +526,32 @@ test "Futex - Chain" {
531526 };
532527
533528 const Self = @This();
534 const Chain = struct {
535 self: *Self,
536 index: usize,
537529
538 fn run(chain: Chain) void {
539 const this_signal = &chain.self.threads[chain.index].signal;
530 fn runThread(self: *Self, index: usize) void {
531 const this_signal = &chain.self.threads[chain.index].signal;
540532
541 var next_signal = &chain.self.completed;
542 if (chain.index + 1 < chain.self.threads.len) {
543 next_signal = &chain.self.threads[chain.index + 1].signal;
544 }
545
546 this_signal.wait();
547 next_signal.notify();
533 var next_signal = &chain.self.completed;
534 if (chain.index + 1 < chain.self.threads.len) {
535 next_signal = &chain.self.threads[chain.index + 1].signal;
548536 }
549 };
537
538 this_signal.wait();
539 next_signal.notify();
540 }
550541
551542 fn run() !void {
552543 var self = Self{};
553544
554545 for (self.threads) |*entry, index| {
555546 entry.signal = .{};
556 entry.thread = try std.Thread.spawn(Chain.run, .{
557 .self = &self,
558 .index = index,
559 });
547 entry.thread = try std.Thread.spawn(.{}, runThread .{&self, index});
560548 }
561549
562550 self.threads[0].signal.notify();
563551 self.completed.wait();
564552
565553 for (self.threads) |entry| {
566 entry.thread.wait();
554 entry.thread.join();
567555 }
568556 }
569557 }).run();
lib/std/Thread/Mutex.zig+3-3
......@@ -297,12 +297,12 @@ test "basic usage" {
297297 try testing.expect(context.data == TestContext.incr_count);
298298 } else {
299299 const thread_count = 10;
300 var threads: [thread_count]*std.Thread = undefined;
300 var threads: [thread_count]std.Thread = undefined;
301301 for (threads) |*t| {
302 t.* = try std.Thread.spawn(worker, &context);
302 t.* = try std.Thread.spawn(.{}, worker, .{&context});
303303 }
304304 for (threads) |t|
305 t.wait();
305 t.join();
306306
307307 try testing.expect(context.data == thread_count * TestContext.incr_count);
308308 }
lib/std/Thread/ResetEvent.zig+4-4
......@@ -281,8 +281,8 @@ test "basic usage" {
281281 var context: Context = undefined;
282282 try context.init();
283283 defer context.deinit();
284 const receiver = try std.Thread.spawn(Context.receiver, &context);
285 defer receiver.wait();
284 const receiver = try std.Thread.spawn(.{}, Context.receiver, .{&context});
285 defer receiver.join();
286286 try context.sender();
287287
288288 if (false) {
......@@ -290,8 +290,8 @@ test "basic usage" {
290290 // https://github.com/ziglang/zig/issues/7009
291291 var timed = Context.init();
292292 defer timed.deinit();
293 const sleeper = try std.Thread.spawn(Context.sleeper, &timed);
294 defer sleeper.wait();
293 const sleeper = try std.Thread.spawn(.{}, Context.sleeper, .{&timed});
294 defer sleeper.join();
295295 try timed.timedWaiter();
296296 }
297297}
lib/std/Thread/StaticResetEvent.zig+4-4
......@@ -384,8 +384,8 @@ test "basic usage" {
384384 };
385385
386386 var context = Context{};
387 const receiver = try std.Thread.spawn(Context.receiver, &context);
388 defer receiver.wait();
387 const receiver = try std.Thread.spawn(.{}, Context.receiver, .{&context});
388 defer receiver.join();
389389 try context.sender();
390390
391391 if (false) {
......@@ -393,8 +393,8 @@ test "basic usage" {
393393 // https://github.com/ziglang/zig/issues/7009
394394 var timed = Context.init();
395395 defer timed.deinit();
396 const sleeper = try std.Thread.spawn(Context.sleeper, &timed);
397 defer sleeper.wait();
396 const sleeper = try std.Thread.spawn(.{}, Context.sleeper, .{&timed});
397 defer sleeper.join();
398398 try timed.timedWaiter();
399399 }
400400}
lib/std/atomic/queue.zig+6-6
......@@ -214,20 +214,20 @@ test "std.atomic.Queue" {
214214 } else {
215215 try expect(context.queue.isEmpty());
216216
217 var putters: [put_thread_count]*std.Thread = undefined;
217 var putters: [put_thread_count]std.Thread = undefined;
218218 for (putters) |*t| {
219 t.* = try std.Thread.spawn(startPuts, &context);
219 t.* = try std.Thread.spawn(.{}, startPuts, .{&context});
220220 }
221 var getters: [put_thread_count]*std.Thread = undefined;
221 var getters: [put_thread_count]std.Thread = undefined;
222222 for (getters) |*t| {
223 t.* = try std.Thread.spawn(startGets, &context);
223 t.* = try std.Thread.spawn(.{}, startGets, .{&context});
224224 }
225225
226226 for (putters) |t|
227 t.wait();
227 t.join();
228228 @atomicStore(bool, &context.puts_done, true, .SeqCst);
229229 for (getters) |t|
230 t.wait();
230 t.join();
231231
232232 try expect(context.queue.isEmpty());
233233 }
lib/std/atomic/stack.zig+6-6
......@@ -121,20 +121,20 @@ test "std.atomic.stack" {
121121 }
122122 }
123123 } else {
124 var putters: [put_thread_count]*std.Thread = undefined;
124 var putters: [put_thread_count]std.Thread = undefined;
125125 for (putters) |*t| {
126 t.* = try std.Thread.spawn(startPuts, &context);
126 t.* = try std.Thread.spawn(.{}, startPuts, .{&context});
127127 }
128 var getters: [put_thread_count]*std.Thread = undefined;
128 var getters: [put_thread_count]std.Thread = undefined;
129129 for (getters) |*t| {
130 t.* = try std.Thread.spawn(startGets, &context);
130 t.* = try std.Thread.spawn(.{}, startGets, .{&context});
131131 }
132132
133133 for (putters) |t|
134 t.wait();
134 t.join();
135135 @atomicStore(bool, &context.puts_done, true, .SeqCst);
136136 for (getters) |t|
137 t.wait();
137 t.join();
138138 }
139139
140140 if (context.put_sum != context.get_sum) {
lib/std/debug.zig+2-2
......@@ -273,8 +273,8 @@ pub fn panicExtra(trace: ?*const builtin.StackTrace, first_trace_addr: ?usize, c
273273 if (builtin.single_threaded) {
274274 stderr.print("panic: ", .{}) catch os.abort();
275275 } else {
276 const current_thread_id = std.Thread.getCurrentThreadId();
277 stderr.print("thread {d} panic: ", .{current_thread_id}) catch os.abort();
276 const current_thread_id = std.Thread.getCurrentId();
277 stderr.print("thread {} panic: ", .{current_thread_id}) catch os.abort();
278278 }
279279 stderr.print(format ++ "\n", args) catch os.abort();
280280 if (trace) |t| {
lib/std/event/loop.zig+15-15
......@@ -21,12 +21,12 @@ pub const Loop = struct {
2121 os_data: OsData,
2222 final_resume_node: ResumeNode,
2323 pending_event_count: usize,
24 extra_threads: []*Thread,
24 extra_threads: []Thread,
2525 /// TODO change this to a pool of configurable number of threads
2626 /// and rename it to be not file-system-specific. it will become
2727 /// a thread pool for turning non-CPU-bound blocking things into
2828 /// async things. A fallback for any missing OS-specific API.
29 fs_thread: *Thread,
29 fs_thread: Thread,
3030 fs_queue: std.atomic.Queue(Request),
3131 fs_end_request: Request.Node,
3232 fs_thread_wakeup: std.Thread.ResetEvent,
......@@ -189,11 +189,11 @@ pub const Loop = struct {
189189 errdefer self.deinitOsData();
190190
191191 if (!builtin.single_threaded) {
192 self.fs_thread = try Thread.spawn(posixFsRun, self);
192 self.fs_thread = try Thread.spawn(.{}, posixFsRun, .{self});
193193 }
194194 errdefer if (!builtin.single_threaded) {
195195 self.posixFsRequest(&self.fs_end_request);
196 self.fs_thread.wait();
196 self.fs_thread.join();
197197 };
198198
199199 if (!std.builtin.single_threaded)
......@@ -264,11 +264,11 @@ pub const Loop = struct {
264264 assert(amt == wakeup_bytes.len);
265265 while (extra_thread_index != 0) {
266266 extra_thread_index -= 1;
267 self.extra_threads[extra_thread_index].wait();
267 self.extra_threads[extra_thread_index].join();
268268 }
269269 }
270270 while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) {
271 self.extra_threads[extra_thread_index] = try Thread.spawn(workerRun, self);
271 self.extra_threads[extra_thread_index] = try Thread.spawn(.{}, workerRun, .{self});
272272 }
273273 },
274274 .macos, .freebsd, .netbsd, .dragonfly, .openbsd => {
......@@ -329,11 +329,11 @@ pub const Loop = struct {
329329 _ = os.kevent(self.os_data.kqfd, final_kev_arr, empty_kevs, null) catch unreachable;
330330 while (extra_thread_index != 0) {
331331 extra_thread_index -= 1;
332 self.extra_threads[extra_thread_index].wait();
332 self.extra_threads[extra_thread_index].join();
333333 }
334334 }
335335 while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) {
336 self.extra_threads[extra_thread_index] = try Thread.spawn(workerRun, self);
336 self.extra_threads[extra_thread_index] = try Thread.spawn(.{}, workerRun, .{self});
337337 }
338338 },
339339 .windows => {
......@@ -378,11 +378,11 @@ pub const Loop = struct {
378378 }
379379 while (extra_thread_index != 0) {
380380 extra_thread_index -= 1;
381 self.extra_threads[extra_thread_index].wait();
381 self.extra_threads[extra_thread_index].join();
382382 }
383383 }
384384 while (extra_thread_index < extra_thread_count) : (extra_thread_index += 1) {
385 self.extra_threads[extra_thread_index] = try Thread.spawn(workerRun, self);
385 self.extra_threads[extra_thread_index] = try Thread.spawn(.{}, workerRun, .{self});
386386 }
387387 },
388388 else => {},
......@@ -651,18 +651,18 @@ pub const Loop = struct {
651651 .netbsd,
652652 .dragonfly,
653653 .openbsd,
654 => self.fs_thread.wait(),
654 => self.fs_thread.join(),
655655 else => {},
656656 }
657657 }
658658
659659 for (self.extra_threads) |extra_thread| {
660 extra_thread.wait();
660 extra_thread.join();
661661 }
662662
663663 @atomicStore(bool, &self.delay_queue.is_running, false, .SeqCst);
664664 self.delay_queue.event.set();
665 self.delay_queue.thread.wait();
665 self.delay_queue.thread.join();
666666 }
667667
668668 /// Runs the provided function asynchronously. The function's frame is allocated
......@@ -787,7 +787,7 @@ pub const Loop = struct {
787787 const DelayQueue = struct {
788788 timer: std.time.Timer,
789789 waiters: Waiters,
790 thread: *std.Thread,
790 thread: std.Thread,
791791 event: std.Thread.AutoResetEvent,
792792 is_running: bool,
793793
......@@ -802,7 +802,7 @@ pub const Loop = struct {
802802 .event = std.Thread.AutoResetEvent{},
803803 .is_running = true,
804804 // Must be last so that it can read the other state, such as `is_running`.
805 .thread = try std.Thread.spawn(DelayQueue.run, self),
805 .thread = try std.Thread.spawn(.{}, DelayQueue.run, .{self}),
806806 };
807807 }
808808
lib/std/fs/test.zig+5-6
......@@ -862,11 +862,10 @@ test "open file with exclusive lock twice, make sure it waits" {
862862 errdefer file.close();
863863
864864 const S = struct {
865 const C = struct { dir: *fs.Dir, evt: *std.Thread.ResetEvent };
866 fn checkFn(ctx: C) !void {
867 const file1 = try ctx.dir.createFile(filename, .{ .lock = .Exclusive });
865 fn checkFn(dir: *fs.Dir, evt: *std.Thread.ResetEvent) !void {
866 const file1 = try dir.createFile(filename, .{ .lock = .Exclusive });
868867 defer file1.close();
869 ctx.evt.set();
868 evt.set();
870869 }
871870 };
872871
......@@ -874,8 +873,8 @@ test "open file with exclusive lock twice, make sure it waits" {
874873 try evt.init();
875874 defer evt.deinit();
876875
877 const t = try std.Thread.spawn(S.checkFn, S.C{ .dir = &tmp.dir, .evt = &evt });
878 defer t.wait();
876 const t = try std.Thread.spawn(.{}, S.checkFn, .{ &tmp.dir, &evt });
877 defer t.join();
879878
880879 const SLEEP_TIMEOUT_NS = 10 * std.time.ns_per_ms;
881880 // Make sure we've slept enough.
lib/std/net/test.zig+5-5
......@@ -161,8 +161,8 @@ test "listen on a port, send bytes, receive bytes" {
161161 }
162162 };
163163
164 const t = try std.Thread.spawn(S.clientFn, server.listen_address);
165 defer t.wait();
164 const t = try std.Thread.spawn(.{}, S.clientFn, .{server.listen_address});
165 defer t.join();
166166
167167 var client = try server.accept();
168168 defer client.stream.close();
......@@ -277,7 +277,7 @@ test "listen on a unix socket, send bytes, receive bytes" {
277277 try server.listen(socket_addr);
278278
279279 const S = struct {
280 fn clientFn(_: void) !void {
280 fn clientFn() !void {
281281 const socket = try net.connectUnixSocket(socket_path);
282282 defer socket.close();
283283
......@@ -285,8 +285,8 @@ test "listen on a unix socket, send bytes, receive bytes" {
285285 }
286286 };
287287
288 const t = try std.Thread.spawn(S.clientFn, {});
289 defer t.wait();
288 const t = try std.Thread.spawn(.{}, S.clientFn, .{});
289 defer t.join();
290290
291291 var client = try server.accept();
292292 defer client.stream.close();
lib/std/once.zig+4-4
......@@ -55,16 +55,16 @@ test "Once executes its function just once" {
5555 global_once.call();
5656 global_once.call();
5757 } else {
58 var threads: [10]*std.Thread = undefined;
59 defer for (threads) |handle| handle.wait();
58 var threads: [10]std.Thread = undefined;
59 defer for (threads) |handle| handle.join();
6060
6161 for (threads) |*handle| {
62 handle.* = try std.Thread.spawn(struct {
62 handle.* = try std.Thread.spawn(.{}, struct {
6363 fn thread_fn(x: u8) void {
6464 _ = x;
6565 global_once.call();
6666 }
67 }.thread_fn, 0);
67 }.thread_fn, .{0});
6868 }
6969 }
7070
lib/std/os/test.zig+17-19
......@@ -320,9 +320,9 @@ test "std.Thread.getCurrentId" {
320320 if (builtin.single_threaded) return error.SkipZigTest;
321321
322322 var thread_current_id: Thread.Id = undefined;
323 const thread = try Thread.spawn(testThreadIdFn, &thread_current_id);
324 const thread_id = thread.handle();
325 thread.wait();
323 const thread = try Thread.spawn(.{}, testThreadIdFn, .{&thread_current_id});
324 const thread_id = thread.getHandle();
325 thread.join();
326326 if (Thread.use_pthreads) {
327327 try expect(thread_current_id == thread_id);
328328 } else if (native_os == .windows) {
......@@ -339,21 +339,20 @@ test "spawn threads" {
339339
340340 var shared_ctx: i32 = 1;
341341
342 const thread1 = try Thread.spawn(start1, {});
343 const thread2 = try Thread.spawn(start2, &shared_ctx);
344 const thread3 = try Thread.spawn(start2, &shared_ctx);
345 const thread4 = try Thread.spawn(start2, &shared_ctx);
342 const thread1 = try Thread.spawn(.{}, start1, .{});
343 const thread2 = try Thread.spawn(.{}, start2, .{&shared_ctx});
344 const thread3 = try Thread.spawn(.{}, start2, .{&shared_ctx});
345 const thread4 = try Thread.spawn(.{}, start2, .{&shared_ctx});
346346
347 thread1.wait();
348 thread2.wait();
349 thread3.wait();
350 thread4.wait();
347 thread1.join();
348 thread2.join();
349 thread3.join();
350 thread4.join();
351351
352352 try expect(shared_ctx == 4);
353353}
354354
355fn start1(ctx: void) u8 {
356 _ = ctx;
355fn start1() u8 {
357356 return 0;
358357}
359358
......@@ -371,16 +370,15 @@ test "cpu count" {
371370
372371test "thread local storage" {
373372 if (builtin.single_threaded) return error.SkipZigTest;
374 const thread1 = try Thread.spawn(testTls, {});
375 const thread2 = try Thread.spawn(testTls, {});
373 const thread1 = try Thread.spawn(.{}, testTls, .{});
374 const thread2 = try Thread.spawn(.{}, testTls, .{});
376375 try testTls({});
377 thread1.wait();
378 thread2.wait();
376 thread1.join();
377 thread2.join();
379378}
380379
381380threadlocal var x: i32 = 1234;
382fn testTls(context: void) !void {
383 _ = context;
381fn testTls() !void {
384382 if (x != 1234) return error.TlsBadStartValue;
385383 x += 1;
386384 if (x != 1235) return error.TlsBadEndValue;
lib/std/target.zig+19-4
......@@ -69,6 +69,13 @@ pub const Target = struct {
6969 };
7070 }
7171
72 pub fn isBSD(tag: Tag) bool {
73 return tag.isDarwin() or switch (tag) {
74 .kfreebsd, .freebsd, .openbsd, .netbsd, .dragonfly => true,
75 else => false,
76 };
77 }
78
7279 pub fn dynamicLibSuffix(tag: Tag) [:0]const u8 {
7380 if (tag.isDarwin()) {
7481 return ".dylib";
......@@ -787,6 +794,13 @@ pub const Target = struct {
787794 };
788795 }
789796
797 pub fn isAARCH64(arch: Arch) bool {
798 return switch (arch) {
799 .aarch64, .aarch64_be, .aarch64_32 => true,
800 else => false,
801 };
802 }
803
790804 pub fn isThumb(arch: Arch) bool {
791805 return switch (arch) {
792806 .thumb, .thumbeb => true,
......@@ -1365,10 +1379,7 @@ pub const Target = struct {
13651379 }
13661380
13671381 pub fn isAndroid(self: Target) bool {
1368 return switch (self.abi) {
1369 .android => true,
1370 else => false,
1371 };
1382 return self.abi == .android;
13721383 }
13731384
13741385 pub fn isWasm(self: Target) bool {
......@@ -1379,6 +1390,10 @@ pub const Target = struct {
13791390 return self.os.tag.isDarwin();
13801391 }
13811392
1393 pub fn isBSD(self: Target) bool {
1394 return self.os.tag.isBSD();
1395 }
1396
13821397 pub fn isGnuLibC_os_tag_abi(os_tag: Os.Tag, abi: Abi) bool {
13831398 return os_tag == .linux and abi.isGnu();
13841399 }
src/ThreadPool.zig+2-2
......@@ -74,13 +74,13 @@ pub fn init(self: *ThreadPool, allocator: *std.mem.Allocator) !void {
7474 try worker.idle_node.data.init();
7575 errdefer worker.idle_node.data.deinit();
7676
77 worker.thread = try std.Thread.spawn(Worker.run, worker);
77 worker.thread = try std.Thread.spawn(.{}, Worker.run, .{worker});
7878 }
7979}
8080
8181fn destroyWorkers(self: *ThreadPool, spawned: usize) void {
8282 for (self.workers[0..spawned]) |*worker| {
83 worker.thread.wait();
83 worker.thread.join();
8484 worker.idle_node.data.deinit();
8585 }
8686}
tools/update_cpu_features.zig+10-8
......@@ -816,18 +816,20 @@ pub fn main() anyerror!void {
816816 });
817817 }
818818 } else {
819 var threads = try arena.alloc(*std.Thread, llvm_targets.len);
819 var threads = try arena.alloc(std.Thread, llvm_targets.len);
820820 for (llvm_targets) |llvm_target, i| {
821 threads[i] = try std.Thread.spawn(processOneTarget, .{
822 .llvm_tblgen_exe = llvm_tblgen_exe,
823 .llvm_src_root = llvm_src_root,
824 .zig_src_dir = zig_src_dir,
825 .root_progress = root_progress,
826 .llvm_target = llvm_target,
821 threads[i] = try std.Thread.spawn(.{}, processOneTarget, .{
822 Job{
823 .llvm_tblgen_exe = llvm_tblgen_exe,
824 .llvm_src_root = llvm_src_root,
825 .zig_src_dir = zig_src_dir,
826 .root_progress = root_progress,
827 .llvm_target = llvm_target,
828 },
827829 });
828830 }
829831 for (threads) |thread| {
830 thread.wait();
832 thread.join();
831833 }
832834 }
833835}