Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread ID support to std.os.Thread (fixes #1316) #1330

Merged
merged 3 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions std/c/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub extern "pthread" fn pthread_create(noalias newthread: *pthread_t, noalias at
pub extern "pthread" fn pthread_attr_init(attr: *pthread_attr_t) c_int;
pub extern "pthread" fn pthread_attr_setstack(attr: *pthread_attr_t, stackaddr: *c_void, stacksize: usize) c_int;
pub extern "pthread" fn pthread_attr_destroy(attr: *pthread_attr_t) c_int;
pub extern "pthread" fn pthread_self() pthread_t;
pub extern "pthread" fn pthread_join(thread: pthread_t, arg_return: ?*?*c_void) c_int;

pub const pthread_t = *@OpaqueType();
42 changes: 36 additions & 6 deletions std/os/index.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2516,26 +2516,56 @@ pub const Thread = struct {
data: Data,

pub const use_pthreads = is_posix and builtin.link_libc;

/// An opaque type representing a kernel thread ID.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove/update this comment because it does not match the code now.

pub const Id = if (use_pthreads)
c.pthread_t
else switch (builtin.os) {
builtin.Os.linux => i32,
builtin.Os.windows => windows.HANDLE,
else => @compileError("Unsupported OS"),
};

pub const Data = if (use_pthreads)
struct {
handle: c.pthread_t,
handle: Thread.Id,
stack_addr: usize,
stack_len: usize,
}
else switch (builtin.os) {
builtin.Os.linux => struct {
pid: i32,
handle: Thread.Id,
stack_addr: usize,
stack_len: usize,
},
builtin.Os.windows => struct {
handle: windows.HANDLE,
handle: Thread.Id,
alloc_start: *c_void,
heap_handle: windows.HANDLE,
},
else => @compileError("Unsupported OS"),
};

/// Returns the ID of the calling thread.
pub fn currentId() Thread.Id {
// TODO: As-is, this function is potentially expensive (making a
// syscall on every call). Once we have support for thread-local
// storage (https://github.com/ziglang/zig/issues/924), we could
// memoize it.
if (use_pthreads) {
return c.pthread_self();
} else return switch (builtin.os) {
builtin.Os.linux => linux.getpid(),
builtin.Os.windows => windows.GetCurrentThread(),
else => @compileError("Unsupported OS"),
};
}

/// Returns the ID of this thread.
pub fn id(self: *const Thread) Thread.Id {
return self.data.handle;
}

pub fn wait(self: *const Thread) void {
if (use_pthreads) {
const err = c.pthread_join(self.data.handle, null);
Expand All @@ -2550,9 +2580,9 @@ pub const Thread = struct {
} else switch (builtin.os) {
builtin.Os.linux => {
while (true) {
const pid_value = @atomicLoad(i32, &self.data.pid, builtin.AtomicOrder.SeqCst);
const pid_value = @atomicLoad(i32, &self.data.handle, builtin.AtomicOrder.SeqCst);
if (pid_value == 0) break;
const rc = linux.futex_wait(@ptrToInt(&self.data.pid), linux.FUTEX_WAIT, pid_value, null);
const rc = linux.futex_wait(@ptrToInt(&self.data.handle), linux.FUTEX_WAIT, pid_value, null);
switch (linux.getErrno(rc)) {
0 => continue,
posix.EINTR => continue,
Expand Down Expand Up @@ -2734,7 +2764,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
// use linux API directly. TODO use posix.CLONE_SETTLS and initialize thread local storage correctly
const flags = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND | posix.CLONE_THREAD | posix.CLONE_SYSVSEM | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID | posix.CLONE_DETACHED;
const newtls: usize = 0;
const rc = posix.clone(MainFuncs.linuxThreadMain, stack_end, flags, arg, &thread_ptr.data.pid, newtls, &thread_ptr.data.pid);
const rc = posix.clone(MainFuncs.linuxThreadMain, stack_end, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle);
const err = posix.getErrno(rc);
switch (err) {
0 => return thread_ptr,
Expand Down
12 changes: 12 additions & 0 deletions std/os/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ test "access file" {
try os.deleteTree(a, "os_test_tmp");
}

fn testThreadIdFn(threadId: *?os.Thread.Id) void {
threadId.* = os.Thread.currentId();
}

test "std.os.Thread.currentId" {
var threadCurrentId: ?os.Thread.Id = null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.Thread.currentId() should always return os.Thread.Id, so no need to make it optional.

fn testThreadIdFn(threadId: *?os.Thread.Id) void {
to
fn testThreadIdFn(threadId: *os.Thread.Id) void {

const thread = try os.spawnThread(&threadCurrentId, testThreadIdFn);
const threadId = thread.id();
thread.wait();
assert(threadCurrentId == threadId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you fix the above, this should work.

}

test "spawn threads" {
var shared_ctx: i32 = 1;

Expand Down
2 changes: 2 additions & 0 deletions std/os/windows/kernel32.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ 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 GetEnvironmentStringsA() ?[*]u8;

pub extern "kernel32" stdcallcc fn GetEnvironmentVariableA(lpName: LPCSTR, lpBuffer: LPSTR, nSize: DWORD) DWORD;
Expand Down