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.
This commit is contained in:
Andrew Kelley 2019-04-05 16:04:27 -04:00
parent c47c2a2f2a
commit fd65cdc55a
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 8 additions and 2 deletions

View File

@ -2959,6 +2959,10 @@ pub const Thread = struct {
/// Returns the handle of this thread.
/// On Linux and POSIX, this is the same as Id.
/// On Linux, it is possible that the thread spawned with `spawnThread`
/// finishes executing entirely before the clone syscall completes. In this
/// case, this function will return 0 rather than the no-longer-existing thread's
/// pid.
pub fn handle(self: Thread) Handle {
return self.data.handle;
}
@ -2977,7 +2981,7 @@ pub const Thread = struct {
} else switch (builtin.os) {
builtin.Os.linux => {
while (true) {
const pid_value = @atomicLoad(i32, &self.data.handle, builtin.AtomicOrder.SeqCst);
const pid_value = @atomicLoad(i32, &self.data.handle, .SeqCst);
if (pid_value == 0) break;
const rc = linux.futex_wait(&self.data.handle, linux.FUTEX_WAIT, pid_value, null);
switch (linux.getErrno(rc)) {

View File

@ -49,7 +49,9 @@ test "std.os.Thread.getCurrentId" {
switch (builtin.os) {
builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id),
else => {
expect(thread_current_id == thread_id);
// If the thread completes very quickly, then thread_id can be 0. See the
// documentation comments for `std.os.Thread.handle`.
expect(thread_id == 0 or thread_current_id == thread_id);
},
}
}