aboutsummaryrefslogtreecommitdiff
path: root/lib/std/Thread
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-06-24 16:58:19 -0700
committerGitHub <noreply@github.com>2023-06-24 16:58:19 -0700
commit146b79af153bbd5dafda0ba12a040385c7fc58f8 (patch)
tree67e3db8b444d65c667e314770fc983a7fc8ba293 /lib/std/Thread
parent13853bef0df3c90633021850cc6d6abaeea03282 (diff)
parent21ac0beb436f49fe49c6982a872f2dc48e4bea5e (diff)
downloadzig-146b79af153bbd5dafda0ba12a040385c7fc58f8.tar.gz
zig-146b79af153bbd5dafda0ba12a040385c7fc58f8.zip
Merge pull request #16163 from mlugg/feat/builtins-infer-dest-ty
Infer destination type of cast builtins using result type
Diffstat (limited to 'lib/std/Thread')
-rw-r--r--lib/std/Thread/Futex.zig50
-rw-r--r--lib/std/Thread/Mutex.zig6
2 files changed, 28 insertions, 28 deletions
diff --git a/lib/std/Thread/Futex.zig b/lib/std/Thread/Futex.zig
index 61e39eba27..768442539b 100644
--- a/lib/std/Thread/Futex.zig
+++ b/lib/std/Thread/Futex.zig
@@ -128,14 +128,14 @@ const WindowsImpl = struct {
// NTDLL functions work with time in units of 100 nanoseconds.
// Positive values are absolute deadlines while negative values are relative durations.
if (timeout) |delay| {
- timeout_value = @intCast(os.windows.LARGE_INTEGER, delay / 100);
+ timeout_value = @as(os.windows.LARGE_INTEGER, @intCast(delay / 100));
timeout_value = -timeout_value;
timeout_ptr = &timeout_value;
}
const rc = os.windows.ntdll.RtlWaitOnAddress(
- @ptrCast(?*const anyopaque, ptr),
- @ptrCast(?*const anyopaque, &expect),
+ @as(?*const anyopaque, @ptrCast(ptr)),
+ @as(?*const anyopaque, @ptrCast(&expect)),
@sizeOf(@TypeOf(expect)),
timeout_ptr,
);
@@ -151,7 +151,7 @@ const WindowsImpl = struct {
}
fn wake(ptr: *const Atomic(u32), max_waiters: u32) void {
- const address = @ptrCast(?*const anyopaque, ptr);
+ const address = @as(?*const anyopaque, @ptrCast(ptr));
assert(max_waiters != 0);
switch (max_waiters) {
@@ -186,7 +186,7 @@ const DarwinImpl = struct {
// true so that we we know to ignore the ETIMEDOUT result.
var timeout_overflowed = false;
- const addr = @ptrCast(*const anyopaque, ptr);
+ const addr = @as(*const anyopaque, @ptrCast(ptr));
const flags = os.darwin.UL_COMPARE_AND_WAIT | os.darwin.ULF_NO_ERRNO;
const status = blk: {
if (supports_ulock_wait2) {
@@ -202,7 +202,7 @@ const DarwinImpl = struct {
};
if (status >= 0) return;
- switch (@enumFromInt(std.os.E, -status)) {
+ switch (@as(std.os.E, @enumFromInt(-status))) {
// Wait was interrupted by the OS or other spurious signalling.
.INTR => {},
// Address of the futex was paged out. This is unlikely, but possible in theory, and
@@ -225,11 +225,11 @@ const DarwinImpl = struct {
}
while (true) {
- const addr = @ptrCast(*const anyopaque, ptr);
+ const addr = @as(*const anyopaque, @ptrCast(ptr));
const status = os.darwin.__ulock_wake(flags, addr, 0);
if (status >= 0) return;
- switch (@enumFromInt(std.os.E, -status)) {
+ switch (@as(std.os.E, @enumFromInt(-status))) {
.INTR => continue, // spurious wake()
.FAULT => unreachable, // __ulock_wake doesn't generate EFAULT according to darwin pthread_cond_t
.NOENT => return, // nothing was woken up
@@ -245,14 +245,14 @@ const LinuxImpl = struct {
fn wait(ptr: *const Atomic(u32), expect: u32, timeout: ?u64) error{Timeout}!void {
var ts: os.timespec = undefined;
if (timeout) |timeout_ns| {
- ts.tv_sec = @intCast(@TypeOf(ts.tv_sec), timeout_ns / std.time.ns_per_s);
- ts.tv_nsec = @intCast(@TypeOf(ts.tv_nsec), timeout_ns % std.time.ns_per_s);
+ 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));
}
const rc = os.linux.futex_wait(
- @ptrCast(*const i32, &ptr.value),
+ @as(*const i32, @ptrCast(&ptr.value)),
os.linux.FUTEX.PRIVATE_FLAG | os.linux.FUTEX.WAIT,
- @bitCast(i32, expect),
+ @as(i32, @bitCast(expect)),
if (timeout != null) &ts else null,
);
@@ -272,7 +272,7 @@ const LinuxImpl = struct {
fn wake(ptr: *const Atomic(u32), max_waiters: u32) void {
const rc = os.linux.futex_wake(
- @ptrCast(*const i32, &ptr.value),
+ @as(*const i32, @ptrCast(&ptr.value)),
os.linux.FUTEX.PRIVATE_FLAG | os.linux.FUTEX.WAKE,
std.math.cast(i32, max_waiters) orelse std.math.maxInt(i32),
);
@@ -299,8 +299,8 @@ const FreebsdImpl = struct {
tm._flags = 0; // use relative time not UMTX_ABSTIME
tm._clockid = os.CLOCK.MONOTONIC;
- tm._timeout.tv_sec = @intCast(@TypeOf(tm._timeout.tv_sec), timeout_ns / std.time.ns_per_s);
- tm._timeout.tv_nsec = @intCast(@TypeOf(tm._timeout.tv_nsec), timeout_ns % std.time.ns_per_s);
+ 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));
}
const rc = os.freebsd._umtx_op(
@@ -347,14 +347,14 @@ const OpenbsdImpl = struct {
fn wait(ptr: *const Atomic(u32), expect: u32, timeout: ?u64) error{Timeout}!void {
var ts: os.timespec = undefined;
if (timeout) |timeout_ns| {
- ts.tv_sec = @intCast(@TypeOf(ts.tv_sec), timeout_ns / std.time.ns_per_s);
- ts.tv_nsec = @intCast(@TypeOf(ts.tv_nsec), timeout_ns % std.time.ns_per_s);
+ 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));
}
const rc = os.openbsd.futex(
- @ptrCast(*const volatile u32, &ptr.value),
+ @as(*const volatile u32, @ptrCast(&ptr.value)),
os.openbsd.FUTEX_WAIT | os.openbsd.FUTEX_PRIVATE_FLAG,
- @bitCast(c_int, expect),
+ @as(c_int, @bitCast(expect)),
if (timeout != null) &ts else null,
null, // FUTEX_WAIT takes no requeue address
);
@@ -377,7 +377,7 @@ const OpenbsdImpl = struct {
fn wake(ptr: *const Atomic(u32), max_waiters: u32) void {
const rc = os.openbsd.futex(
- @ptrCast(*const volatile u32, &ptr.value),
+ @as(*const volatile u32, @ptrCast(&ptr.value)),
os.openbsd.FUTEX_WAKE | os.openbsd.FUTEX_PRIVATE_FLAG,
std.math.cast(c_int, max_waiters) orelse std.math.maxInt(c_int),
null, // FUTEX_WAKE takes no timeout ptr
@@ -411,8 +411,8 @@ const DragonflyImpl = struct {
}
}
- const value = @bitCast(c_int, expect);
- const addr = @ptrCast(*const volatile c_int, &ptr.value);
+ const value = @as(c_int, @bitCast(expect));
+ const addr = @as(*const volatile c_int, @ptrCast(&ptr.value));
const rc = os.dragonfly.umtx_sleep(addr, value, timeout_us);
switch (os.errno(rc)) {
@@ -441,7 +441,7 @@ const DragonflyImpl = struct {
// https://man.dragonflybsd.org/?command=umtx&section=2
// > umtx_wakeup() will generally return 0 unless the address is bad.
// We are fine with the address being bad (e.g. for Semaphore.post() where Semaphore.wait() frees the Semaphore)
- const addr = @ptrCast(*const volatile c_int, &ptr.value);
+ const addr = @as(*const volatile c_int, @ptrCast(&ptr.value));
_ = os.dragonfly.umtx_wakeup(addr, to_wake);
}
};
@@ -488,8 +488,8 @@ const PosixImpl = struct {
var ts: os.timespec = undefined;
if (timeout) |timeout_ns| {
os.clock_gettime(os.CLOCK.REALTIME, &ts) catch unreachable;
- ts.tv_sec +|= @intCast(@TypeOf(ts.tv_sec), timeout_ns / std.time.ns_per_s);
- ts.tv_nsec += @intCast(@TypeOf(ts.tv_nsec), timeout_ns % std.time.ns_per_s);
+ 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));
if (ts.tv_nsec >= std.time.ns_per_s) {
ts.tv_sec +|= 1;
diff --git a/lib/std/Thread/Mutex.zig b/lib/std/Thread/Mutex.zig
index 9114caaa12..0f618516b5 100644
--- a/lib/std/Thread/Mutex.zig
+++ b/lib/std/Thread/Mutex.zig
@@ -242,12 +242,12 @@ const NonAtomicCounter = struct {
value: [2]u64 = [_]u64{ 0, 0 },
fn get(self: NonAtomicCounter) u128 {
- return @bitCast(u128, self.value);
+ return @as(u128, @bitCast(self.value));
}
fn inc(self: *NonAtomicCounter) void {
- for (@bitCast([2]u64, self.get() + 1), 0..) |v, i| {
- @ptrCast(*volatile u64, &self.value[i]).* = v;
+ for (@as([2]u64, @bitCast(self.get() + 1)), 0..) |v, i| {
+ @as(*volatile u64, @ptrCast(&self.value[i])).* = v;
}
}
};