authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-06 17:25:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-06 17:25:24-04:00
logd2dd29e80c89d8cc530a185e32e9025d0e453bb5
tree04c09943d7c685aa812a167053ba788225d0e3d0
parent0a3ae9dc6e79e595bc7a78da564f46f6b466abd0

separate os.Thread.Id and os.Thread.Handle because of windows


3 files changed, 26 insertions(+), 11 deletions(-)

std/os/index.zig+18-9
...@@ -2517,8 +2517,9 @@ pub const Thread = struct {...@@ -2517,8 +2517,9 @@ pub const Thread = struct {
25172517
2518 pub const use_pthreads = is_posix and builtin.link_libc;2518 pub const use_pthreads = is_posix and builtin.link_libc;
25192519
2520 /// An type representing a kernel thread ID.2520 /// Represents a kernel thread handle.
2521 pub const Id = if (use_pthreads)2521 /// May be an integer or a pointer depending on the platform.
2522 pub const Handle = if (use_pthreads)
2522 c.pthread_t2523 c.pthread_t
2523 else switch (builtin.os) {2524 else switch (builtin.os) {
2524 builtin.Os.linux => i32,2525 builtin.Os.linux => i32,
...@@ -2526,20 +2527,28 @@ pub const Thread = struct {...@@ -2526,20 +2527,28 @@ pub const Thread = struct {
2526 else => @compileError("Unsupported OS"),2527 else => @compileError("Unsupported OS"),
2527 };2528 };
25282529
2530 /// Represents a unique ID per thread.
2531 /// May be an integer or pointer depending on the platform.
2532 /// On Linux and POSIX, this is the same as Handle.
2533 pub const Id = switch (builtin.os) {
2534 builtin.Os.windows => windows.DWORD,
2535 else => Handle,
2536 };
2537
2529 pub const Data = if (use_pthreads)2538 pub const Data = if (use_pthreads)
2530 struct {2539 struct {
2531 handle: Thread.Id,2540 handle: Thread.Handle,
2532 stack_addr: usize,2541 stack_addr: usize,
2533 stack_len: usize,2542 stack_len: usize,
2534 }2543 }
2535 else switch (builtin.os) {2544 else switch (builtin.os) {
2536 builtin.Os.linux => struct {2545 builtin.Os.linux => struct {
2537 handle: Thread.Id,2546 handle: Thread.Handle,
2538 stack_addr: usize,2547 stack_addr: usize,
2539 stack_len: usize,2548 stack_len: usize,
2540 },2549 },
2541 builtin.Os.windows => struct {2550 builtin.Os.windows => struct {
2542 handle: Thread.Id,2551 handle: Thread.Handle,
2543 alloc_start: *c_void,2552 alloc_start: *c_void,
2544 heap_handle: windows.HANDLE,2553 heap_handle: windows.HANDLE,
2545 },2554 },
...@@ -2548,19 +2557,19 @@ pub const Thread = struct {...@@ -2548,19 +2557,19 @@ pub const Thread = struct {
25482557
2549 /// Returns the ID of the calling thread.2558 /// Returns the ID of the calling thread.
2550 /// Makes a syscall every time the function is called.2559 /// Makes a syscall every time the function is called.
2551 pub fn getCurrentId() Thread.Id {2560 pub fn getCurrentId() Id {
2552 if (use_pthreads) {2561 if (use_pthreads) {
2553 return c.pthread_self();2562 return c.pthread_self();
2554 } else2563 } else
2555 return switch (builtin.os) {2564 return switch (builtin.os) {
2556 builtin.Os.linux => linux.gettid(),2565 builtin.Os.linux => linux.gettid(),
2557 builtin.Os.windows => windows.GetCurrentThread(),2566 builtin.Os.windows => windows.GetCurrentThreadId(),
2558 else => @compileError("Unsupported OS"),2567 else => @compileError("Unsupported OS"),
2559 };2568 };
2560 }2569 }
25612570
2562 /// Returns the ID of this thread.2571 /// Returns the handle of this thread.
2563 pub fn id(self: Thread) Thread.Id {2572 pub fn handle(self: Thread) Thread.Handle {
2564 return self.data.handle;2573 return self.data.handle;
2565 }2574 }
25662575
std/os/test.zig+7-2
...@@ -41,9 +41,14 @@ fn testThreadIdFn(thread_id: *os.Thread.Id) void {...@@ -41,9 +41,14 @@ fn testThreadIdFn(thread_id: *os.Thread.Id) void {
41test "std.os.Thread.getCurrentId" {41test "std.os.Thread.getCurrentId" {
42 var thread_current_id: os.Thread.Id = undefined;42 var thread_current_id: os.Thread.Id = undefined;
43 const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);43 const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
44 const thread_id = thread.id();
45 thread.wait();44 thread.wait();
46 assert(thread_current_id == thread_id);45 switch (builtin.os) {
46 builtin.Os.windows => assert(os.Thread.getCurrentId() != thread_current_id),
47 else => {
48 const thread_id = thread.handle();
49 assert(thread_current_id == thread_id);
50 },
51 }
47}52}
4853
49test "spawn threads" {54test "spawn threads" {
std/os/windows/kernel32.zig+1
...@@ -64,6 +64,7 @@ pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out...@@ -64,6 +64,7 @@ pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out
64pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) DWORD;64pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) DWORD;
6565
66pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;66pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
67pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
6768
68pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;69pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
6970