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 {...@@ -497,7 +497,7 @@ const LinuxThreadImpl = struct {
497497
498 const ThreadCompletion = struct {498 const ThreadCompletion = struct {
499 completion: Completion = Completion.init(.running),499 completion: Completion = Completion.init(.running),
500 child_tid: Atomic(i32) = Atomic(i32).init(0),500 child_tid: Atomic(i32) = Atomic(i32).init(1),
501 parent_tid: i32 = undefined,501 parent_tid: i32 = undefined,
502 mapped: []align(std.mem.page_size) u8,502 mapped: []align(std.mem.page_size) u8,
503 };503 };
...@@ -510,7 +510,7 @@ const LinuxThreadImpl = struct {...@@ -510,7 +510,7 @@ const LinuxThreadImpl = struct {
510510
511 fn entryFn(raw_arg: usize) callconv(.C) u8 {511 fn entryFn(raw_arg: usize) callconv(.C) u8 {
512 const self = @intToPtr(*@This(), raw_arg);512 const self = @intToPtr(*@This(), raw_arg);
513 defer switch (self.thread.completion.swap(.completed, .Acquire)) {513 defer switch (self.thread.completion.swap(.completed, .SeqCst)) {
514 .running => {},514 .running => {},
515 .completed => unreachable,515 .completed => unreachable,
516 .detached => {516 .detached => {
...@@ -600,8 +600,8 @@ const LinuxThreadImpl = struct {...@@ -600,8 +600,8 @@ const LinuxThreadImpl = struct {
600600
601 const flags: u32 = os.CLONE_THREAD | os.CLONE_DETACHED |601 const flags: u32 = os.CLONE_THREAD | os.CLONE_DETACHED |
602 os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES |602 os.CLONE_VM | os.CLONE_FS | os.CLONE_FILES |
603 os.CLONE_SIGHAND | os.CLONE_SYSVSEM | os.CLONE_SETTLS |603 os.CLONE_PARENT_SETTID | os.CLONE_CHILD_CLEARTID |
604 os.CLONE_PARENT_SETTID | os.CLONE_CHILD_SETTID | os.CLONE_CHILD_CLEARTID;604 os.CLONE_SIGHAND | os.CLONE_SYSVSEM | os.CLONE_SETTLS;
605605
606 switch (linux.getErrno(linux.clone(606 switch (linux.getErrno(linux.clone(
607 Instance.entryFn,607 Instance.entryFn,
...@@ -628,7 +628,7 @@ const LinuxThreadImpl = struct {...@@ -628,7 +628,7 @@ const LinuxThreadImpl = struct {
628 }628 }
629629
630 fn detach(self: Impl) void {630 fn detach(self: Impl) void {
631 switch (self.thread.completion.swap(.detached, .AcqRel)) {631 switch (self.thread.completion.swap(.detached, .SeqCst)) {
632 .running => {},632 .running => {},
633 .completed => self.join(),633 .completed => self.join(),
634 .detached => unreachable,634 .detached => unreachable,
...@@ -640,7 +640,7 @@ const LinuxThreadImpl = struct {...@@ -640,7 +640,7 @@ const LinuxThreadImpl = struct {
640640
641 var spin: u8 = 10;641 var spin: u8 = 10;
642 while (true) {642 while (true) {
643 const tid = self.thread.child_tid.load(.Acquire);643 const tid = self.thread.child_tid.load(.SeqCst);
644 if (tid == 0) {644 if (tid == 0) {
645 break;645 break;
646 }646 }