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 {
25172517
25182518 pub const use_pthreads = is_posix and builtin.link_libc;
25192519
2520 /// An type representing a kernel thread ID.
2521 pub const Id = if (use_pthreads)
2520 /// Represents a kernel thread handle.
2521 /// May be an integer or a pointer depending on the platform.
2522 pub const Handle = if (use_pthreads)
25222523 c.pthread_t
25232524 else switch (builtin.os) {
25242525 builtin.Os.linux => i32,
......@@ -2526,20 +2527,28 @@ pub const Thread = struct {
25262527 else => @compileError("Unsupported OS"),
25272528 };
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
25292538 pub const Data = if (use_pthreads)
25302539 struct {
2531 handle: Thread.Id,
2540 handle: Thread.Handle,
25322541 stack_addr: usize,
25332542 stack_len: usize,
25342543 }
25352544 else switch (builtin.os) {
25362545 builtin.Os.linux => struct {
2537 handle: Thread.Id,
2546 handle: Thread.Handle,
25382547 stack_addr: usize,
25392548 stack_len: usize,
25402549 },
25412550 builtin.Os.windows => struct {
2542 handle: Thread.Id,
2551 handle: Thread.Handle,
25432552 alloc_start: *c_void,
25442553 heap_handle: windows.HANDLE,
25452554 },
......@@ -2548,19 +2557,19 @@ pub const Thread = struct {
25482557
25492558 /// Returns the ID of the calling thread.
25502559 /// Makes a syscall every time the function is called.
2551 pub fn getCurrentId() Thread.Id {
2560 pub fn getCurrentId() Id {
25522561 if (use_pthreads) {
25532562 return c.pthread_self();
25542563 } else
25552564 return switch (builtin.os) {
25562565 builtin.Os.linux => linux.gettid(),
2557 builtin.Os.windows => windows.GetCurrentThread(),
2566 builtin.Os.windows => windows.GetCurrentThreadId(),
25582567 else => @compileError("Unsupported OS"),
25592568 };
25602569 }
25612570
2562 /// Returns the ID of this thread.
2563 pub fn id(self: Thread) Thread.Id {
2571 /// Returns the handle of this thread.
2572 pub fn handle(self: Thread) Thread.Handle {
25642573 return self.data.handle;
25652574 }
25662575
std/os/test.zig+7-2
......@@ -41,9 +41,14 @@ fn testThreadIdFn(thread_id: *os.Thread.Id) void {
4141test "std.os.Thread.getCurrentId" {
4242 var thread_current_id: os.Thread.Id = undefined;
4343 const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
44 const thread_id = thread.id();
4544 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 }
4752}
4853
4954test "spawn threads" {
std/os/windows/kernel32.zig+1
......@@ -64,6 +64,7 @@ pub extern "kernel32" stdcallcc fn GetConsoleMode(in_hConsoleHandle: HANDLE, out
6464pub extern "kernel32" stdcallcc fn GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) DWORD;
6565
6666pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
67pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
6768
6869pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
6970