authorgravatar for kris.tate+github@gmail.comkristopher tate <kris.tate+github@gmail.com> 2018-08-05 03:38:51+09:00
committergravatar for mdsteele@alum.mit.eduMatthew D. Steele <mdsteele@alum.mit.edu> 2018-08-04 14:38:51-04:00
loga25824e033d11a8ada0d444efab9ac08a93185af
treef1d4d73e85ca4c73487e3c927b5842388e8d799a
parent86d1cc8e2fffed9c93e41b28605d764e6b1749dc

zig/std/os/index.zig: clean-up thread id; (#1)

Ref #1316 #1330

1 files changed, 18 insertions(+), 21 deletions(-)

std/os/index.zig+18-21
...@@ -2518,22 +2518,28 @@ pub const Thread = struct {...@@ -2518,22 +2518,28 @@ pub const Thread = struct {
2518 pub const use_pthreads = is_posix and builtin.link_libc;2518 pub const use_pthreads = is_posix and builtin.link_libc;
25192519
2520 /// An opaque type representing a kernel thread ID.2520 /// An opaque type representing a kernel thread ID.
2521 pub const Id = *@OpaqueType();2521 pub const Id = if (use_pthreads)
2522 c.pthread_t
2523 else switch (builtin.os) {
2524 builtin.Os.linux => i32,
2525 builtin.Os.windows => windows.HANDLE,
2526 else => @compileError("Unsupported OS"),
2527 };
25222528
2523 pub const Data = if (use_pthreads)2529 pub const Data = if (use_pthreads)
2524 struct {2530 struct {
2525 handle: c.pthread_t,2531 handle: Thread.Id,
2526 stack_addr: usize,2532 stack_addr: usize,
2527 stack_len: usize,2533 stack_len: usize,
2528 }2534 }
2529 else switch (builtin.os) {2535 else switch (builtin.os) {
2530 builtin.Os.linux => struct {2536 builtin.Os.linux => struct {
2531 pid: i32,2537 handle: Thread.Id,
2532 stack_addr: usize,2538 stack_addr: usize,
2533 stack_len: usize,2539 stack_len: usize,
2534 },2540 },
2535 builtin.Os.windows => struct {2541 builtin.Os.windows => struct {
2536 handle: windows.HANDLE,2542 handle: Thread.Id,
2537 alloc_start: *c_void,2543 alloc_start: *c_void,
2538 heap_handle: windows.HANDLE,2544 heap_handle: windows.HANDLE,
2539 },2545 },
...@@ -2547,26 +2553,17 @@ pub const Thread = struct {...@@ -2547,26 +2553,17 @@ pub const Thread = struct {
2547 // storage (https://github.com/ziglang/zig/issues/924), we could2553 // storage (https://github.com/ziglang/zig/issues/924), we could
2548 // memoize it.2554 // memoize it.
2549 if (use_pthreads) {2555 if (use_pthreads) {
2550 return @ptrCast(Thread.Id, c.pthread_self());2556 return c.pthread_self();
2551 } else return switch (builtin.os) {2557 } else return switch (builtin.os) {
2552 builtin.Os.linux =>2558 builtin.Os.linux => linux.getpid(),
2553 @intToPtr(Thread.Id, @bitCast(u32, linux.getpid())),2559 builtin.Os.windows => windows.GetCurrentThread(),
2554 builtin.Os.windows =>
2555 @ptrCast(Thread.Id, windows.GetCurrentThread()),
2556 else => @compileError("Unsupported OS"),2560 else => @compileError("Unsupported OS"),
2557 };2561 };
2558 }2562 }
25592563
2560 /// Returns the ID of this thread object.2564 /// Returns the ID of this thread.
2561 pub fn id(self: *const Thread) Thread.Id {2565 pub fn id(self: *const Thread) Thread.Id {
2562 if (use_pthreads) {2566 return self.data.handle;
2563 return @ptrCast(Thread.Id, self.data.handle);
2564 } else return switch (builtin.os) {
2565 builtin.Os.linux =>
2566 @intToPtr(Thread.Id, @bitCast(u32, self.data.pid)),
2567 builtin.Os.windows => @ptrCast(Thread.Id, self.data.handle),
2568 else => @compileError("Unsupported OS"),
2569 };
2570 }2567 }
25712568
2572 pub fn wait(self: *const Thread) void {2569 pub fn wait(self: *const Thread) void {
...@@ -2583,9 +2580,9 @@ pub const Thread = struct {...@@ -2583,9 +2580,9 @@ pub const Thread = struct {
2583 } else switch (builtin.os) {2580 } else switch (builtin.os) {
2584 builtin.Os.linux => {2581 builtin.Os.linux => {
2585 while (true) {2582 while (true) {
2586 const pid_value = @atomicLoad(i32, &self.data.pid, builtin.AtomicOrder.SeqCst);2583 const pid_value = @atomicLoad(i32, &self.data.handle, builtin.AtomicOrder.SeqCst);
2587 if (pid_value == 0) break;2584 if (pid_value == 0) break;
2588 const rc = linux.futex_wait(@ptrToInt(&self.data.pid), linux.FUTEX_WAIT, pid_value, null);2585 const rc = linux.futex_wait(@ptrToInt(&self.data.handle), linux.FUTEX_WAIT, pid_value, null);
2589 switch (linux.getErrno(rc)) {2586 switch (linux.getErrno(rc)) {
2590 0 => continue,2587 0 => continue,
2591 posix.EINTR => continue,2588 posix.EINTR => continue,
...@@ -2767,7 +2764,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread...@@ -2767,7 +2764,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread
2767 // use linux API directly. TODO use posix.CLONE_SETTLS and initialize thread local storage correctly2764 // use linux API directly. TODO use posix.CLONE_SETTLS and initialize thread local storage correctly
2768 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;2765 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;
2769 const newtls: usize = 0;2766 const newtls: usize = 0;
2770 const rc = posix.clone(MainFuncs.linuxThreadMain, stack_end, flags, arg, &thread_ptr.data.pid, newtls, &thread_ptr.data.pid);2767 const rc = posix.clone(MainFuncs.linuxThreadMain, stack_end, flags, arg, &thread_ptr.data.handle, newtls, &thread_ptr.data.handle);
2771 const err = posix.getErrno(rc);2768 const err = posix.getErrno(rc);
2772 switch (err) {2769 switch (err) {
2773 0 => return thread_ptr,2770 0 => return thread_ptr,