authorgravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2021-06-30 11:55:52-05:00
committergravatar for kbutcher6200@gmail.comkprotty <kbutcher6200@gmail.com> 2021-06-30 21:49:00-05:00
log2309c81a7812fab071fd2994bb3074edf748de63
tree36fc9d8a47b44d0ef9862d76a925d337e3652b5f
parent2a6ba410209e867ced3b82cf7326960137995853

std.Thread: non-zero child_tid to avoid racy join()


1 files changed, 6 insertions(+), 6 deletions(-)

lib/std/Thread.zig+6-6
......@@ -497,7 +497,7 @@ const LinuxThreadImpl = struct {
497497
498498 const ThreadCompletion = struct {
499499 completion: Completion = Completion.init(.running),
500 child_tid: Atomic(i32) = Atomic(i32).init(0),
500 child_tid: Atomic(i32) = Atomic(i32).init(1),
501501 parent_tid: i32 = undefined,
502502 mapped: []align(std.mem.page_size) u8,
503503 };
......@@ -510,7 +510,7 @@ const LinuxThreadImpl = struct {
510510
511511 fn entryFn(raw_arg: usize) callconv(.C) u8 {
512512 const self = @intToPtr(*@This(), raw_arg);
513 defer switch (self.thread.completion.swap(.completed, .Acquire)) {
513 defer switch (self.thread.completion.swap(.completed, .SeqCst)) {
514514 .running => {},
515515 .completed => unreachable,
516516 .detached => {
......@@ -600,8 +600,8 @@ const LinuxThreadImpl = struct {
600600
601601 const flags: u32 = os.CLONE_THREAD | os.CLONE_DETACHED |
602602 os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES |
603 os.CLONE_SIGHAND | os.CLONE_SYSVSEM | os.CLONE_SETTLS |
604 os.CLONE_PARENT_SETTID | os.CLONE_CHILD_SETTID | os.CLONE_CHILD_CLEARTID;
603 os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
604 os.CLONE_SIGHAND | os.CLONE_SYSVSEM | os.CLONE_SETTLS;
605605
606606 switch (linux.getErrno(linux.clone(
607607 Instance.entryFn,
......@@ -628,7 +628,7 @@ const LinuxThreadImpl = struct {
628628 }
629629
630630 fn detach(self: Impl) void {
631 switch (self.thread.completion.swap(.detached, .AcqRel)) {
631 switch (self.thread.completion.swap(.detached, .SeqCst)) {
632632 .running => {},
633633 .completed => self.join(),
634634 .detached => unreachable,
......@@ -640,7 +640,7 @@ const LinuxThreadImpl = struct {
640640
641641 var spin: u8 = 10;
642642 while (true) {
643 const tid = self.thread.child_tid.load(.Acquire);
643 const tid = self.thread.child_tid.load(.SeqCst);
644644 if (tid == 0) {
645645 break;
646646 }