Commit d2dd29e80c

Andrew Kelley <superjoe30@gmail.com>
2018-08-06 23:25:24
separate os.Thread.Id and os.Thread.Handle because of windows
1 parent 0a3ae9d
Changed files (3)
std/os/windows/kernel32.zig
@@ -64,6 +64,7 @@ pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out
 pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) DWORD;
 
 pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
+pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
 
 pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
 
std/os/index.zig
@@ -2517,8 +2517,9 @@ pub const Thread = struct {
 
     pub const use_pthreads = is_posix and builtin.link_libc;
 
-    /// An type representing a kernel thread ID.
-    pub const Id = if (use_pthreads)
+    /// Represents a kernel thread handle.
+    /// May be an integer or a pointer depending on the platform.
+    pub const Handle = if (use_pthreads)
         c.pthread_t
     else switch (builtin.os) {
         builtin.Os.linux => i32,
@@ -2526,20 +2527,28 @@ pub const Thread = struct {
         else => @compileError("Unsupported OS"),
     };
 
+    /// Represents a unique ID per thread.
+    /// May be an integer or pointer depending on the platform.
+    /// On Linux and POSIX, this is the same as Handle.
+    pub const Id = switch (builtin.os) {
+        builtin.Os.windows => windows.DWORD,
+        else => Handle,
+    };
+
     pub const Data = if (use_pthreads)
         struct {
-            handle: Thread.Id,
+            handle: Thread.Handle,
             stack_addr: usize,
             stack_len: usize,
         }
     else switch (builtin.os) {
         builtin.Os.linux => struct {
-            handle: Thread.Id,
+            handle: Thread.Handle,
             stack_addr: usize,
             stack_len: usize,
         },
         builtin.Os.windows => struct {
-            handle: Thread.Id,
+            handle: Thread.Handle,
             alloc_start: *c_void,
             heap_handle: windows.HANDLE,
         },
@@ -2548,19 +2557,19 @@ pub const Thread = struct {
 
     /// Returns the ID of the calling thread.
     /// Makes a syscall every time the function is called.
-    pub fn getCurrentId() Thread.Id {
+    pub fn getCurrentId() Id {
         if (use_pthreads) {
             return c.pthread_self();
         } else
             return switch (builtin.os) {
             builtin.Os.linux => linux.gettid(),
-            builtin.Os.windows => windows.GetCurrentThread(),
+            builtin.Os.windows => windows.GetCurrentThreadId(),
             else => @compileError("Unsupported OS"),
         };
     }
 
-    /// Returns the ID of this thread.
-    pub fn id(self: Thread) Thread.Id {
+    /// Returns the handle of this thread.
+    pub fn handle(self: Thread) Thread.Handle {
         return self.data.handle;
     }
 
std/os/test.zig
@@ -41,9 +41,14 @@ fn testThreadIdFn(thread_id: *os.Thread.Id) void {
 test "std.os.Thread.getCurrentId" {
     var thread_current_id: os.Thread.Id = undefined;
     const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
-    const thread_id = thread.id();
     thread.wait();
-    assert(thread_current_id == thread_id);
+    switch (builtin.os) {
+        builtin.Os.windows => assert(os.Thread.getCurrentId() != thread_current_id),
+        else => {
+            const thread_id = thread.handle();
+            assert(thread_current_id == thread_id);
+        },
+    }
 }
 
 test "spawn threads" {