| author | |
| committer | |
| log | 0a3ae9dc6e79e595bc7a78da564f46f6b466abd0 |
| tree | 180eb112c299ef4da1661e2b1080b37d4e3275fb |
| parent | 647fd0f4f1f99bc5bb7783cfc293e026974207c5 |
3 files changed, 18 insertions(+), 16 deletions(-)
std/os/index.zig+7-9| ... | ... | @@ -160,7 +160,7 @@ test "os.getRandomBytes" { |
| 160 | 160 | try getRandomBytes(buf_b[0..]); |
| 161 | 161 | |
| 162 | 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 | } |
| 165 | 165 | |
| 166 | 166 | /// Raises a signal in the current kernel thread, ending its execution. |
| ... | ... | @@ -2547,22 +2547,20 @@ pub const Thread = struct { |
| 2547 | 2547 | }; |
| 2548 | 2548 | |
| 2549 | 2549 | /// 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 { | |
| 2555 | 2552 | if (use_pthreads) { |
| 2556 | 2553 | 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(), | |
| 2559 | 2557 | builtin.Os.windows => windows.GetCurrentThread(), |
| 2560 | 2558 | else => @compileError("Unsupported OS"), |
| 2561 | 2559 | }; |
| 2562 | 2560 | } |
| 2563 | 2561 | |
| 2564 | 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 | 2564 | return self.data.handle; |
| 2567 | 2565 | } |
| 2568 | 2566 |
std/os/linux/index.zig+4| ... | ... | @@ -947,6 +947,10 @@ pub fn getpid() i32 { |
| 947 | 947 | return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid))); |
| 948 | 948 | } |
| 949 | 949 | |
| 950 | pub fn gettid() i32 { | |
| 951 | return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid))); | |
| 952 | } | |
| 953 | ||
| 950 | 954 | pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize { |
| 951 | 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 | 34 | try os.deleteTree(a, "os_test_tmp"); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | fn testThreadIdFn(threadId: *os.Thread.Id) void { | |
| 38 | threadId.* = os.Thread.currentId(); | |
| 37 | fn testThreadIdFn(thread_id: *os.Thread.Id) void { | |
| 38 | thread_id.* = os.Thread.getCurrentId(); | |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | test "std.os.Thread.currentId" { | |
| 42 | var threadCurrentId: os.Thread.Id = undefined; | |
| 43 | const thread = try os.spawnThread(&threadCurrentId, testThreadIdFn); | |
| 44 | const threadId = thread.id(); | |
| 41 | test "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(); | |
| 45 | 45 | thread.wait(); |
| 46 | assert(threadCurrentId == threadId); | |
| 46 | assert(thread_current_id == thread_id); | |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | test "spawn threads" { |