separate os.Thread.Id and os.Thread.Handle because of windows
This commit is contained in:
parent
0a3ae9dc6e
commit
d2dd29e80c
@ -2517,8 +2517,9 @@ pub const Thread = struct {
|
|||||||
|
|
||||||
pub const use_pthreads = is_posix and builtin.link_libc;
|
pub const use_pthreads = is_posix and builtin.link_libc;
|
||||||
|
|
||||||
/// An type representing a kernel thread ID.
|
/// Represents a kernel thread handle.
|
||||||
pub const Id = if (use_pthreads)
|
/// May be an integer or a pointer depending on the platform.
|
||||||
|
pub const Handle = if (use_pthreads)
|
||||||
c.pthread_t
|
c.pthread_t
|
||||||
else switch (builtin.os) {
|
else switch (builtin.os) {
|
||||||
builtin.Os.linux => i32,
|
builtin.Os.linux => i32,
|
||||||
@ -2526,20 +2527,28 @@ pub const Thread = struct {
|
|||||||
else => @compileError("Unsupported OS"),
|
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)
|
pub const Data = if (use_pthreads)
|
||||||
struct {
|
struct {
|
||||||
handle: Thread.Id,
|
handle: Thread.Handle,
|
||||||
stack_addr: usize,
|
stack_addr: usize,
|
||||||
stack_len: usize,
|
stack_len: usize,
|
||||||
}
|
}
|
||||||
else switch (builtin.os) {
|
else switch (builtin.os) {
|
||||||
builtin.Os.linux => struct {
|
builtin.Os.linux => struct {
|
||||||
handle: Thread.Id,
|
handle: Thread.Handle,
|
||||||
stack_addr: usize,
|
stack_addr: usize,
|
||||||
stack_len: usize,
|
stack_len: usize,
|
||||||
},
|
},
|
||||||
builtin.Os.windows => struct {
|
builtin.Os.windows => struct {
|
||||||
handle: Thread.Id,
|
handle: Thread.Handle,
|
||||||
alloc_start: *c_void,
|
alloc_start: *c_void,
|
||||||
heap_handle: windows.HANDLE,
|
heap_handle: windows.HANDLE,
|
||||||
},
|
},
|
||||||
@ -2548,19 +2557,19 @@ pub const Thread = struct {
|
|||||||
|
|
||||||
/// Returns the ID of the calling thread.
|
/// Returns the ID of the calling thread.
|
||||||
/// Makes a syscall every time the function is called.
|
/// Makes a syscall every time the function is called.
|
||||||
pub fn getCurrentId() Thread.Id {
|
pub fn getCurrentId() Id {
|
||||||
if (use_pthreads) {
|
if (use_pthreads) {
|
||||||
return c.pthread_self();
|
return c.pthread_self();
|
||||||
} else
|
} else
|
||||||
return switch (builtin.os) {
|
return switch (builtin.os) {
|
||||||
builtin.Os.linux => linux.gettid(),
|
builtin.Os.linux => linux.gettid(),
|
||||||
builtin.Os.windows => windows.GetCurrentThread(),
|
builtin.Os.windows => windows.GetCurrentThreadId(),
|
||||||
else => @compileError("Unsupported OS"),
|
else => @compileError("Unsupported OS"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the ID of this thread.
|
/// Returns the handle of this thread.
|
||||||
pub fn id(self: Thread) Thread.Id {
|
pub fn handle(self: Thread) Thread.Handle {
|
||||||
return self.data.handle;
|
return self.data.handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,9 +41,14 @@ fn testThreadIdFn(thread_id: *os.Thread.Id) void {
|
|||||||
test "std.os.Thread.getCurrentId" {
|
test "std.os.Thread.getCurrentId" {
|
||||||
var thread_current_id: os.Thread.Id = undefined;
|
var thread_current_id: os.Thread.Id = undefined;
|
||||||
const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
|
const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
|
||||||
const thread_id = thread.id();
|
|
||||||
thread.wait();
|
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" {
|
test "spawn threads" {
|
||||||
|
@ -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 GetCurrentDirectoryA(nBufferLength: WORD, lpBuffer: ?LPSTR) DWORD;
|
||||||
|
|
||||||
pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
|
pub extern "kernel32" stdcallcc fn GetCurrentThread() HANDLE;
|
||||||
|
pub extern "kernel32" stdcallcc fn GetCurrentThreadId() DWORD;
|
||||||
|
|
||||||
pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
|
pub extern "kernel32" stdcallcc fn GetEnvironmentStringsA() ?[*]u8;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user