fix Thread impl on Linux and add docs

This commit is contained in:
Andrew Kelley 2018-08-06 17:30:55 -04:00
parent d2dd29e80c
commit 24d74cbf44
2 changed files with 5 additions and 2 deletions

View File

@ -2519,6 +2519,7 @@ pub const Thread = struct {
/// Represents a kernel thread handle.
/// May be an integer or a pointer depending on the platform.
/// On Linux and POSIX, this is the same as Id.
pub const Handle = if (use_pthreads)
c.pthread_t
else switch (builtin.os) {
@ -2557,6 +2558,7 @@ pub const Thread = struct {
/// Returns the ID of the calling thread.
/// Makes a syscall every time the function is called.
/// On Linux and POSIX, this Id is the same as a Handle.
pub fn getCurrentId() Id {
if (use_pthreads) {
return c.pthread_self();
@ -2569,7 +2571,8 @@ pub const Thread = struct {
}
/// Returns the handle of this thread.
pub fn handle(self: Thread) Thread.Handle {
/// On Linux and POSIX, this is the same as Id.
pub fn handle(self: Thread) Handle {
return self.data.handle;
}

View File

@ -41,11 +41,11 @@ 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.handle();
thread.wait();
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);
},
}