authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-06 16:48:49-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-06 16:48:49-04:00
log0a3ae9dc6e79e595bc7a78da564f46f6b466abd0
tree180eb112c299ef4da1661e2b1080b37d4e3275fb
parent647fd0f4f1f99bc5bb7783cfc293e026974207c5

fix std.os.Thread.getCurrentId for linux


3 files changed, 18 insertions(+), 16 deletions(-)

std/os/index.zig+7-9
...@@ -160,7 +160,7 @@ test "os.getRandomBytes" {...@@ -160,7 +160,7 @@ test "os.getRandomBytes" {
160 try getRandomBytes(buf_b[0..]);160 try getRandomBytes(buf_b[0..]);
161161
162 // Check if random (not 100% conclusive)162 // Check if random (not 100% conclusive)
163 assert( !mem.eql(u8, buf_a, buf_b) );163 assert(!mem.eql(u8, buf_a, buf_b));
164}164}
165165
166/// Raises a signal in the current kernel thread, ending its execution.166/// Raises a signal in the current kernel thread, ending its execution.
...@@ -2547,22 +2547,20 @@ pub const Thread = struct {...@@ -2547,22 +2547,20 @@ pub const Thread = struct {
2547 };2547 };
25482548
2549 /// Returns the ID of the calling thread.2549 /// Returns the ID of the calling thread.
2550 pub fn currentId() Thread.Id {2550 /// Makes a syscall every time the function is called.
2551 // TODO: As-is, this function is potentially expensive (making a2551 pub fn getCurrentId() Thread.Id {
2552 // syscall on every call). Once we have support for thread-local
2553 // storage (https://github.com/ziglang/zig/issues/924), we could
2554 // memoize it.
2555 if (use_pthreads) {2552 if (use_pthreads) {
2556 return c.pthread_self();2553 return c.pthread_self();
2557 } else return switch (builtin.os) {2554 } else
2558 builtin.Os.linux => linux.getpid(),2555 return switch (builtin.os) {
2556 builtin.Os.linux => linux.gettid(),
2559 builtin.Os.windows => windows.GetCurrentThread(),2557 builtin.Os.windows => windows.GetCurrentThread(),
2560 else => @compileError("Unsupported OS"),2558 else => @compileError("Unsupported OS"),
2561 };2559 };
2562 }2560 }
25632561
2564 /// Returns the ID of this thread.2562 /// Returns the ID of this thread.
2565 pub fn id(self: *const Thread) Thread.Id {2563 pub fn id(self: Thread) Thread.Id {
2566 return self.data.handle;2564 return self.data.handle;
2567 }2565 }
25682566
std/os/linux/index.zig+4
...@@ -947,6 +947,10 @@ pub fn getpid() i32 {...@@ -947,6 +947,10 @@ pub fn getpid() i32 {
947 return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));947 return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));
948}948}
949949
950pub fn gettid() i32 {
951 return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid)));
952}
953
950pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {954pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
951 return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);955 return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
952}956}
std/os/test.zig+7-7
...@@ -34,16 +34,16 @@ test "access file" {...@@ -34,16 +34,16 @@ test "access file" {
34 try os.deleteTree(a, "os_test_tmp");34 try os.deleteTree(a, "os_test_tmp");
35}35}
3636
37fn testThreadIdFn(threadId: *os.Thread.Id) void {37fn testThreadIdFn(thread_id: *os.Thread.Id) void {
38 threadId.* = os.Thread.currentId();38 thread_id.* = os.Thread.getCurrentId();
39}39}
4040
41test "std.os.Thread.currentId" {41test "std.os.Thread.getCurrentId" {
42 var threadCurrentId: os.Thread.Id = undefined;42 var thread_current_id: os.Thread.Id = undefined;
43 const thread = try os.spawnThread(&threadCurrentId, testThreadIdFn);43 const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
44 const threadId = thread.id();44 const thread_id = thread.id();
45 thread.wait();45 thread.wait();
46 assert(threadCurrentId == threadId);46 assert(thread_current_id == thread_id);
47}47}
4848
49test "spawn threads" {49test "spawn threads" {