const Syscall = struct thread: ?*Threadfn start() Io.Cancelable!SyscallMarks entry to a syscall region. This should be tightly scoped around the actual syscall to minimize races. The syscall must be marked as "finished" by checkCancel, finish, or one of the wrappers of finish.
fn checkCancel(s: Syscall) Io.Cancelable!voidChecks whether this syscall has been canceled. This should be called when a syscall is interrupted through a mechanism which may indicate cancelation, or may be spurious. If the syscall was canceled, it is finished and error.Canceled is returned. Otherwise, the syscall is not marked finished, and the caller should retry.
fn finish(s: Syscall) voidMarks this syscall as finished.
fn toAlertable(s: Syscall) Io.Cancelable!AlertableSyscallIndicates instead of NtCancelSynchronousIoFile we need to use NtAlertThread to interrupt the wait.
Windows only, called from blocked state only.
fn fail(s: Syscall, err: anytype) @TypeOf(err)fn errnoBug(s: Syscall, err: posix.E) Io.UnexpectedErrorConvenience wrapper which calls finish, then calls Threaded.errnoBug.
fn unexpectedErrno(s: Syscall, err: posix.E) Io.UnexpectedErrorConvenience wrapper which calls finish, then calls posix.unexpectedErrno.
fn ntstatusBug(s: Syscall, status: windows.NTSTATUS) Io.UnexpectedErrorConvenience wrapper which calls finish, then calls windows.statusBug.
fn unexpectedNtstatus(s: Syscall, status: windows.NTSTATUS) Io.UnexpectedErrorConvenience wrapper which calls finish, then calls windows.unexpectedStatus.
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462const Syscall = struct {
thread: ?*Thread,
/// Marks entry to a syscall region. This should be tightly scoped around the actual syscall
/// to minimize races. The syscall must be marked as "finished" by `checkCancel`, `finish`,
/// or one of the wrappers of `finish`.
fn start() Io.Cancelable!Syscall {
const thread = Thread.current orelse return .{ .thread = null };
switch (thread.cancel_protection) {
.blocked => return .{ .thread = null },
.unblocked => {},
}
switch (thread.status.fetchOr(.{
.cancelation = @fromBackingInt(@intCast(0b011)),
.awaitable = .null,
}, .monotonic).cancelation) {
.parked => unreachable,
.blocked => unreachable,
.blocked_alertable => unreachable,
.blocked_alertable_canceling => unreachable,
.blocked_canceling => unreachable,
.none => return .{ .thread = thread }, // new status is `.blocked`
.canceling => return error.Canceled, // new status is `.canceled`
.canceled => return .{ .thread = null }, // new status is `.canceled` (unchanged)
}
}
/// Checks whether this syscall has been canceled. This should be called when a syscall is
/// interrupted through a mechanism which may indicate cancelation, or may be spurious. If
/// the syscall was canceled, it is finished and `error.Canceled` is returned. Otherwise,
/// the syscall is not marked finished, and the caller should retry.
fn checkCancel(s: Syscall) Io.Cancelable!void {
const thread = s.thread orelse return;
switch (thread.status.fetchOr(.{
.cancelation = @fromBackingInt(@intCast(0b010)),
.awaitable = .null,
}, .monotonic).cancelation) {
.none => unreachable,
.parked => unreachable,
.blocked_alertable => unreachable,
.blocked_alertable_canceling => unreachable,
.canceling => unreachable,
.canceled => unreachable,
.blocked => {}, // new status is `.blocked` (unchanged)
.blocked_canceling => return error.Canceled, // new status is `.canceled`
}
}
/// Marks this syscall as finished.
fn finish(s: Syscall) void {
const thread = s.thread orelse return;
switch (thread.status.fetchXor(.{
.cancelation = @fromBackingInt(@intCast(0b011)),
.awaitable = .null,
}, .monotonic).cancelation) {
.none => unreachable,
.parked => unreachable,
.blocked_alertable => unreachable,
.blocked_alertable_canceling => unreachable,
.canceling => unreachable,
.canceled => unreachable,
.blocked => {}, // new status is `.none`
.blocked_canceling => {}, // new status is `.canceling`
}
}
/// Indicates instead of `NtCancelSynchronousIoFile` we need to use
/// `NtAlertThread` to interrupt the wait.
///
/// Windows only, called from blocked state only.
fn toAlertable(s: Syscall) Io.Cancelable!AlertableSyscall {
comptime assert(is_windows);
const thread = s.thread orelse return .{ .thread = null };
var prev = thread.status.load(.monotonic);
while (true) prev = switch (prev.cancelation) {
.none => unreachable,
.parked => unreachable,
.blocked_alertable => unreachable,
.blocked_alertable_canceling => unreachable,
.canceling => unreachable,
.canceled => unreachable,
.blocked => thread.status.cmpxchgWeak(prev, .{
.cancelation = .blocked_alertable,
.awaitable = prev.awaitable,
}, .monotonic, .monotonic) orelse return .{ .thread = thread },
.blocked_canceling => thread.status.cmpxchgWeak(prev, .{
.cancelation = .canceled,
.awaitable = prev.awaitable,
}, .monotonic, .monotonic) orelse return error.Canceled,
};
}
/// Convenience wrapper which calls `finish`, then returns `err`.
fn fail(s: Syscall, err: anytype) @TypeOf(err) {
s.finish();
return err;
}
/// Convenience wrapper which calls `finish`, then calls `Threaded.errnoBug`.
fn errnoBug(s: Syscall, err: posix.E) Io.UnexpectedError {
@branchHint(.cold);
s.finish();
return Threaded.errnoBug(err);
}
/// Convenience wrapper which calls `finish`, then calls `posix.unexpectedErrno`.
fn unexpectedErrno(s: Syscall, err: posix.E) Io.UnexpectedError {
@branchHint(.cold);
s.finish();
return posix.unexpectedErrno(err);
}
/// Convenience wrapper which calls `finish`, then calls `windows.statusBug`.
fn ntstatusBug(s: Syscall, status: windows.NTSTATUS) Io.UnexpectedError {
@branchHint(.cold);
s.finish();
return windows.statusBug(status);
}
/// Convenience wrapper which calls `finish`, then calls `windows.unexpectedStatus`.
fn unexpectedNtstatus(s: Syscall, status: windows.NTSTATUS) Io.UnexpectedError {
@branchHint(.cold);
s.finish();
return windows.unexpectedStatus(status);
}
}