diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-05-11 12:01:52 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-05-11 12:01:52 -0400 |
| commit | 2ef2f9d71f851823966ae16b45a189657051b8df (patch) | |
| tree | 9d6f1c17e04869fe105023dbfa09c5af7c518273 | |
| parent | 3d93c89fc55a428171bbfa779fb604801d84421d (diff) | |
| parent | 1b23348f30243ed61a61f1892bb391d8afab7f10 (diff) | |
| download | zig-2ef2f9d71f851823966ae16b45a189657051b8df.tar.gz zig-2ef2f9d71f851823966ae16b45a189657051b8df.zip | |
Merge pull request #2475 from LemonBoy/linux-wo-vdso
Fix clock_gettime on systems without VDSO
| -rw-r--r-- | std/os/linux.zig | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/std/os/linux.zig b/std/os/linux.zig index 99a29db582..1e121b81c8 100644 --- a/std/os/linux.zig +++ b/std/os/linux.zig @@ -959,10 +959,13 @@ pub fn waitpid(pid: i32, status: *i32, options: i32) usize { return syscall4(SYS_wait4, @bitCast(usize, isize(pid)), @ptrToInt(status), @bitCast(usize, isize(options)), 0); } +var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime); + pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { if (VDSO_CGT_SYM.len != 0) { - const f = @atomicLoad(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, builtin.AtomicOrder.Unordered); - if (@ptrToInt(f) != 0) { + const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered); + if (ptr) |fn_ptr| { + const f = @ptrCast(@typeOf(clock_gettime), fn_ptr); const rc = f(clk_id, tp); switch (rc) { 0, @bitCast(usize, isize(-EINVAL)) => return rc, @@ -972,13 +975,18 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) usize { } return syscall2(SYS_clock_gettime, @bitCast(usize, isize(clk_id)), @ptrToInt(tp)); } -var vdso_clock_gettime = init_vdso_clock_gettime; + extern fn init_vdso_clock_gettime(clk: i32, ts: *timespec) usize { - const addr = vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM); - var f = @intToPtr(@typeOf(init_vdso_clock_gettime), addr); - _ = @cmpxchgStrong(@typeOf(init_vdso_clock_gettime), &vdso_clock_gettime, init_vdso_clock_gettime, f, builtin.AtomicOrder.Monotonic, builtin.AtomicOrder.Monotonic); - if (@ptrToInt(f) == 0) return @bitCast(usize, isize(-ENOSYS)); - return f(clk, ts); + const ptr = @intToPtr(?*const c_void, vdso.lookup(VDSO_CGT_VER, VDSO_CGT_SYM)); + // Note that we may not have a VDSO at all, update the stub address anyway + // so that clock_gettime will fall back on the good old (and slow) syscall + _ = @cmpxchgStrong(?*const c_void, &vdso_clock_gettime, &init_vdso_clock_gettime, ptr, .Monotonic, .Monotonic); + // Call into the VDSO if available + if (ptr) |fn_ptr| { + const f = @ptrCast(@typeOf(clock_gettime), fn_ptr); + return f(clk, ts); + } + return @bitCast(usize, isize(-ENOSYS)); } pub fn clock_getres(clk_id: i32, tp: *timespec) usize { @@ -1104,8 +1112,8 @@ pub fn sigaction(sig: u6, noalias act: *const Sigaction, noalias oact: ?*Sigacti const NSIG = 65; const sigset_t = [128 / @sizeOf(usize)]usize; -const all_mask = []u32{0xffffffff, 0xffffffff}; -const app_mask = []u32{0xfffffffc, 0x7fffffff}; +const all_mask = []u32{ 0xffffffff, 0xffffffff }; +const app_mask = []u32{ 0xfffffffc, 0x7fffffff }; const k_sigaction = extern struct { handler: extern fn (i32) void, @@ -1403,9 +1411,15 @@ pub const epoll_data = extern union { // On x86_64 the structure is packed so that it matches the definition of its // 32bit counterpart pub const epoll_event = if (builtin.arch != .x86_64) - extern struct { events: u32, data: epoll_data } - else - packed struct { events: u32, data: epoll_data }; + extern struct { + events: u32, + data: epoll_data, + } +else + packed struct { + events: u32, + data: epoll_data, + }; pub fn epoll_create() usize { return epoll_create1(0); |
