Skip to content

Commit 0d1db79

Browse files
authored
Merge pull request #20679 from ziglang/std.c-reorg
std.c reorganization
2 parents 89e4c38 + 01337e2 commit 0d1db79

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+9775
-11584
lines changed

CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,6 @@ set(ZIG_STAGE2_SOURCES
404404
lib/std/buf_map.zig
405405
lib/std/builtin.zig
406406
lib/std/c.zig
407-
lib/std/c/linux.zig
408407
lib/std/coff.zig
409408
lib/std/crypto.zig
410409
lib/std/crypto/blake3.zig

lib/std/Progress.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,16 +1349,16 @@ fn maybeUpdateSize(resize_flag: bool) void {
13491349
}
13501350
} else {
13511351
var winsize: posix.winsize = .{
1352-
.ws_row = 0,
1353-
.ws_col = 0,
1354-
.ws_xpixel = 0,
1355-
.ws_ypixel = 0,
1352+
.row = 0,
1353+
.col = 0,
1354+
.xpixel = 0,
1355+
.ypixel = 0,
13561356
};
13571357

13581358
const err = posix.system.ioctl(fd, posix.T.IOCGWINSZ, @intFromPtr(&winsize));
13591359
if (posix.errno(err) == .SUCCESS) {
1360-
global_progress.rows = winsize.ws_row;
1361-
global_progress.cols = winsize.ws_col;
1360+
global_progress.rows = winsize.row;
1361+
global_progress.cols = winsize.col;
13621362
} else {
13631363
std.log.debug("failed to determine terminal size; using conservative guess 80x25", .{});
13641364
global_progress.rows = 25;

lib/std/Thread/Futex.zig

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,10 @@ const DarwinImpl = struct {
196196
var timeout_overflowed = false;
197197

198198
const addr: *const anyopaque = ptr;
199-
const flags = c.UL_COMPARE_AND_WAIT | c.ULF_NO_ERRNO;
199+
const flags: c.UL = .{
200+
.op = .COMPARE_AND_WAIT,
201+
.NO_ERRNO = true,
202+
};
200203
const status = blk: {
201204
if (supports_ulock_wait2) {
202205
break :blk c.__ulock_wait2(flags, addr, expect, timeout_ns, 0);
@@ -228,10 +231,11 @@ const DarwinImpl = struct {
228231
}
229232

230233
fn wake(ptr: *const atomic.Value(u32), max_waiters: u32) void {
231-
var flags: u32 = c.UL_COMPARE_AND_WAIT | c.ULF_NO_ERRNO;
232-
if (max_waiters > 1) {
233-
flags |= c.ULF_WAKE_ALL;
234-
}
234+
const flags: c.UL = .{
235+
.op = .COMPARE_AND_WAIT,
236+
.NO_ERRNO = true,
237+
.WAKE_ALL = max_waiters > 1,
238+
};
235239

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

261265
const rc = linux.futex_wait(
@@ -306,10 +310,10 @@ const FreebsdImpl = struct {
306310
tm_ptr = &tm;
307311
tm_size = @sizeOf(@TypeOf(tm));
308312

309-
tm._flags = 0; // use relative time not UMTX_ABSTIME
310-
tm._clockid = c.CLOCK.MONOTONIC;
311-
tm._timeout.tv_sec = @as(@TypeOf(tm._timeout.tv_sec), @intCast(timeout_ns / std.time.ns_per_s));
312-
tm._timeout.tv_nsec = @as(@TypeOf(tm._timeout.tv_nsec), @intCast(timeout_ns % std.time.ns_per_s));
313+
tm.flags = 0; // use relative time not UMTX_ABSTIME
314+
tm.clockid = .MONOTONIC;
315+
tm.timeout.sec = @as(@TypeOf(tm.timeout.sec), @intCast(timeout_ns / std.time.ns_per_s));
316+
tm.timeout.nsec = @as(@TypeOf(tm.timeout.nsec), @intCast(timeout_ns % std.time.ns_per_s));
313317
}
314318

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

363367
const rc = c.futex(
364368
@as(*const volatile u32, @ptrCast(&ptr.raw)),
365-
c.FUTEX_WAIT | c.FUTEX_PRIVATE_FLAG,
369+
c.FUTEX.WAIT | c.FUTEX.PRIVATE_FLAG,
366370
@as(c_int, @bitCast(expect)),
367371
if (timeout != null) &ts else null,
368-
null, // FUTEX_WAIT takes no requeue address
372+
null, // FUTEX.WAIT takes no requeue address
369373
);
370374

371375
switch (std.posix.errno(rc)) {
@@ -387,10 +391,10 @@ const OpenbsdImpl = struct {
387391
fn wake(ptr: *const atomic.Value(u32), max_waiters: u32) void {
388392
const rc = c.futex(
389393
@as(*const volatile u32, @ptrCast(&ptr.raw)),
390-
c.FUTEX_WAKE | c.FUTEX_PRIVATE_FLAG,
394+
c.FUTEX.WAKE | c.FUTEX.PRIVATE_FLAG,
391395
std.math.cast(c_int, max_waiters) orelse std.math.maxInt(c_int),
392-
null, // FUTEX_WAKE takes no timeout ptr
393-
null, // FUTEX_WAKE takes no requeue address
396+
null, // FUTEX.WAKE takes no timeout ptr
397+
null, // FUTEX.WAKE takes no requeue address
394398
);
395399

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

546-
if (ts.tv_nsec >= std.time.ns_per_s) {
547-
ts.tv_sec +|= 1;
548-
ts.tv_nsec -= std.time.ns_per_s;
550+
if (ts.nsec >= std.time.ns_per_s) {
551+
ts.sec +|= 1;
552+
ts.nsec -= std.time.ns_per_s;
549553
}
550554
}
551555

lib/std/Thread/Mutex.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ const SingleThreadedImpl = struct {
103103
}
104104
};
105105

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

@@ -123,7 +123,7 @@ const WindowsImpl = struct {
123123
const windows = std.os.windows;
124124
};
125125

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

0 commit comments

Comments
 (0)