Skip to content

std.c reorganization #20679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ set(ZIG_STAGE2_SOURCES
lib/std/buf_map.zig
lib/std/builtin.zig
lib/std/c.zig
lib/std/c/linux.zig
lib/std/coff.zig
lib/std/crypto.zig
lib/std/crypto/blake3.zig
Expand Down
12 changes: 6 additions & 6 deletions lib/std/Progress.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1349,16 +1349,16 @@ fn maybeUpdateSize(resize_flag: bool) void {
}
} else {
var winsize: posix.winsize = .{
.ws_row = 0,
.ws_col = 0,
.ws_xpixel = 0,
.ws_ypixel = 0,
.row = 0,
.col = 0,
.xpixel = 0,
.ypixel = 0,
};

const err = posix.system.ioctl(fd, posix.T.IOCGWINSZ, @intFromPtr(&winsize));
if (posix.errno(err) == .SUCCESS) {
global_progress.rows = winsize.ws_row;
global_progress.cols = winsize.ws_col;
global_progress.rows = winsize.row;
global_progress.cols = winsize.col;
} else {
std.log.debug("failed to determine terminal size; using conservative guess 80x25", .{});
global_progress.rows = 25;
Expand Down
52 changes: 28 additions & 24 deletions lib/std/Thread/Futex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ const DarwinImpl = struct {
var timeout_overflowed = false;

const addr: *const anyopaque = ptr;
const flags = c.UL_COMPARE_AND_WAIT | c.ULF_NO_ERRNO;
const flags: c.UL = .{
.op = .COMPARE_AND_WAIT,
.NO_ERRNO = true,
};
const status = blk: {
if (supports_ulock_wait2) {
break :blk c.__ulock_wait2(flags, addr, expect, timeout_ns, 0);
Expand Down Expand Up @@ -228,10 +231,11 @@ const DarwinImpl = struct {
}

fn wake(ptr: *const atomic.Value(u32), max_waiters: u32) void {
var flags: u32 = c.UL_COMPARE_AND_WAIT | c.ULF_NO_ERRNO;
if (max_waiters > 1) {
flags |= c.ULF_WAKE_ALL;
}
const flags: c.UL = .{
.op = .COMPARE_AND_WAIT,
.NO_ERRNO = true,
.WAKE_ALL = max_waiters > 1,
};

while (true) {
const addr: *const anyopaque = ptr;
Expand All @@ -242,7 +246,7 @@ const DarwinImpl = struct {
.INTR => continue, // spurious wake()
.FAULT => unreachable, // __ulock_wake doesn't generate EFAULT according to darwin pthread_cond_t
.NOENT => return, // nothing was woken up
.ALREADY => unreachable, // only for ULF_WAKE_THREAD
.ALREADY => unreachable, // only for UL.Op.WAKE_THREAD
else => unreachable,
}
}
Expand All @@ -254,8 +258,8 @@ const LinuxImpl = struct {
fn wait(ptr: *const atomic.Value(u32), expect: u32, timeout: ?u64) error{Timeout}!void {
var ts: linux.timespec = undefined;
if (timeout) |timeout_ns| {
ts.tv_sec = @as(@TypeOf(ts.tv_sec), @intCast(timeout_ns / std.time.ns_per_s));
ts.tv_nsec = @as(@TypeOf(ts.tv_nsec), @intCast(timeout_ns % std.time.ns_per_s));
ts.sec = @as(@TypeOf(ts.sec), @intCast(timeout_ns / std.time.ns_per_s));
ts.nsec = @as(@TypeOf(ts.nsec), @intCast(timeout_ns % std.time.ns_per_s));
}

const rc = linux.futex_wait(
Expand Down Expand Up @@ -306,10 +310,10 @@ const FreebsdImpl = struct {
tm_ptr = &tm;
tm_size = @sizeOf(@TypeOf(tm));

tm._flags = 0; // use relative time not UMTX_ABSTIME
tm._clockid = c.CLOCK.MONOTONIC;
tm._timeout.tv_sec = @as(@TypeOf(tm._timeout.tv_sec), @intCast(timeout_ns / std.time.ns_per_s));
tm._timeout.tv_nsec = @as(@TypeOf(tm._timeout.tv_nsec), @intCast(timeout_ns % std.time.ns_per_s));
tm.flags = 0; // use relative time not UMTX_ABSTIME
tm.clockid = .MONOTONIC;
tm.timeout.sec = @as(@TypeOf(tm.timeout.sec), @intCast(timeout_ns / std.time.ns_per_s));
tm.timeout.nsec = @as(@TypeOf(tm.timeout.nsec), @intCast(timeout_ns % std.time.ns_per_s));
}

const rc = c._umtx_op(
Expand Down Expand Up @@ -356,16 +360,16 @@ const OpenbsdImpl = struct {
fn wait(ptr: *const atomic.Value(u32), expect: u32, timeout: ?u64) error{Timeout}!void {
var ts: c.timespec = undefined;
if (timeout) |timeout_ns| {
ts.tv_sec = @as(@TypeOf(ts.tv_sec), @intCast(timeout_ns / std.time.ns_per_s));
ts.tv_nsec = @as(@TypeOf(ts.tv_nsec), @intCast(timeout_ns % std.time.ns_per_s));
ts.sec = @as(@TypeOf(ts.sec), @intCast(timeout_ns / std.time.ns_per_s));
ts.nsec = @as(@TypeOf(ts.nsec), @intCast(timeout_ns % std.time.ns_per_s));
}

const rc = c.futex(
@as(*const volatile u32, @ptrCast(&ptr.raw)),
c.FUTEX_WAIT | c.FUTEX_PRIVATE_FLAG,
c.FUTEX.WAIT | c.FUTEX.PRIVATE_FLAG,
@as(c_int, @bitCast(expect)),
if (timeout != null) &ts else null,
null, // FUTEX_WAIT takes no requeue address
null, // FUTEX.WAIT takes no requeue address
);

switch (std.posix.errno(rc)) {
Expand All @@ -387,10 +391,10 @@ const OpenbsdImpl = struct {
fn wake(ptr: *const atomic.Value(u32), max_waiters: u32) void {
const rc = c.futex(
@as(*const volatile u32, @ptrCast(&ptr.raw)),
c.FUTEX_WAKE | c.FUTEX_PRIVATE_FLAG,
c.FUTEX.WAKE | c.FUTEX.PRIVATE_FLAG,
std.math.cast(c_int, max_waiters) orelse std.math.maxInt(c_int),
null, // FUTEX_WAKE takes no timeout ptr
null, // FUTEX_WAKE takes no requeue address
null, // FUTEX.WAKE takes no timeout ptr
null, // FUTEX.WAKE takes no requeue address
);

// returns number of threads woken up.
Expand Down Expand Up @@ -540,12 +544,12 @@ const PosixImpl = struct {
var ts: c.timespec = undefined;
if (timeout) |timeout_ns| {
std.posix.clock_gettime(c.CLOCK.REALTIME, &ts) catch unreachable;
ts.tv_sec +|= @as(@TypeOf(ts.tv_sec), @intCast(timeout_ns / std.time.ns_per_s));
ts.tv_nsec += @as(@TypeOf(ts.tv_nsec), @intCast(timeout_ns % std.time.ns_per_s));
ts.sec +|= @as(@TypeOf(ts.sec), @intCast(timeout_ns / std.time.ns_per_s));
ts.nsec += @as(@TypeOf(ts.nsec), @intCast(timeout_ns % std.time.ns_per_s));

if (ts.tv_nsec >= std.time.ns_per_s) {
ts.tv_sec +|= 1;
ts.tv_nsec -= std.time.ns_per_s;
if (ts.nsec >= std.time.ns_per_s) {
ts.sec +|= 1;
ts.nsec -= std.time.ns_per_s;
}
}

Expand Down
6 changes: 3 additions & 3 deletions lib/std/Thread/Mutex.zig
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ const SingleThreadedImpl = struct {
}
};

// SRWLOCK on windows is almost always faster than Futex solution.
// It also implements an efficient Condition with requeue support for us.
/// SRWLOCK on windows is almost always faster than Futex solution.
/// It also implements an efficient Condition with requeue support for us.
const WindowsImpl = struct {
srwlock: windows.SRWLOCK = .{},

Expand All @@ -123,7 +123,7 @@ const WindowsImpl = struct {
const windows = std.os.windows;
};

// os_unfair_lock on darwin supports priority inheritance and is generally faster than Futex solutions.
/// os_unfair_lock on darwin supports priority inheritance and is generally faster than Futex solutions.
const DarwinImpl = struct {
oul: c.os_unfair_lock = .{},

Expand Down
Loading