aboutsummaryrefslogtreecommitdiff
path: root/std
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-08-06 17:25:24 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-08-06 17:25:24 -0400
commitd2dd29e80c89d8cc530a185e32e9025d0e453bb5 (patch)
tree04c09943d7c685aa812a167053ba788225d0e3d0 /std
parent0a3ae9dc6e79e595bc7a78da564f46f6b466abd0 (diff)
downloadzig-d2dd29e80c89d8cc530a185e32e9025d0e453bb5.tar.gz
zig-d2dd29e80c89d8cc530a185e32e9025d0e453bb5.zip
separate os.Thread.Id and os.Thread.Handle because of windows
Diffstat (limited to 'std')
-rw-r--r--std/os/index.zig27
-rw-r--r--std/os/test.zig9
-rw-r--r--std/os/windows/kernel32.zig1
3 files changed, 26 insertions, 11 deletions
diff --git a/std/os/index.zig b/std/os/index.zig
index c36769c455..6f124df4ed 100644
--- a/std/os/index.zig
+++ b/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;
}
diff --git a/std/os/test.zig b/std/os/test.zig
index 440491380b..ee5f253f7b 100644
--- a/std/os/test.zig
+++ b/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" {
diff --git a/std/os/windows/kernel32.zig b/std/os/windows/kernel32.zig
index a28179f4eb..daeebf1021 100644
--- a/std/os/windows/kernel32.zig
+++ b/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;