authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2025-12-29 22:00:59-05:00
committergravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2025-12-29 22:29:48-05:00
log3c851ec3969f1396eff5c3af21be7b9e3328c7ae
tree370b9943a447bb4d4e0b44a507006934daef01b1
parent09f06082f0b43f93d1d4267539cb29c603fe0d11
signaturelock-open Commit is signed but in an unrecognized format.

dragonfly: Io.Threaded: use futex


1 files changed, 26 insertions(+), 0 deletions(-)

lib/std/Io/Threaded.zig+26
...@@ -417,6 +417,25 @@ const Thread = struct {...@@ -417,6 +417,25 @@ const Thread = struct {
417 else => unreachable,417 else => unreachable,
418 };418 };
419 },419 },
420 .dragonfly => {
421 var timeout_us: c_int = undefined;
422 if (timeout_ns) |ns| {
423 timeout_us = std.math.cast(c_int, ns / std.time.ns_per_us) orelse std.math.maxInt(c_int);
424 } else {
425 timeout_us = 0;
426 }
427 if (thread) |t| try t.beginSyscall();
428 const rc = std.c.umtx_sleep(@ptrCast(ptr), @bitCast(expect), timeout_us);
429 if (thread) |t| t.endSyscall();
430 if (is_debug) switch (std.posix.errno(rc)) {
431 .SUCCESS => {},
432 .BUSY => {}, // ptr != expect
433 .AGAIN => {}, // maybe timed out, or paged out, or hit 2s kernel refresh
434 .INTR => {}, // spurious wake
435 .INVAL => unreachable, // invalid timeout
436 else => unreachable,
437 };
438 },
420 else => if (std.Thread.use_pthreads) {439 else => if (std.Thread.use_pthreads) {
421 // TODO integrate the following function being called with robust cancelation.440 // TODO integrate the following function being called with robust cancelation.
422 return pthreads_futex.wait(ptr, expect, timeout_ns) catch |err| switch (err) {441 return pthreads_futex.wait(ptr, expect, timeout_ns) catch |err| switch (err) {
...@@ -510,6 +529,13 @@ const Thread = struct {...@@ -510,6 +529,13 @@ const Thread = struct {
510 );529 );
511 assert(rc >= 0);530 assert(rc >= 0);
512 },531 },
532 .dragonfly => {
533 // will generally return 0 unless the address is bad
534 _ = std.c.umtx_wakeup(
535 @ptrCast(ptr),
536 @min(max_waiters, std.math.maxInt(c_int)),
537 );
538 },
513 else => if (std.Thread.use_pthreads) {539 else => if (std.Thread.use_pthreads) {
514 return pthreads_futex.wake(ptr, max_waiters);540 return pthreads_futex.wake(ptr, max_waiters);
515 } else {541 } else {