Commit fd65cdc55a

Andrew Kelley <andrew@ziglang.org>
2019-04-05 22:04:27
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.
1 parent c47c2a2
Changed files (2)
std/os/test.zig
@@ -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);
         },
     }
 }
std/os.zig
@@ -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)) {