authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-05 16:04:27-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-05 16:06:35-04:00
logfd65cdc55a21640cf67a4a1b6ff8b58ad8c390fd
treea11207e17878cf28be03a1076bdfe14603407e7a
parentc47c2a2f2a518c8878a081dd30a631f5bc21eefa
signaturelock-open Commit is signed but in an unrecognized format.

fix incorrect Thread.getCurrentId test

Documentation comments copied here: On Linux, it is possible that the thread spawned with `spawnThread` finishes executing entirely before the clone syscall completes. In this case, `std.os.Thread.handle` will return 0 rather than the no-longer-existing thread's pid.

2 files changed, 8 insertions(+), 2 deletions(-)

std/os.zig+5-1
......@@ -2959,6 +2959,10 @@ pub const Thread = struct {
29592959
29602960 /// Returns the handle of this thread.
29612961 /// On Linux and POSIX, this is the same as Id.
2962 /// On Linux, it is possible that the thread spawned with `spawnThread`
2963 /// finishes executing entirely before the clone syscall completes. In this
2964 /// case, this function will return 0 rather than the no-longer-existing thread's
2965 /// pid.
29622966 pub fn handle(self: Thread) Handle {
29632967 return self.data.handle;
29642968 }
......@@ -2977,7 +2981,7 @@ pub const Thread = struct {
29772981 } else switch (builtin.os) {
29782982 builtin.Os.linux => {
29792983 while (true) {
2980 const pid_value = @atomicLoad(i32, &self.data.handle, builtin.AtomicOrder.SeqCst);
2984 const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst);
29812985 if (pid_value == 0) break;
29822986 const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null);
29832987 switch (linux.getErrno(rc)) {
std/os/test.zig+3-1
......@@ -49,7 +49,9 @@ test "std.os.Thread.getCurrentId" {
4949 switch (builtin.os) {
5050 builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id),
5151 else => {
52 expect(thread_current_id == thread_id);
52 // If the thread completes very quickly, then thread_id can be 0. See the
53 // documentation comments for `std.os.Thread.handle`.
54 expect(thread_id == 0 or thread_current_id == thread_id);
5355 },
5456 }
5557}