mirror of
https://github.com/ziglang/zig.git
synced 2026-02-21 16:54:52 +00:00
fix std.os.Thread.getCurrentId for linux
This commit is contained in:
parent
647fd0f4f1
commit
0a3ae9dc6e
@ -160,7 +160,7 @@ test "os.getRandomBytes" {
|
||||
try getRandomBytes(buf_b[0..]);
|
||||
|
||||
// Check if random (not 100% conclusive)
|
||||
assert( !mem.eql(u8, buf_a, buf_b) );
|
||||
assert(!mem.eql(u8, buf_a, buf_b));
|
||||
}
|
||||
|
||||
/// Raises a signal in the current kernel thread, ending its execution.
|
||||
@ -2547,22 +2547,20 @@ pub const Thread = struct {
|
||||
};
|
||||
|
||||
/// 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.
|
||||
/// Makes a syscall every time the function is called.
|
||||
pub fn getCurrentId() Thread.Id {
|
||||
if (use_pthreads) {
|
||||
return c.pthread_self();
|
||||
} else return switch (builtin.os) {
|
||||
builtin.Os.linux => linux.getpid(),
|
||||
} else
|
||||
return switch (builtin.os) {
|
||||
builtin.Os.linux => linux.gettid(),
|
||||
builtin.Os.windows => windows.GetCurrentThread(),
|
||||
else => @compileError("Unsupported OS"),
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns the ID of this thread.
|
||||
pub fn id(self: *const Thread) Thread.Id {
|
||||
pub fn id(self: Thread) Thread.Id {
|
||||
return self.data.handle;
|
||||
}
|
||||
|
||||
|
||||
@ -947,6 +947,10 @@ pub fn getpid() i32 {
|
||||
return @bitCast(i32, @truncate(u32, syscall0(SYS_getpid)));
|
||||
}
|
||||
|
||||
pub fn gettid() i32 {
|
||||
return @bitCast(i32, @truncate(u32, syscall0(SYS_gettid)));
|
||||
}
|
||||
|
||||
pub fn sigprocmask(flags: u32, noalias set: *const sigset_t, noalias oldset: ?*sigset_t) usize {
|
||||
return syscall4(SYS_rt_sigprocmask, flags, @ptrToInt(set), @ptrToInt(oldset), NSIG / 8);
|
||||
}
|
||||
|
||||
@ -34,16 +34,16 @@ test "access file" {
|
||||
try os.deleteTree(a, "os_test_tmp");
|
||||
}
|
||||
|
||||
fn testThreadIdFn(threadId: *os.Thread.Id) void {
|
||||
threadId.* = os.Thread.currentId();
|
||||
fn testThreadIdFn(thread_id: *os.Thread.Id) void {
|
||||
thread_id.* = os.Thread.getCurrentId();
|
||||
}
|
||||
|
||||
test "std.os.Thread.currentId" {
|
||||
var threadCurrentId: os.Thread.Id = undefined;
|
||||
const thread = try os.spawnThread(&threadCurrentId, testThreadIdFn);
|
||||
const threadId = thread.id();
|
||||
test "std.os.Thread.getCurrentId" {
|
||||
var thread_current_id: os.Thread.Id = undefined;
|
||||
const thread = try os.spawnThread(&thread_current_id, testThreadIdFn);
|
||||
const thread_id = thread.id();
|
||||
thread.wait();
|
||||
assert(threadCurrentId == threadId);
|
||||
assert(thread_current_id == thread_id);
|
||||
}
|
||||
|
||||
test "spawn threads" {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user