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" {
160160 try getRandomBytes(buf_b[0..]);
161161
162162 // Check if random (not 100% conclusive)
163 assert( !mem.eql(u8, buf_a, buf_b) );
163 assert(!mem.eql(u8, buf_a, buf_b));
164164}
165165
166166/// Raises a signal in the current kernel thread, ending its execution.
......@@ -2547,22 +2547,20 @@ pub const Thread = struct {
25472547 };
25482548
25492549 /// Returns the ID of the calling thread.
2550 pub fn currentId() Thread.Id {
2551 // TODO: As-is, this function is potentially expensive (making a
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.
2550 /// Makes a syscall every time the function is called.
2551 pub fn getCurrentId() Thread.Id {
25552552 if (use_pthreads) {
25562553 return c.pthread_self();
2557 } else return switch (builtin.os) {
2558 builtin.Os.linux => linux.getpid(),
2554 } else
2555 return switch (builtin.os) {
2556 builtin.Os.linux => linux.gettid(),
25592557 builtin.Os.windows => windows.GetCurrentThread(),
25602558 else => @compileError("Unsupported OS"),
25612559 };
25622560 }
25632561
25642562 /// Returns the ID of this thread.
2565 pub fn id(self: *const Thread) Thread.Id {
2563 pub fn id(self: Thread) Thread.Id {
25662564 return self.data.handle;
25672565 }
25682566
std/os/linux/index.zig+4
......@@ -947,6 +947,10 @@ pub fn getpid() i32 {
947947 return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));
948948}
949949
950pub fn gettid() i32 {
951 return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid)));
952}
953
950954pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
951955 return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
952956}
std/os/test.zig+7-7
......@@ -34,16 +34,16 @@ test "access file" {
3434 try os.deleteTree(a, "os_test_tmp");
3535}
3636
37fn testThreadIdFn(threadId: *os.Thread.Id) void {
38 threadId.* = os.Thread.currentId();
37fn testThreadIdFn(thread_id: *os.Thread.Id) void {
38 thread_id.* = os.Thread.getCurrentId();
3939}
4040
41test "std.os.Thread.currentId" {
42 var threadCurrentId: os.Thread.Id = undefined;
43 const thread = try os.spawnThread(&threadCurrentId, testThreadIdFn);
44 const threadId = thread.id();
41test "std.os.Thread.getCurrentId" {
42 var thread_current_id: os.Thread.Id = undefined;
43 const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
44 const thread_id = thread.id();
4545 thread.wait();
46 assert(threadCurrentId == threadId);
46 assert(thread_current_id == thread_id);
4747}
4848
4949test "spawn threads" {