Fields
gpa: AllocatorMust be a thread-safe allocator.
mutex: Io.Mutex
main_fiber_buffer: [@sizeOf(Fiber) + Fiber.max_result_size]u8 align(@alignOf(Fiber))
threads: Thread.List
KqueuebuiltinstdIoDirFilenetassertAllocatorAlignmentIpAddresserrnoBugcloseFdposixposixSocketModeProtocolidle_stack_sizemax_idle_searchmax_steal_ready_searchmax_iovecs_lenchanges_buffer_lenThreadFiberrecycleInitOptionsInitErrorinitdeinitCreateFileDescriptorErrorcreateFileDescriptorfindReadyFiberyieldschedulemainIdlethreadEntryCompletionidleSwitchMessagecontextSwitchmainIdleEntryfiberEntryAsyncClosureioasyncconcurrentawaitcancelcancelRequestedgroupAsyncgroupConcurrentgroupAwaitgroupCanceldirCreateDirdirCreateDirPathdirCreateDirPathOpendirStatdirStatFiledirAccessdirCreateFiledirOpenFiledirOpenDirdirClosefileStatfileClosefileWriteStreamingfileWritePositionalfileReadStreamingfileReadPositionalfileSeekByfileSeekTonowsleepnetListenIpnetAcceptnetBindIpnetConnectIpposixConnectnetListenUnixnetConnectUnixnetSendnetSendOnenetReceivenetReadnetShutdownnetInterfaceNameResolvenetInterfaceNamenetLookupopenSocketPosixposixBindposixGetSockNamesetSocketOptioncheckCancelKEventErrorkevent
std.Io.Kqueue.Alignment
alias
Alias of std.mem.Alignment
std.Io.Kqueue.Allocator
alias
Alias of std.mem.Allocator
std.Io.Kqueue.assert
alias
Alias of std.debug.assert
std.Io.Kqueue.async
function
fn async( userdata: ?*anyopaque, result: []u8, result_alignment: std.mem.Alignment, context: []const u8, context_alignment: std.mem.Alignment, start: *const fn (context: *const anyopaque, result: *anyopaque) void, ) ?*Io.AnyFuture
Parameters
std.Io.Kqueue.AsyncClosure
container
Fields
std.Io.Kqueue.AsyncClosure.call
function
fn call(closure: *AsyncClosure, message: *const SwitchMessage) callconv(.withStackAlign(.c, @alignOf(AsyncClosure))) noreturn
Parameters
closure: *AsyncClosure
message: *const SwitchMessage
std.Io.Kqueue.AsyncClosure.contextPointer
function
fn contextPointer(closure: *AsyncClosure) [*]align(Fiber.max_context_align.toByteUnits()) u8
Parameters
closure: *AsyncClosure
std.Io.Kqueue.AsyncClosure.fromFiber
function
fn fromFiber(fiber: *Fiber) *AsyncClosure
Parameters
fiber: *Fiber
std.Io.Kqueue.await
function
fn await( userdata: ?*anyopaque, any_future: *Io.AnyFuture, result: []u8, result_alignment: std.mem.Alignment, ) void
Parameters
std.Io.Kqueue.builtin
constant
std.Io.Kqueue.cancel
function
fn cancel( userdata: ?*anyopaque, any_future: *Io.AnyFuture, result: []u8, result_alignment: std.mem.Alignment, ) void
Parameters
std.Io.Kqueue.cancelRequested
function
fn cancelRequested(userdata: ?*anyopaque) bool
Parameters
userdata: ?*anyopaque
std.Io.Kqueue.changes_buffer_len
constant
std.Io.Kqueue.checkCancel
function
fn checkCancel(k: *Kqueue) error{Canceled}!void
Parameters
k: *Kqueue
Errors
- Canceled
std.Io.Kqueue.closeFd
alias
Alias of std.Io.Threaded.closeFd
std.Io.Kqueue.Completion
container
Fields
flags: u16Corresponds to Kevent field.
fflags: u32Corresponds to Kevent field.
data: isizeCorresponds to Kevent field.
std.Io.Kqueue.Completion.UserData
container
Fields
unused
wakeup
cleanup
exit
_*Fiber
std.Io.Kqueue.concurrent
function
fn concurrent( userdata: ?*anyopaque, result_len: usize, result_alignment: Alignment, context: []const u8, context_alignment: Alignment, start: *const fn (context: *const anyopaque, result: *anyopaque) void, ) Io.ConcurrentError!*Io.AnyFuture
Parameters
userdata: ?*anyopaque
result_len: usize
result_alignment: Alignment
context: []const u8
context_alignment: Alignment
start: *const fn (context: *const anyopaque, result: *anyopaque) void
Errors
- ConcurrencyUnavailable
May occur due to a temporary condition such as resource exhaustion, or to the Io implementation not supporting concurrency.
std.Io.Kqueue.contextSwitch
function
inline fn contextSwitch(message: *const SwitchMessage) *const SwitchMessage
Parameters
message: *const SwitchMessage
std.Io.Kqueue.createFileDescriptor
function
pub fn createFileDescriptor() CreateFileDescriptorError!posix.fd_t
Errors
- ProcessFdQuotaExceeded
The per-process limit on the number of open file descriptors has been reached.
- SystemFdQuotaExceeded
The system-wide limit on the total number of open files has been reached.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.CreateFileDescriptorError
error-set
Errors
- ProcessFdQuotaExceeded
The per-process limit on the number of open file descriptors has been reached.
- SystemFdQuotaExceeded
The system-wide limit on the total number of open files has been reached.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.deinit
function
pub fn deinit(k: *Kqueue) void
Parameters
k: *Kqueue
std.Io.Kqueue.Dir
alias
Alias of std.Io.Dir
std.Io.Kqueue.dirAccess
function
fn dirAccess(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, options: Dir.AccessOptions) Dir.AccessError!void
Parameters
Errors
- AccessDenied
The requested
AccessOptionswould be denied to the file, or search permission is denied for one of the directories in the path prefix.- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- FileBusy
- FileNotFound
- InputOutput
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- PermissionDenied
Write permission was requested but the file is immutable.
- ReadOnlyFileSystem
- SymLinkLoop
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.dirClose
function
fn dirClose(userdata: ?*anyopaque, dirs: []const Dir) void
Parameters
userdata: ?*anyopaque
dirs: []const Dir
std.Io.Kqueue.dirCreateDir
function
fn dirCreateDir(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, permissions: Dir.Permissions) Dir.CreateDirError!void
Parameters
Errors
- AccessDenied
In WASI, this error may occur when the file descriptor does not hold the required rights to create a new directory relative to it.
- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DiskQuota
- FileNotFound
- LinkQuotaExceeded
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- NetworkNotFound
On Windows,
\\serveror\\server\sharewas not found.- NoDevice
- NoSpaceLeft
- NotDir
- PathAlreadyExists
- PermissionDenied
- ReadOnlyFileSystem
- SymLinkLoop
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.dirCreateDirPath
function
fn dirCreateDirPath( userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, permissions: Dir.Permissions, ) Dir.CreateDirPathError!Dir.CreatePathStatus
Parameters
Errors
- AccessDenied CreateDirError
In WASI, this error may occur when the file descriptor does not hold the required rights to create a new directory relative to it.
- AntivirusInterference OpenError
On Windows, antivirus software is enabled by default. It can be disabled, but Windows Update sometimes ignores the user's preference and re-enables it. When enabled, antivirus software on Windows intercepts file system operations and makes them significantly slower in addition to possibly failing with this error code.
- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DeviceBusy OpenError
- DiskQuota CreateDirError
- FileBusy OpenError
One of these three things:
- pathname refers to an executable image which is currently being executed and write access was requested.
- pathname refers to a file that is currently in use as a swap file, and the O_TRUNC flag was specified.
- pathname refers to a file that is currently being read by the kernel (e.g., for module/firmware loading), and write access was requested.
- FileLocksUnsupported OpenError
- FileNotFound OpenError
Either:
- One of the path components does not exist.
- Cwd was used, but cwd has been deleted.
- The path associated with the open directory handle has been deleted.
- On macOS, multiple processes or threads raced to create the same file
with
O.EXCLset tofalse.
- FileTooBig OpenError
The file is too large to be opened. This error is unreachable for 64-bit targets, as well as when opening directories.
- IsDir OpenError
Either:
- The path refers to a directory and write permissions were requested.
- The path refers to a directory and
allow_directorywas set to false.
- LinkQuotaExceeded CreateDirError
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- NetworkNotFound CreateDirError
On Windows,
\\serveror\\server\sharewas not found.- NoDevice CreateDirError
- NoSpaceLeft OpenError
A new path cannot be created because the device has no room for the new file. This error is only reachable when the
CREATflag is provided.- NotDir OpenError
A component used as a directory in the path was not, in fact, a directory, or
DIRECTORYwas specified and the path was not a directory.- PathAlreadyExists OpenError
The path already exists and the
CREATandEXCLflags were provided.- PermissionDenied CreateDirError
- PipeBusy OpenError
- ProcessFdQuotaExceeded OpenError
- ReadOnlyFileSystem CreateDirError
- Streaming StatError
Attempted to stat a non-file stream.
- SymLinkLoop CreateDirError
- SystemFdQuotaExceeded OpenError
- SystemResources OpenError
The path exceeded
max_path_bytesbytes. Insufficient kernel memory was available, or the named file is a FIFO and per-user hard limit on memory allocation for pipes has been reached.- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock OpenError
Non-blocking was requested and the operation cannot return immediately.
std.Io.Kqueue.dirCreateDirPathOpen
function
fn dirCreateDirPathOpen( userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, permissions: Dir.Permissions, options: Dir.OpenOptions, ) Dir.CreateDirPathOpenError!Dir
Parameters
userdata: ?*anyopaque
dir: Dir
sub_path: []const u8
permissions: Dir.Permissions
options: Dir.OpenOptions
Errors
- AccessDenied CreateDirError
In WASI, this error may occur when the file descriptor does not hold the required rights to create a new directory relative to it.
- AntivirusInterference OpenError
On Windows, antivirus software is enabled by default. It can be disabled, but Windows Update sometimes ignores the user's preference and re-enables it. When enabled, antivirus software on Windows intercepts file system operations and makes them significantly slower in addition to possibly failing with this error code.
- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DeviceBusy OpenError
- DiskQuota CreateDirError
- FileBusy OpenError
One of these three things:
- pathname refers to an executable image which is currently being executed and write access was requested.
- pathname refers to a file that is currently in use as a swap file, and the O_TRUNC flag was specified.
- pathname refers to a file that is currently being read by the kernel (e.g., for module/firmware loading), and write access was requested.
- FileLocksUnsupported OpenError
- FileNotFound OpenError
Either:
- One of the path components does not exist.
- Cwd was used, but cwd has been deleted.
- The path associated with the open directory handle has been deleted.
- On macOS, multiple processes or threads raced to create the same file
with
O.EXCLset tofalse.
- FileTooBig OpenError
The file is too large to be opened. This error is unreachable for 64-bit targets, as well as when opening directories.
- IsDir OpenError
Either:
- The path refers to a directory and write permissions were requested.
- The path refers to a directory and
allow_directorywas set to false.
- LinkQuotaExceeded CreateDirError
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- NetworkNotFound CreateDirError
On Windows,
\\serveror\\server\sharewas not found.- NoDevice CreateDirError
- NoSpaceLeft OpenError
A new path cannot be created because the device has no room for the new file. This error is only reachable when the
CREATflag is provided.- NotDir OpenError
A component used as a directory in the path was not, in fact, a directory, or
DIRECTORYwas specified and the path was not a directory.- PathAlreadyExists OpenError
The path already exists and the
CREATandEXCLflags were provided.- PermissionDenied CreateDirError
- PipeBusy OpenError
- ProcessFdQuotaExceeded OpenError
- ReadOnlyFileSystem CreateDirError
- Streaming StatError
Attempted to stat a non-file stream.
- SymLinkLoop CreateDirError
- SystemFdQuotaExceeded OpenError
- SystemResources OpenError
The path exceeded
max_path_bytesbytes. Insufficient kernel memory was available, or the named file is a FIFO and per-user hard limit on memory allocation for pipes has been reached.- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock OpenError
Non-blocking was requested and the operation cannot return immediately.
std.Io.Kqueue.dirCreateFile
function
fn dirCreateFile(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, flags: Dir.CreateFileOptions) File.OpenError!File
Parameters
Errors
- AccessDenied
In WASI, this error may occur when the file descriptor does not hold the required rights to open a new resource relative to it.
- AntivirusInterference
On Windows, antivirus software is enabled by default. It can be disabled, but Windows Update sometimes ignores the user's preference and re-enables it. When enabled, antivirus software on Windows intercepts file system operations and makes them significantly slower in addition to possibly failing with this error code.
- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DeviceBusy
- FileBusy
One of these three things:
- pathname refers to an executable image which is currently being executed and write access was requested.
- pathname refers to a file that is currently in use as a swap file, and the O_TRUNC flag was specified.
- pathname refers to a file that is currently being read by the kernel (e.g., for module/firmware loading), and write access was requested.
- FileLocksUnsupported
- FileNotFound
Either:
- One of the path components does not exist.
- Cwd was used, but cwd has been deleted.
- The path associated with the open directory handle has been deleted.
- On macOS, multiple processes or threads raced to create the same file
with
O.EXCLset tofalse.
- FileTooBig
The file is too large to be opened. This error is unreachable for 64-bit targets, as well as when opening directories.
- IsDir
Either:
- The path refers to a directory and write permissions were requested.
- The path refers to a directory and
allow_directorywas set to false.
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- NetworkNotFound
On Windows,
\\serveror\\server\sharewas not found.- NoDevice
- NoSpaceLeft
A new path cannot be created because the device has no room for the new file. This error is only reachable when the
CREATflag is provided.- NotDir
A component used as a directory in the path was not, in fact, a directory, or
DIRECTORYwas specified and the path was not a directory.- PathAlreadyExists
The path already exists and the
CREATandEXCLflags were provided.- PermissionDenied
- PipeBusy
- ProcessFdQuotaExceeded
- ReadOnlyFileSystem
- SymLinkLoop
- SystemFdQuotaExceeded
- SystemResources
The path exceeded
max_path_bytesbytes. Insufficient kernel memory was available, or the named file is a FIFO and per-user hard limit on memory allocation for pipes has been reached.- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock
Non-blocking was requested and the operation cannot return immediately.
std.Io.Kqueue.dirOpenDir
function
fn dirOpenDir(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, options: Dir.OpenOptions) Dir.OpenError!Dir
Parameters
Errors
- AccessDenied
- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- FileNotFound
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- NetworkNotFound
On Windows,
\\serveror\\server\sharewas not found.- NoDevice
- NotDir
- PermissionDenied
- ProcessFdQuotaExceeded
- SymLinkLoop
- SystemFdQuotaExceeded
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.dirOpenFile
function
fn dirOpenFile(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, flags: Dir.OpenFileOptions) File.OpenError!File
Parameters
Errors
- AccessDenied
In WASI, this error may occur when the file descriptor does not hold the required rights to open a new resource relative to it.
- AntivirusInterference
On Windows, antivirus software is enabled by default. It can be disabled, but Windows Update sometimes ignores the user's preference and re-enables it. When enabled, antivirus software on Windows intercepts file system operations and makes them significantly slower in addition to possibly failing with this error code.
- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DeviceBusy
- FileBusy
One of these three things:
- pathname refers to an executable image which is currently being executed and write access was requested.
- pathname refers to a file that is currently in use as a swap file, and the O_TRUNC flag was specified.
- pathname refers to a file that is currently being read by the kernel (e.g., for module/firmware loading), and write access was requested.
- FileLocksUnsupported
- FileNotFound
Either:
- One of the path components does not exist.
- Cwd was used, but cwd has been deleted.
- The path associated with the open directory handle has been deleted.
- On macOS, multiple processes or threads raced to create the same file
with
O.EXCLset tofalse.
- FileTooBig
The file is too large to be opened. This error is unreachable for 64-bit targets, as well as when opening directories.
- IsDir
Either:
- The path refers to a directory and write permissions were requested.
- The path refers to a directory and
allow_directorywas set to false.
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- NetworkNotFound
On Windows,
\\serveror\\server\sharewas not found.- NoDevice
- NoSpaceLeft
A new path cannot be created because the device has no room for the new file. This error is only reachable when the
CREATflag is provided.- NotDir
A component used as a directory in the path was not, in fact, a directory, or
DIRECTORYwas specified and the path was not a directory.- PathAlreadyExists
The path already exists and the
CREATandEXCLflags were provided.- PermissionDenied
- PipeBusy
- ProcessFdQuotaExceeded
- ReadOnlyFileSystem
- SymLinkLoop
- SystemFdQuotaExceeded
- SystemResources
The path exceeded
max_path_bytesbytes. Insufficient kernel memory was available, or the named file is a FIFO and per-user hard limit on memory allocation for pipes has been reached.- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock
Non-blocking was requested and the operation cannot return immediately.
std.Io.Kqueue.dirStat
function
fn dirStat(userdata: ?*anyopaque, dir: Dir) Dir.StatError!Dir.Stat
Parameters
userdata: ?*anyopaque
dir: Dir
Errors
- AccessDenied StatError
In WASI, this error may occur when the file descriptor does not hold the required rights to get its filestat information.
- Canceled Cancelable
Caller has requested the async operation to stop.
- PermissionDenied StatError
- Streaming StatError
Attempted to stat a non-file stream.
- SystemResources StatError
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.dirStatFile
function
fn dirStatFile( userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, options: Dir.StatFileOptions, ) Dir.StatFileError!File.Stat
Parameters
Errors
- AccessDenied OpenError
In WASI, this error may occur when the file descriptor does not hold the required rights to open a new resource relative to it.
- AntivirusInterference OpenError
On Windows, antivirus software is enabled by default. It can be disabled, but Windows Update sometimes ignores the user's preference and re-enables it. When enabled, antivirus software on Windows intercepts file system operations and makes them significantly slower in addition to possibly failing with this error code.
- BadPathName PathNameError
File system cannot encode the requested file name bytes. Could be due to invalid WTF-8 on Windows, invalid UTF-8 on WASI, invalid characters on Windows, etc. Filesystem and operating specific.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DeviceBusy OpenError
- FileBusy OpenError
One of these three things:
- pathname refers to an executable image which is currently being executed and write access was requested.
- pathname refers to a file that is currently in use as a swap file, and the O_TRUNC flag was specified.
- pathname refers to a file that is currently being read by the kernel (e.g., for module/firmware loading), and write access was requested.
- FileLocksUnsupported OpenError
- FileNotFound OpenError
Either:
- One of the path components does not exist.
- Cwd was used, but cwd has been deleted.
- The path associated with the open directory handle has been deleted.
- On macOS, multiple processes or threads raced to create the same file
with
O.EXCLset tofalse.
- FileTooBig OpenError
The file is too large to be opened. This error is unreachable for 64-bit targets, as well as when opening directories.
- IsDir OpenError
Either:
- The path refers to a directory and write permissions were requested.
- The path refers to a directory and
allow_directorywas set to false.
- NameTooLong PathNameError
Returned when an insufficient buffer is provided that cannot fit the path name.
- NetworkNotFound OpenError
On Windows,
\\serveror\\server\sharewas not found.- NoDevice OpenError
- NoSpaceLeft OpenError
A new path cannot be created because the device has no room for the new file. This error is only reachable when the
CREATflag is provided.- NotDir OpenError
A component used as a directory in the path was not, in fact, a directory, or
DIRECTORYwas specified and the path was not a directory.- PathAlreadyExists OpenError
The path already exists and the
CREATandEXCLflags were provided.- PermissionDenied OpenError
- PipeBusy OpenError
- ProcessFdQuotaExceeded OpenError
- ReadOnlyFileSystem OpenError
- Streaming StatError
Attempted to stat a non-file stream.
- SymLinkLoop OpenError
- SystemFdQuotaExceeded OpenError
- SystemResources OpenError
The path exceeded
max_path_bytesbytes. Insufficient kernel memory was available, or the named file is a FIFO and per-user hard limit on memory allocation for pipes has been reached.- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock OpenError
Non-blocking was requested and the operation cannot return immediately.
std.Io.Kqueue.errnoBug
alias
Alias of std.Io.Threaded.errnoBug
std.Io.Kqueue.Fiber
container
Fields
std.Io.Kqueue.Fiber.allocate
function
fn allocate(k: *Kqueue) error{OutOfMemory}!*Fiber
Parameters
k: *Kqueue
Errors
- OutOfMemory
std.Io.Kqueue.Fiber.allocatedEnd
function
fn allocatedEnd(f: *Fiber) [*]u8
Parameters
f: *Fiber
std.Io.Kqueue.Fiber.allocatedSlice
function
fn allocatedSlice(f: *Fiber) []align(@alignOf(Fiber)) u8
Parameters
f: *Fiber
std.Io.Kqueue.Fiber.allocation_size
constant
std.Io.Kqueue.Fiber.enterCancelRegion
function
fn enterCancelRegion(fiber: *Fiber, thread: *Thread) error{Canceled}!void
Parameters
Errors
- Canceled
std.Io.Kqueue.Fiber.exitCancelRegion
function
fn exitCancelRegion(fiber: *Fiber, thread: *Thread) void
Parameters
std.Io.Kqueue.Fiber.finished
constant
?*Fiber
std.Io.Kqueue.Fiber.max_closure_align
constant
Alignment
std.Io.Kqueue.Fiber.max_closure_size
constant
usize
std.Io.Kqueue.Fiber.max_context_align
constant
Alignment
std.Io.Kqueue.Fiber.max_context_size
constant
std.Io.Kqueue.Fiber.max_result_align
constant
Alignment
std.Io.Kqueue.Fiber.max_result_size
constant
std.Io.Kqueue.Fiber.min_stack_size
constant
This includes any stack realignments that need to happen, and also the initial frame return address slot and argument frame, depending on target.
std.Io.Kqueue.Fiber.Queue
container
Fields
std.Io.Kqueue.Fiber.resultBytes
function
fn resultBytes(f: *Fiber, alignment: Alignment) [*]u8
Parameters
std.Io.Kqueue.Fiber.resultPointer
function
fn resultPointer(f: *Fiber, comptime Result: type) *Result
Parameters
f: *Fiber
Result: type
std.Io.Kqueue.fiberEntry
function
fn fiberEntry() callconv(.naked) void
std.Io.Kqueue.File
alias
Alias of std.Io.File
std.Io.Kqueue.fileClose
function
fn fileClose(userdata: ?*anyopaque, files: []const File) void
Parameters
userdata: ?*anyopaque
files: []const File
std.Io.Kqueue.fileReadPositional
function
fn fileReadPositional(userdata: ?*anyopaque, file: File, data: []const []u8, offset: u64) File.ReadPositionalError!usize
Parameters
Errors
- AccessDenied
In WASI, this error occurs when the file descriptor does not hold the required rights to read from it.
- Canceled Cancelable
Caller has requested the async operation to stop.
- InputOutput
- IsDir
Trying to read a directory file descriptor as if it were a file.
- LockViolation
Unable to read file due to lock. Depending on the
Ioimplementation, reading from a locked file may return this error, or may ignore the lock.- NotOpenForReading
File was not opened with read capability.
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- Unseekable
This file cannot be read positionally.
- WouldBlock
Non-blocking has been enabled, and reading from the file descriptor would block.
std.Io.Kqueue.fileReadStreaming
function
fn fileReadStreaming(userdata: ?*anyopaque, file: File, data: []const []u8) File.Reader.Error!usize
Parameters
Errors
- AccessDenied UnendingError
In WASI, this error occurs when the file descriptor does not hold the required rights to read from it.
- Canceled Cancelable
Caller has requested the async operation to stop.
- ConnectionResetByPeer UnendingError
- InputOutput UnendingError
- IsDir UnendingError
Trying to read a directory file descriptor as if it were a file.
- LockViolation UnendingError
Unable to read file due to lock. Depending on the
Ioimplementation, reading from a locked file may return this error, or may ignore the lock.- NotOpenForReading UnendingError
File was not opened with read capability.
- SocketUnconnected UnendingError
- SystemResources UnendingError
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock UnendingError
Non-blocking has been enabled, and reading from the file descriptor would block.
std.Io.Kqueue.fileSeekBy
function
fn fileSeekBy(userdata: ?*anyopaque, file: File, relative_offset: i64) File.SeekError!void
Parameters
Errors
- AccessDenied
The file descriptor does not hold the required rights to seek on it.
- Canceled Cancelable
Caller has requested the async operation to stop.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- Unseekable
std.Io.Kqueue.fileSeekTo
function
fn fileSeekTo(userdata: ?*anyopaque, file: File, absolute_offset: u64) File.SeekError!void
Parameters
Errors
- AccessDenied
The file descriptor does not hold the required rights to seek on it.
- Canceled Cancelable
Caller has requested the async operation to stop.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- Unseekable
std.Io.Kqueue.fileStat
function
fn fileStat(userdata: ?*anyopaque, file: File) File.StatError!File.Stat
Parameters
userdata: ?*anyopaque
file: File
Errors
- AccessDenied
In WASI, this error may occur when the file descriptor does not hold the required rights to get its filestat information.
- Canceled Cancelable
Caller has requested the async operation to stop.
- PermissionDenied
- Streaming
Attempted to stat a non-file stream.
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.fileWritePositional
function
fn fileWritePositional( userdata: ?*anyopaque, file: File, header: []const u8, data: []const []const u8, splat: usize, offset: u64, ) File.WritePositionalError!usize
Parameters
userdata: ?*anyopaque
file: File
header: []const u8
data: []const []const u8
splat: usize
offset: u64
Errors
- AccessDenied
File descriptor does not hold the required rights to write to it.
- BrokenPipe
File is an unconnected socket, or closed its read end.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DeviceBusy
- DiskQuota
- FileBusy
- FileTooBig
- InputOutput
- LockViolation
The process cannot access the file because another process has locked a portion of the file. Windows-only.
- NoDevice
This error occurs when a device gets disconnected before or mid-flush while it's being written to - errno(6): No such device or address.
- NoSpaceLeft
- NotOpenForWriting
File was not opened with write capability.
- PermissionDenied
- SystemResources
Insufficient kernel memory to read from in_fd.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- Unseekable
This file cannot be written positionally.
- WouldBlock
Non-blocking has been enabled and this operation would block.
std.Io.Kqueue.fileWriteStreaming
function
fn fileWriteStreaming( userdata: ?*anyopaque, file: File, header: []const u8, data: []const []const u8, splat: usize, ) File.Writer.Error!usize
Parameters
Errors
- AccessDenied Error
File descriptor does not hold the required rights to write to it.
- BrokenPipe Error
File is an unconnected socket, or closed its read end.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DeviceBusy Error
- DiskQuota Error
- FileBusy Error
- FileTooBig Error
- InputOutput Error
- LockViolation Error
The process cannot access the file because another process has locked a portion of the file. Windows-only.
- NoDevice Error
This error occurs when a device gets disconnected before or mid-flush while it's being written to - errno(6): No such device or address.
- NoSpaceLeft Error
- NotOpenForWriting Error
- PermissionDenied Error
- SystemResources Error
Insufficient kernel memory to read from in_fd.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock Error
Non-blocking has been enabled and this operation would block.
std.Io.Kqueue.findReadyFiber
function
fn findReadyFiber(k: *Kqueue, thread: *Thread) ?*Fiber
Parameters
std.Io.Kqueue.groupAsync
function
fn groupAsync( userdata: ?*anyopaque, type_erased: *Io.Group, context: []const u8, context_alignment: Alignment, start: *const fn (context: *const anyopaque) void, ) void
Parameters
std.Io.Kqueue.groupAwait
function
fn groupAwait(userdata: ?*anyopaque, type_erased: *Io.Group, initial_token: *anyopaque) Io.Cancelable!void
Parameters
Errors
- Canceled
Caller has requested the async operation to stop.
std.Io.Kqueue.groupCancel
function
fn groupCancel(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void
Parameters
std.Io.Kqueue.groupConcurrent
function
fn groupConcurrent( userdata: ?*anyopaque, type_erased: *Io.Group, context: []const u8, context_alignment: Alignment, start: *const fn (context: *const anyopaque) void, ) Io.ConcurrentError!void
Parameters
userdata: ?*anyopaque
type_erased: *Io.Group
context: []const u8
context_alignment: Alignment
start: *const fn (context: *const anyopaque) void
Errors
- ConcurrencyUnavailable
May occur due to a temporary condition such as resource exhaustion, or to the Io implementation not supporting concurrency.
std.Io.Kqueue.idle
function
fn idle(k: *Kqueue, thread: *Thread) void
Parameters
std.Io.Kqueue.idle_stack_size
constant
Empirically saw >128KB being used by the self-hosted backend to panic.
std.Io.Kqueue.init
function
pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void
Parameters
std.Io.Kqueue.InitError
error-set
Errors
- OutOfMemory Error
- ProcessFdQuotaExceeded CreateFileDescriptorError
The per-process limit on the number of open file descriptors has been reached.
- SystemFdQuotaExceeded CreateFileDescriptorError
The system-wide limit on the total number of open files has been reached.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.InitOptions
container
Fields
n_threads: ?usize = null
std.Io.Kqueue.io
function
pub fn io(k: *Kqueue) Io
Parameters
k: *Kqueue
std.Io.Kqueue.Io
alias
Alias of std.Io
std.Io.Kqueue.IpAddress
alias
Alias of std.Io.net.IpAddress
std.Io.Kqueue.kevent
function
pub fn kevent( kq: i32, changelist: []const posix.Kevent, eventlist: []posix.Kevent, timeout: ?*const posix.timespec, ) KEventError!usize
Parameters
Errors
- AccessDenied
The process does not have permission to register a filter.
- EventNotFound
The event could not be found to be modified or deleted.
- Overflow
changelist or eventlist had too many items on it. TODO remove this possibility
- ProcessNotFound
The specified process to attach to does not exist.
- SystemResources
No memory was available to register the event.
std.Io.Kqueue.KEventError
error-set
Errors
- AccessDenied
The process does not have permission to register a filter.
- EventNotFound
The event could not be found to be modified or deleted.
- Overflow
changelist or eventlist had too many items on it. TODO remove this possibility
- ProcessNotFound
The specified process to attach to does not exist.
- SystemResources
No memory was available to register the event.
std.Io.Kqueue.Kqueue
alias
Alias of std.Io.Kqueue
std.Io.Kqueue.mainIdle
function
fn mainIdle(k: *Kqueue, message: *const SwitchMessage) callconv(.withStackAlign(.c, @max(@alignOf(Thread), @alignOf(Io.fiber.Context)))) noreturn
Parameters
k: *Kqueue
message: *const SwitchMessage
std.Io.Kqueue.mainIdleEntry
function
fn mainIdleEntry() callconv(.naked) void
std.Io.Kqueue.max_idle_search
constant
std.Io.Kqueue.max_iovecs_len
constant
std.Io.Kqueue.max_steal_ready_search
constant
std.Io.Kqueue.net
alias
Alias of std.Io.net
std.Io.Kqueue.netAccept
function
fn netAccept(userdata: ?*anyopaque, server: net.Socket.Handle, options: net.Server.AcceptOptions) net.Server.AcceptError!net.Socket
Parameters
Errors
- BlockedByFirewall
Firewall rules forbid connection.
- Canceled Cancelable
Caller has requested the async operation to stop.
- ConnectionAborted
An incoming connection was indicated, but was subsequently terminated by the remote peer prior to accepting the call.
- NetworkDown
The network subsystem has failed.
- ProcessFdQuotaExceeded
The per-process limit on the number of open file descriptors has been reached.
- ProtocolFailure
- SocketNotListening
Either
listenwas never called, orshutdownwas called (possibly while this call was blocking). This allowsshutdownto be used as a concurrent cancellation mechanism.- SystemFdQuotaExceeded
The system-wide limit on the total number of open files has been reached.
- SystemResources
Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock
No connection is already queued and ready to be accepted, and the socket is configured as non-blocking.
std.Io.Kqueue.netBindIp
function
fn netBindIp( userdata: ?*anyopaque, address: *const net.IpAddress, options: net.IpAddress.BindOptions, ) net.IpAddress.BindError!net.Socket
Parameters
Errors
- AccessDenied
The address is protected and the current user does not have permission to bind it.
- AddressFamilyUnsupported
The address is not valid for the address family of socket.
- AddressInUse
The address is already taken. Can occur when bound port is 0 but all ephemeral ports are already in use.
- AddressUnavailable
A nonexistent interface was requested or the requested address was not local.
- Canceled Cancelable
Caller has requested the async operation to stop.
- NetworkDown
The local network interface used to reach the destination is offline.
- OptionUnsupported
One of the
BindOptionsis not supported by the Io implementation.- ProcessFdQuotaExceeded
Per-process limit on the number of open file descriptors has been reached.
- ProtocolUnsupportedByAddressFamily
- ProtocolUnsupportedBySystem
- SocketModeUnsupported
- SystemFdQuotaExceeded
System-wide limit on the total number of open files has been reached.
- SystemResources
Insufficient memory or other resource internal to the operating system.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.netConnectIp
function
fn netConnectIp(userdata: ?*anyopaque, address: *const net.IpAddress, options: net.IpAddress.ConnectOptions) net.IpAddress.ConnectError!net.Socket
Parameters
Errors
- AccessDenied
The user tried to connect to a broadcast address without having the socket broadcast flag enabled or the connection request failed because of a local firewall rule.
- AddressFamilyUnsupported
- AddressUnavailable
- Canceled Cancelable
Caller has requested the async operation to stop.
- ConnectionPending
- ConnectionRefused
- ConnectionResetByPeer
- HostUnreachable
- NetworkDown
- NetworkUnreachable
- OptionUnsupported
One of the
ConnectOptionsis not supported by the Io implementation.- ProcessFdQuotaExceeded
Per-process limit on the number of open file descriptors has been reached.
- ProtocolUnsupportedByAddressFamily
- ProtocolUnsupportedBySystem
- SocketModeUnsupported
- SystemFdQuotaExceeded
System-wide limit on the total number of open files has been reached.
- SystemResources
Insufficient memory or other resource internal to the operating system.
- Timeout
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock
Non-blocking was requested and the operation cannot return immediately.
std.Io.Kqueue.netConnectUnix
function
fn netConnectUnix( userdata: ?*anyopaque, unix_address: *const net.UnixAddress, ) net.UnixAddress.ConnectError!net.Socket.Handle
Parameters
userdata: ?*anyopaque
unix_address: *const net.UnixAddress
Errors
- AccessDenied
- AddressFamilyUnsupported
- Canceled Cancelable
Caller has requested the async operation to stop.
- ConnectionRefused
- FileNotFound
- NetworkDown
- NotDir
- PermissionDenied
- ProcessFdQuotaExceeded
- ProtocolUnsupportedBySystem
- ReadOnlyFileSystem
- SocketModeUnsupported
- SymLinkLoop
- SystemFdQuotaExceeded
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- WouldBlock
std.Io.Kqueue.netInterfaceName
function
fn netInterfaceName(userdata: ?*anyopaque, interface: net.Interface) net.Interface.NameError!net.Interface.Name
Parameters
Errors
- Canceled Cancelable
Caller has requested the async operation to stop.
- InterfaceNotFound
Out of range
index.- NameTooLong
Interface name longer than
Name.max_len.- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.netInterfaceNameResolve
function
fn netInterfaceNameResolve( userdata: ?*anyopaque, name: *const net.Interface.Name, ) net.Interface.Name.ResolveError!net.Interface
Parameters
Errors
- AccessDenied
- Canceled Cancelable
Caller has requested the async operation to stop.
- InterfaceNotFound
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.netListenIp
function
fn netListenIp( userdata: ?*anyopaque, address: *const net.IpAddress, options: net.IpAddress.ListenOptions, ) net.IpAddress.ListenError!net.Socket
Parameters
Errors
- AccessDenied
The address is protected and the current user does not have permission to bind it.
- AddressFamilyUnsupported
The requested address family (IPv4 or IPv6) is not supported by the operating system.
- AddressInUse
The address is already taken. Can occur when bound port is 0 but all ephemeral ports are already in use.
- AddressUnavailable
A nonexistent interface was requested or the requested address was not local.
- Canceled Cancelable
Caller has requested the async operation to stop.
- NetworkDown
The local network interface used to reach the destination is offline.
- OptionUnsupported
One of the
ListenOptionsis not supported by the Io implementation.- ProcessFdQuotaExceeded
Per-process limit on the number of open file descriptors has been reached.
- ProtocolUnsupportedByAddressFamily
- ProtocolUnsupportedBySystem
- SocketModeUnsupported
- SystemFdQuotaExceeded
System-wide limit on the total number of open files has been reached.
- SystemResources
Insufficient memory or other resource internal to the operating system.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.netListenUnix
function
fn netListenUnix( userdata: ?*anyopaque, unix_address: *const net.UnixAddress, options: net.UnixAddress.ListenOptions, ) net.UnixAddress.ListenError!net.Socket.Handle
Parameters
Errors
- AccessDenied
- AddressFamilyUnsupported
- AddressInUse
- AddressUnavailable
- Canceled Cancelable
Caller has requested the async operation to stop.
- FileNotFound
- NetworkDown
- NotDir
- PermissionDenied
- ProcessFdQuotaExceeded
- ReadOnlyFileSystem
- SymLinkLoop
- SystemFdQuotaExceeded
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.netLookup
function
fn netLookup( userdata: ?*anyopaque, host_name: net.HostName, resolved: *Io.Queue(net.HostName.LookupResult), options: net.HostName.LookupOptions, ) net.HostName.LookupError!void
Parameters
userdata: ?*anyopaque
host_name: net.HostName
resolved: *Io.Queue(net.HostName.LookupResult)
options: net.HostName.LookupOptions
Errors
- AccessDenied BindError
The address is protected and the current user does not have permission to bind it.
- AddressFamilyUnsupported BindError
The address is not valid for the address family of socket.
- AddressInUse BindError
The address is already taken. Can occur when bound port is 0 but all ephemeral ports are already in use.
- AddressUnavailable BindError
A nonexistent interface was requested or the requested address was not local.
- Canceled Cancelable
Caller has requested the async operation to stop.
- DetectingNetworkConfigurationFailed
Failed to open or read "/etc/hosts" or "/etc/resolv.conf".
- InvalidDnsAAAARecord
- InvalidDnsARecord
- InvalidDnsCnameRecord
- NameServerFailure
- NetworkDown BindError
The local network interface used to reach the destination is offline.
- NoAddressReturned
- OptionUnsupported BindError
One of the
BindOptionsis not supported by the Io implementation.- ProcessFdQuotaExceeded BindError
Per-process limit on the number of open file descriptors has been reached.
- ProtocolUnsupportedByAddressFamily BindError
- ProtocolUnsupportedBySystem BindError
- ResolvConfParseFailed
- SocketModeUnsupported BindError
- SystemFdQuotaExceeded BindError
System-wide limit on the total number of open files has been reached.
- SystemResources BindError
Insufficient memory or other resource internal to the operating system.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
- UnknownHostName
std.Io.Kqueue.netRead
function
fn netRead(userdata: ?*anyopaque, fd: net.Socket.Handle, data: [][]u8) net.Stream.Reader.Error!usize
Parameters
Errors
- AccessDenied Error
File descriptor does not hold the required rights to read from it.
- Canceled Cancelable
Caller has requested the async operation to stop.
- ConnectionResetByPeer Error
- ConnectionTimedOut Error
The remote peer did not respond to ongoing communication, causing the OS to abort the connection.
- NetworkDown Error
- SocketUnconnected Error
- SystemResources Error
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.netReceive
type-function
fn netReceive( userdata: ?*anyopaque, handle: net.Socket.Handle, message_buffer: []net.IncomingMessage, data_buffer: []u8, flags: net.ReceiveFlags, timeout: Io.Timeout, ) struct { ?net.Socket.ReceiveTimeoutError, usize }
Parameters
userdata: ?*anyopaque
handle: net.Socket.Handle
message_buffer: []net.IncomingMessage
data_buffer: []u8
flags: net.ReceiveFlags
timeout: Io.Timeout
std.Io.Kqueue.netSend
type-function
fn netSend( userdata: ?*anyopaque, handle: net.Socket.Handle, outgoing_messages: []net.OutgoingMessage, flags: net.SendFlags, ) struct { ?net.Socket.SendError, usize }
Parameters
std.Io.Kqueue.netSendOne
function
fn netSendOne( k: *Kqueue, handle: net.Socket.Handle, message: *net.OutgoingMessage, flags: u32, ) net.Socket.SendError!void
Parameters
Errors
- AccessDenied Error
An attempt was made to send to a network/broadcast address as though it was a unicast address.
- AddressFamilyUnsupported Error
Operating system or protocol does not support the address family.
- Canceled Cancelable
Caller has requested the async operation to stop.
- ConnectionRefused Error
The destination address is not listening. Can still occur for connectionless messages.
- ConnectionResetByPeer Error
Network session was unexpectedly closed by recipient.
- ConnectionTimedOut Error
The remote peer did not respond to ongoing communication, causing the OS to abort the connection.
- FastOpenAlreadyInProgress Error
Another TCP Fast Open is already in progress.
- HostUnreachable Error
Network reached but no route to host.
- MessageOversize Error
The socket type requires that message be sent atomically, and the size of the message to be sent made this impossible. The message was not transmitted, or was partially transmitted.
- NetworkDown Error
The local network interface used to reach the destination is offline.
- NetworkUnreachable Error
No route to network.
- SocketUnconnected Error
Local end has been shut down on a connection-oriented socket, or the socket was never connected.
- SystemResources Error
The output queue for a network interface was full. This generally indicates that the interface has stopped sending, but may be caused by transient congestion. (Normally, this does not occur in Linux. Packets are just silently dropped when a device queue overflows.)
This is also caused when there is not enough kernel memory available.
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.netShutdown
function
fn netShutdown(userdata: ?*anyopaque, handle: net.Socket.Handle, how: net.ShutdownHow) net.ShutdownError!void
Parameters
Errors
- Canceled Cancelable
Caller has requested the async operation to stop.
- ConnectionAborted
- ConnectionResetByPeer
- NetworkDown
- SocketUnconnected
- SystemResources
- Unexpected UnexpectedError
The Operating System returned an undocumented error code.
This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.
When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.
std.Io.Kqueue.now
function
fn now(userdata: ?*anyopaque, clock: Io.Clock) Io.Clock.Error!Io.Timestamp
Parameters
std.Io.Kqueue.openSocketPosix
function
fn openSocketPosix( k: *Kqueue, family: posix.sa_family_t, options: IpAddress.BindOptions, ) error{ AddressFamilyUnsupported, ProtocolUnsupportedBySystem, ProcessFdQuotaExceeded, SystemFdQuotaExceeded, SystemResources, ProtocolUnsupportedByAddressFamily, SocketModeUnsupported, OptionUnsupported, Unexpected, Canceled, }!posix.socket_t
Parameters
Errors
- AddressFamilyUnsupported
- Canceled
- OptionUnsupported
- ProcessFdQuotaExceeded
- ProtocolUnsupportedByAddressFamily
- ProtocolUnsupportedBySystem
- SocketModeUnsupported
- SystemFdQuotaExceeded
- SystemResources
- Unexpected
std.Io.Kqueue.posix
alias
Alias of std.posix
std.Io.Kqueue.posixBind
function
fn posixBind( k: *Kqueue, socket_fd: posix.socket_t, addr: *const posix.sockaddr, addr_len: posix.socklen_t, ) !void
Parameters
std.Io.Kqueue.posixConnect
function
fn posixConnect(k: *Kqueue, socket_fd: posix.socket_t, addr: *const posix.sockaddr, addr_len: posix.socklen_t) !void
Parameters
std.Io.Kqueue.posixGetSockName
function
fn posixGetSockName(k: *Kqueue, socket_fd: posix.fd_t, addr: *posix.sockaddr, addr_len: *posix.socklen_t) !void
Parameters
std.Io.Kqueue.posixSocketModeProtocol
alias
std.Io.Kqueue.recycle
function
fn recycle(k: *Kqueue, fiber: *Fiber) void
Parameters
std.Io.Kqueue.schedule
function
fn schedule(k: *Kqueue, thread: *Thread, ready_queue: Fiber.Queue) void
Parameters
std.Io.Kqueue.setSocketOption
function
fn setSocketOption(k: *Kqueue, fd: posix.fd_t, level: i32, opt_name: u32, option: u32) !void
Parameters
std.Io.Kqueue.sleep
function
fn sleep(userdata: ?*anyopaque, timeout: Io.Timeout) Io.SleepError!void
Parameters
std.Io.Kqueue.std
alias
Alias of std
std.Io.Kqueue.SwitchMessage
container
Fields
contexts: Io.fiber.Switch
pending_task: PendingTask
std.Io.Kqueue.SwitchMessage.handle
function
fn handle(message: *const SwitchMessage, k: *Kqueue) void
Parameters
message: *const SwitchMessage
k: *Kqueue
std.Io.Kqueue.SwitchMessage.PendingTask
container
Fields
std.Io.Kqueue.Thread
container
Fields
thread: std.Thread
idle_context: Io.fiber.Context
current_context: *Io.fiber.Context
ready_queue: ?*Fiber
kq_fd: posix.fd_t
idle_search_index: u32
steal_ready_search_index: u32
wait_queues: std.array_hash_map.Auto(WaitQueueKey, *Fiber)For ensuring multiple fibers waiting on the same file descriptor and filter use the same kevent.
std.Io.Kqueue.Thread.canceling
constant
?*Thread
std.Io.Kqueue.Thread.current
function
fn current() *Thread
std.Io.Kqueue.Thread.currentFiber
function
fn currentFiber(thread: *Thread) *Fiber
Parameters
thread: *Thread
std.Io.Kqueue.Thread.deinit
function
fn deinit(thread: *Thread, gpa: Allocator) void
Parameters
std.Io.Kqueue.Thread.List
container
Fields
std.Io.Kqueue.Thread.self
variable
*Thread
std.Io.Kqueue.Thread.WaitQueueKey
container
Fields
ident: usize
filter: i32
std.Io.Kqueue.threadEntry
function
fn threadEntry(k: *Kqueue, index: u32) void
Parameters
k: *Kqueue
index: u32
std.Io.Kqueue.yield
function
fn yield(k: *Kqueue, maybe_ready_fiber: ?*Fiber, pending_task: SwitchMessage.PendingTask) void
Parameters
Source code
std/Io/Kqueue.zig
const Kqueue = @This();
const builtin = @import("builtin");
const std = @import("../std.zig");
const Io = std.Io;
const Dir = std.Io.Dir;
const File = std.Io.File;
const net = std.Io.net;
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const Alignment = std.mem.Alignment;
const IpAddress = std.Io.net.IpAddress;
const errnoBug = std.Io.Threaded.errnoBug;
const closeFd = std.Io.Threaded.closeFd;
const posix = std.posix;
const posixSocketModeProtocol = Io.Threaded.posixSocketModeProtocol;
/// Must be a thread-safe allocator.
gpa: Allocator,
mutex: Io.Mutex,
main_fiber_buffer: [@sizeOf(Fiber) + Fiber.max_result_size]u8 align(@alignOf(Fiber)),
threads: Thread.List,
/// Empirically saw >128KB being used by the self-hosted backend to panic.
const idle_stack_size = 256 * 1024;
const max_idle_search = 4;
const max_steal_ready_search = 4;
const max_iovecs_len = 8;
const changes_buffer_len = 64;
const Thread = struct {
thread: std.Thread,
idle_context: Io.fiber.Context,
current_context: *Io.fiber.Context,
ready_queue: ?*Fiber,
kq_fd: posix.fd_t,
idle_search_index: u32,
steal_ready_search_index: u32,
/// For ensuring multiple fibers waiting on the same file descriptor and
/// filter use the same kevent.
wait_queues: std.array_hash_map.Auto(WaitQueueKey, *Fiber),
const WaitQueueKey = struct {
ident: usize,
filter: i32,
};
const canceling: ?*Thread = @ptrFromInt(@alignOf(Thread));
threadlocal var self: *Thread = undefined;
fn current() *Thread {
return self;
}
fn currentFiber(thread: *Thread) *Fiber {
return @fieldParentPtr("context", thread.current_context);
}
const List = struct {
allocated: []Thread,
reserved: u32,
active: u32,
};
fn deinit(thread: *Thread, gpa: Allocator) void {
closeFd(thread.kq_fd);
assert(thread.wait_queues.count() == 0);
thread.wait_queues.deinit(gpa);
thread.* = undefined;
}
};
const Fiber = struct {
required_align: void align(4),
context: Io.fiber.Context,
awaiter: ?*Fiber,
queue_next: ?*Fiber,
cancel_thread: ?*Thread,
awaiting_completions: std.bit_set.Static(3),
const finished: ?*Fiber = @ptrFromInt(@alignOf(Thread));
const max_result_align: Alignment = .@"16";
const max_result_size = max_result_align.forward(64);
/// This includes any stack realignments that need to happen, and also the
/// initial frame return address slot and argument frame, depending on target.
const min_stack_size = 4 * 1024 * 1024;
const max_context_align: Alignment = .@"16";
const max_context_size = max_context_align.forward(1024);
const max_closure_size: usize = @sizeOf(AsyncClosure);
const max_closure_align: Alignment = .of(AsyncClosure);
const allocation_size = std.mem.alignForward(
usize,
max_closure_align.max(max_context_align).forward(
max_result_align.forward(@sizeOf(Fiber)) + max_result_size + min_stack_size,
) + max_closure_size + max_context_size,
std.heap.page_size_max,
);
fn allocate(k: *Kqueue) error{OutOfMemory}!*Fiber {
return @ptrCast(try k.gpa.alignedAlloc(u8, .of(Fiber), allocation_size));
}
fn allocatedSlice(f: *Fiber) []align(@alignOf(Fiber)) u8 {
return @as([*]align(@alignOf(Fiber)) u8, @ptrCast(f))[0..allocation_size];
}
fn allocatedEnd(f: *Fiber) [*]u8 {
const allocated_slice = f.allocatedSlice();
return allocated_slice[allocated_slice.len..].ptr;
}
fn resultPointer(f: *Fiber, comptime Result: type) *Result {
return @ptrCast(@alignCast(f.resultBytes(.of(Result))));
}
fn resultBytes(f: *Fiber, alignment: Alignment) [*]u8 {
return @ptrFromInt(alignment.forward(@intFromPtr(f) + @sizeOf(Fiber)));
}
fn enterCancelRegion(fiber: *Fiber, thread: *Thread) error{Canceled}!void {
if (@cmpxchgStrong(
?*Thread,
&fiber.cancel_thread,
null,
thread,
.acq_rel,
.acquire,
)) |cancel_thread| {
assert(cancel_thread == Thread.canceling);
return error.Canceled;
}
}
fn exitCancelRegion(fiber: *Fiber, thread: *Thread) void {
if (@cmpxchgStrong(
?*Thread,
&fiber.cancel_thread,
thread,
null,
.acq_rel,
.acquire,
)) |cancel_thread| assert(cancel_thread == Thread.canceling);
}
const Queue = struct { head: *Fiber, tail: *Fiber };
};
fn recycle(k: *Kqueue, fiber: *Fiber) void {
std.log.debug("recyling {*}", .{fiber});
assert(fiber.queue_next == null);
k.gpa.free(fiber.allocatedSlice());
}
pub const InitOptions = struct {
n_threads: ?usize = null,
};
pub const InitError = Allocator.Error || CreateFileDescriptorError;
pub fn init(k: *Kqueue, gpa: Allocator, options: InitOptions) !void {
assert(options.n_threads != 0);
const n_threads = @max(1, options.n_threads orelse std.Thread.getCpuCount() catch 1);
const threads_size = n_threads * @sizeOf(Thread);
const idle_stack_end_offset = std.mem.alignForward(usize, threads_size + idle_stack_size, std.heap.page_size_max);
const allocated_slice = try gpa.alignedAlloc(u8, .of(Thread), idle_stack_end_offset);
errdefer gpa.free(allocated_slice);
k.* = .{
.gpa = gpa,
.mutex = .init,
.main_fiber_buffer = undefined,
.threads = .{
.allocated = @ptrCast(allocated_slice[0..threads_size]),
.reserved = 1,
.active = 1,
},
};
const main_fiber: *Fiber = @ptrCast(&k.main_fiber_buffer);
main_fiber.* = .{
.required_align = {},
.context = undefined,
.awaiter = null,
.queue_next = null,
.cancel_thread = null,
.awaiting_completions = .empty,
};
const main_thread = &k.threads.allocated[0];
Thread.self = main_thread;
const idle_stack_end: [*]align(16) usize = @ptrCast(@alignCast(allocated_slice[idle_stack_end_offset..].ptr));
(idle_stack_end - 1)[0..1].* = .{@intFromPtr(k)};
main_thread.* = .{
.thread = undefined,
.idle_context = switch (builtin.cpu.arch) {
.aarch64 => .{
.sp = @intFromPtr(idle_stack_end),
.fp = 0,
.pc = @intFromPtr(&mainIdleEntry),
},
.x86_64 => .{
.rsp = @intFromPtr(idle_stack_end - 1),
.rbp = 0,
.rip = @intFromPtr(&mainIdleEntry),
},
else => @compileError("unimplemented architecture"),
},
.current_context = &main_fiber.context,
.ready_queue = null,
.kq_fd = try createFileDescriptor(),
.idle_search_index = 1,
.steal_ready_search_index = 1,
.wait_queues = .empty,
};
errdefer closeFd(main_thread.kq_fd);
std.log.debug("created main idle {*}", .{&main_thread.idle_context});
std.log.debug("created main {*}", .{main_fiber});
}
pub fn deinit(k: *Kqueue) void {
const active_threads = @atomicLoad(u32, &k.threads.active, .acquire);
for (k.threads.allocated[0..active_threads]) |*thread| {
const ready_fiber = @atomicLoad(?*Fiber, &thread.ready_queue, .monotonic);
assert(ready_fiber == null or ready_fiber == Fiber.finished); // pending async
}
k.yield(null, .exit);
const main_thread = &k.threads.allocated[0];
const gpa = k.gpa;
main_thread.deinit(gpa);
const allocated_ptr: [*]align(@alignOf(Thread)) u8 = @ptrCast(@alignCast(k.threads.allocated.ptr));
const idle_stack_end_offset = std.mem.alignForward(usize, k.threads.allocated.len * @sizeOf(Thread) + idle_stack_size, std.heap.page_size_max);
for (k.threads.allocated[1..active_threads]) |*thread| thread.thread.join();
gpa.free(allocated_ptr[0..idle_stack_end_offset]);
k.* = undefined;
}
pub const CreateFileDescriptorError = error{
/// The per-process limit on the number of open file descriptors has been reached.
ProcessFdQuotaExceeded,
/// The system-wide limit on the total number of open files has been reached.
SystemFdQuotaExceeded,
} || Io.UnexpectedError;
pub fn createFileDescriptor() CreateFileDescriptorError!posix.fd_t {
const rc = posix.system.kqueue();
switch (posix.errno(rc)) {
.SUCCESS => return @intCast(rc),
.MFILE => return error.ProcessFdQuotaExceeded,
.NFILE => return error.SystemFdQuotaExceeded,
else => |err| return posix.unexpectedErrno(err),
}
}
fn findReadyFiber(k: *Kqueue, thread: *Thread) ?*Fiber {
if (@atomicRmw(?*Fiber, &thread.ready_queue, .Xchg, Fiber.finished, .acquire)) |ready_fiber| {
@atomicStore(?*Fiber, &thread.ready_queue, ready_fiber.queue_next, .release);
ready_fiber.queue_next = null;
return ready_fiber;
}
const active_threads = @atomicLoad(u32, &k.threads.active, .acquire);
for (0..@min(max_steal_ready_search, active_threads)) |_| {
defer thread.steal_ready_search_index += 1;
if (thread.steal_ready_search_index == active_threads) thread.steal_ready_search_index = 0;
const steal_ready_search_thread = &k.threads.allocated[0..active_threads][thread.steal_ready_search_index];
if (steal_ready_search_thread == thread) continue;
const ready_fiber = @atomicLoad(?*Fiber, &steal_ready_search_thread.ready_queue, .acquire) orelse continue;
if (ready_fiber == Fiber.finished) continue;
if (@cmpxchgWeak(
?*Fiber,
&steal_ready_search_thread.ready_queue,
ready_fiber,
null,
.acquire,
.monotonic,
)) |_| continue;
@atomicStore(?*Fiber, &thread.ready_queue, ready_fiber.queue_next, .release);
ready_fiber.queue_next = null;
return ready_fiber;
}
// couldn't find anything to do, so we are now open for business
@atomicStore(?*Fiber, &thread.ready_queue, null, .monotonic);
return null;
}
fn yield(k: *Kqueue, maybe_ready_fiber: ?*Fiber, pending_task: SwitchMessage.PendingTask) void {
const thread: *Thread = .current();
const ready_context = if (maybe_ready_fiber orelse k.findReadyFiber(thread)) |ready_fiber|
&ready_fiber.context
else
&thread.idle_context;
const message: SwitchMessage = .{
.contexts = .{
.old = thread.current_context,
.new = ready_context,
},
.pending_task = pending_task,
};
std.log.debug("switching from {*} to {*}", .{ message.contexts.old, message.contexts.new });
contextSwitch(&message).handle(k);
}
fn schedule(k: *Kqueue, thread: *Thread, ready_queue: Fiber.Queue) void {
{
var fiber = ready_queue.head;
while (true) {
std.log.debug("scheduling {*}", .{fiber});
fiber = fiber.queue_next orelse break;
}
assert(fiber == ready_queue.tail);
}
// shared fields of previous `Thread` must be initialized before later ones are marked as active
const new_thread_index = @atomicLoad(u32, &k.threads.active, .acquire);
for (0..@min(max_idle_search, new_thread_index)) |_| {
defer thread.idle_search_index += 1;
if (thread.idle_search_index == new_thread_index) thread.idle_search_index = 0;
const idle_search_thread = &k.threads.allocated[0..new_thread_index][thread.idle_search_index];
if (idle_search_thread == thread) continue;
if (@cmpxchgWeak(
?*Fiber,
&idle_search_thread.ready_queue,
null,
ready_queue.head,
.release,
.monotonic,
)) |_| continue;
const changes = [_]posix.Kevent{
.{
.ident = 0,
.filter = std.c.EVFILT.USER,
.flags = std.c.EV.ADD | std.c.EV.ONESHOT,
.fflags = std.c.NOTE.TRIGGER,
.data = 0,
.udata = @backingInt(Completion.UserData.wakeup),
},
};
// If an error occurs it only pessimises scheduling.
_ = kevent(idle_search_thread.kq_fd, &changes, &.{}, null) catch |err| {
// TODO handle EINTR for cancellation purposes
@panic(@errorName(err)); // TODO
};
return;
}
spawn_thread: {
// previous failed reservations must have completed before retrying
if (new_thread_index == k.threads.allocated.len or @cmpxchgWeak(
u32,
&k.threads.reserved,
new_thread_index,
new_thread_index + 1,
.acquire,
.monotonic,
) != null) break :spawn_thread;
const new_thread = &k.threads.allocated[new_thread_index];
const next_thread_index = new_thread_index + 1;
new_thread.* = .{
.thread = undefined,
.idle_context = undefined,
.current_context = &new_thread.idle_context,
.ready_queue = ready_queue.head,
.kq_fd = createFileDescriptor() catch |err| {
@atomicStore(u32, &k.threads.reserved, new_thread_index, .release);
// no more access to `thread` after giving up reservation
std.log.warn("unable to create worker thread due to kqueue init failure: {t}", .{err});
break :spawn_thread;
},
.idle_search_index = 0,
.steal_ready_search_index = 0,
.wait_queues = .empty,
};
new_thread.thread = std.Thread.spawn(.{
.stack_size = idle_stack_size,
.allocator = k.gpa,
}, threadEntry, .{ k, new_thread_index }) catch |err| {
closeFd(new_thread.kq_fd);
@atomicStore(u32, &k.threads.reserved, new_thread_index, .release);
// no more access to `thread` after giving up reservation
std.log.warn("unable to create worker thread due spawn failure: {s}", .{@errorName(err)});
break :spawn_thread;
};
// shared fields of `Thread` must be initialized before being marked active
@atomicStore(u32, &k.threads.active, next_thread_index, .release);
return;
}
// nobody wanted it, so just queue it on ourselves
while (@cmpxchgWeak(
?*Fiber,
&thread.ready_queue,
ready_queue.tail.queue_next,
ready_queue.head,
.acq_rel,
.acquire,
)) |old_head| ready_queue.tail.queue_next = old_head;
}
fn mainIdle(k: *Kqueue, message: *const SwitchMessage) callconv(.withStackAlign(.c, @max(@alignOf(Thread), @alignOf(Io.fiber.Context)))) noreturn {
message.handle(k);
k.idle(&k.threads.allocated[0]);
k.yield(@ptrCast(&k.main_fiber_buffer), .nothing);
unreachable; // switched to dead fiber
}
fn threadEntry(k: *Kqueue, index: u32) void {
const thread: *Thread = &k.threads.allocated[index];
Thread.self = thread;
std.log.debug("created thread idle {*}", .{&thread.idle_context});
k.idle(thread);
thread.deinit(k.gpa);
}
const Completion = struct {
const UserData = enum(usize) {
unused,
wakeup,
cleanup,
exit,
/// *Fiber
_,
};
/// Corresponds to Kevent field.
flags: u16,
/// Corresponds to Kevent field.
fflags: u32,
/// Corresponds to Kevent field.
data: isize,
};
fn idle(k: *Kqueue, thread: *Thread) void {
var events_buffer: [changes_buffer_len]posix.Kevent = undefined;
var maybe_ready_fiber: ?*Fiber = null;
while (true) {
while (maybe_ready_fiber orelse k.findReadyFiber(thread)) |ready_fiber| {
k.yield(ready_fiber, .nothing);
maybe_ready_fiber = null;
}
const n = kevent(thread.kq_fd, &.{}, &events_buffer, null) catch |err| {
// TODO handle EINTR for cancellation purposes
@panic(@errorName(err)); // TODO
};
var maybe_ready_queue: ?Fiber.Queue = null;
for (events_buffer[0..n]) |event| switch (@as(Completion.UserData, @fromBackingInt(@intCast(event.udata)))) {
.unused => unreachable, // bad submission queued?
.wakeup => {},
.cleanup => @panic("failed to notify other threads that we are exiting"),
.exit => {
assert(maybe_ready_fiber == null and maybe_ready_queue == null); // pending async
return;
},
_ => {
const event_head_fiber: *Fiber = @ptrFromInt(event.udata);
const event_tail_fiber = thread.wait_queues.fetchSwapRemove(.{
.ident = event.ident,
.filter = event.filter,
}).?.value;
assert(event_tail_fiber.queue_next == null);
// TODO reevaluate this logic
event_head_fiber.resultPointer(Completion).* = .{
.flags = event.flags,
.fflags = event.fflags,
.data = event.data,
};
queue_ready: {
const head: *Fiber = if (maybe_ready_fiber == null) f: {
maybe_ready_fiber = event_head_fiber;
const next = event_head_fiber.queue_next orelse break :queue_ready;
event_head_fiber.queue_next = null;
break :f next;
} else event_head_fiber;
if (maybe_ready_queue) |*ready_queue| {
ready_queue.tail.queue_next = head;
ready_queue.tail = event_tail_fiber;
} else {
maybe_ready_queue = .{ .head = head, .tail = event_tail_fiber };
}
}
},
};
if (maybe_ready_queue) |ready_queue| k.schedule(thread, ready_queue);
}
}
const SwitchMessage = struct {
contexts: Io.fiber.Switch,
pending_task: PendingTask,
const PendingTask = union(enum) {
nothing,
reschedule,
recycle: *Fiber,
register_awaiter: *?*Fiber,
exit,
};
fn handle(message: *const SwitchMessage, k: *Kqueue) void {
const thread: *Thread = .current();
thread.current_context = message.contexts.new;
switch (message.pending_task) {
.nothing => {},
.reschedule => if (message.contexts.old != &thread.idle_context) {
const prev_fiber: *Fiber = @alignCast(@fieldParentPtr("context", message.contexts.old));
assert(prev_fiber.queue_next == null);
k.schedule(thread, .{ .head = prev_fiber, .tail = prev_fiber });
},
.recycle => |fiber| {
k.recycle(fiber);
},
.register_awaiter => |awaiter| {
const prev_fiber: *Fiber = @alignCast(@fieldParentPtr("context", message.contexts.old));
assert(prev_fiber.queue_next == null);
if (@atomicRmw(?*Fiber, awaiter, .Xchg, prev_fiber, .acq_rel) == Fiber.finished)
k.schedule(thread, .{ .head = prev_fiber, .tail = prev_fiber });
},
.exit => for (k.threads.allocated[0..@atomicLoad(u32, &k.threads.active, .acquire)]) |*each_thread| {
const changes = [_]posix.Kevent{
.{
.ident = 0,
.filter = std.c.EVFILT.USER,
.flags = std.c.EV.ADD | std.c.EV.ONESHOT,
.fflags = std.c.NOTE.TRIGGER,
.data = 0,
.udata = @backingInt(Completion.UserData.exit),
},
};
_ = kevent(each_thread.kq_fd, &changes, &.{}, null) catch |err| {
// TODO handle EINTR for cancellation purposes
@panic(@errorName(err)); // TODO
};
},
}
}
};
inline fn contextSwitch(message: *const SwitchMessage) *const SwitchMessage {
return @fieldParentPtr("contexts", Io.fiber.contextSwitch(&message.contexts));
}
fn mainIdleEntry() callconv(.naked) void {
switch (builtin.cpu.arch) {
.x86_64 => asm volatile (
\\ movq (%%rsp), %%rdi
\\ jmp %[mainIdle:P]
:
: [mainIdle] "X" (&mainIdle),
),
.aarch64 => asm volatile (
\\ ldr x0, [sp, #-8]
\\ b %[mainIdle]
:
: [mainIdle] "X" (&mainIdle),
),
else => |arch| @compileError("unimplemented architecture: " ++ @tagName(arch)),
}
}
fn fiberEntry() callconv(.naked) void {
switch (builtin.cpu.arch) {
.x86_64 => asm volatile (
\\ leaq 8(%%rsp), %%rdi
\\ jmp %[AsyncClosure_call:P]
:
: [AsyncClosure_call] "X" (&AsyncClosure.call),
),
.aarch64 => asm volatile (
\\ mov x0, sp
\\ b %[AsyncClosure_call]
:
: [AsyncClosure_call] "X" (&AsyncClosure.call),
),
else => |arch| @compileError("unimplemented architecture: " ++ @tagName(arch)),
}
}
const AsyncClosure = struct {
kqueue: *Kqueue,
fiber: *Fiber,
start: *const fn (context: *const anyopaque, result: *anyopaque) void,
result_align: Alignment,
already_awaited: bool,
fn contextPointer(closure: *AsyncClosure) [*]align(Fiber.max_context_align.toByteUnits()) u8 {
return @alignCast(@as([*]u8, @ptrCast(closure)) + @sizeOf(AsyncClosure));
}
fn call(closure: *AsyncClosure, message: *const SwitchMessage) callconv(.withStackAlign(.c, @alignOf(AsyncClosure))) noreturn {
message.handle(closure.kqueue);
const fiber = closure.fiber;
std.log.debug("{*} performing async", .{fiber});
closure.start(closure.contextPointer(), fiber.resultBytes(closure.result_align));
const awaiter = @atomicRmw(?*Fiber, &fiber.awaiter, .Xchg, Fiber.finished, .acq_rel);
const ready_awaiter = r: {
const a = awaiter orelse break :r null;
if (@atomicRmw(bool, &closure.already_awaited, .Xchg, true, .acq_rel)) break :r null;
break :r a;
};
closure.kqueue.yield(ready_awaiter, .nothing);
unreachable; // switched to dead fiber
}
fn fromFiber(fiber: *Fiber) *AsyncClosure {
return @ptrFromInt(Fiber.max_context_align.max(.of(AsyncClosure)).backward(
@intFromPtr(fiber.allocatedEnd()) - Fiber.max_context_size,
) - @sizeOf(AsyncClosure));
}
};
pub fn io(k: *Kqueue) Io {
return .{
.userdata = k,
.vtable = &.{
.async = async,
.concurrent = concurrent,
.await = await,
.cancel = cancel,
.groupAsync = groupAsync,
.groupConcurrent = groupConcurrent,
.groupAwait = groupAwait,
.groupCancel = groupCancel,
.dirCreateDir = dirCreateDir,
.dirCreateDirPath = dirCreateDirPath,
.dirCreateDirPathOpen = dirCreateDirPathOpen,
.dirStat = dirStat,
.dirStatFile = dirStatFile,
.fileStat = fileStat,
.dirAccess = dirAccess,
.dirCreateFile = dirCreateFile,
.dirOpenFile = dirOpenFile,
.dirOpenDir = dirOpenDir,
.dirClose = dirClose,
.fileClose = fileClose,
.fileWriteStreaming = fileWriteStreaming,
.fileWritePositional = fileWritePositional,
.fileReadStreaming = fileReadStreaming,
.fileReadPositional = fileReadPositional,
.fileSeekBy = fileSeekBy,
.fileSeekTo = fileSeekTo,
.now = now,
.sleep = sleep,
.netListenIp = netListenIp,
.netListenUnix = netListenUnix,
.netAccept = netAccept,
.netBindIp = netBindIp,
.netConnectIp = netConnectIp,
.netConnectUnix = netConnectUnix,
.netShutdown = netShutdown,
.netRead = netRead,
.netSend = netSend,
.netReceive = netReceive,
.netInterfaceNameResolve = netInterfaceNameResolve,
.netInterfaceName = netInterfaceName,
.netLookup = netLookup,
},
};
}
fn async(
userdata: ?*anyopaque,
result: []u8,
result_alignment: std.mem.Alignment,
context: []const u8,
context_alignment: std.mem.Alignment,
start: *const fn (context: *const anyopaque, result: *anyopaque) void,
) ?*Io.AnyFuture {
return concurrent(userdata, result.len, result_alignment, context, context_alignment, start) catch {
start(context.ptr, result.ptr);
return null;
};
}
fn concurrent(
userdata: ?*anyopaque,
result_len: usize,
result_alignment: Alignment,
context: []const u8,
context_alignment: Alignment,
start: *const fn (context: *const anyopaque, result: *anyopaque) void,
) Io.ConcurrentError!*Io.AnyFuture {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
assert(result_alignment.compare(.lte, Fiber.max_result_align)); // TODO
assert(context_alignment.compare(.lte, Fiber.max_context_align)); // TODO
assert(result_len <= Fiber.max_result_size); // TODO
assert(context.len <= Fiber.max_context_size); // TODO
const fiber = Fiber.allocate(k) catch return error.ConcurrencyUnavailable;
std.log.debug("allocated {*}", .{fiber});
const closure: *AsyncClosure = .fromFiber(fiber);
fiber.* = .{
.required_align = {},
.context = switch (builtin.cpu.arch) {
.x86_64 => .{
.rsp = @intFromPtr(closure) - @sizeOf(usize),
.rbp = 0,
.rip = @intFromPtr(&fiberEntry),
},
.aarch64 => .{
.sp = @intFromPtr(closure),
.fp = 0,
.pc = @intFromPtr(&fiberEntry),
},
else => |arch| @compileError("unimplemented architecture: " ++ @tagName(arch)),
},
.awaiter = null,
.queue_next = null,
.cancel_thread = null,
.awaiting_completions = .empty,
};
closure.* = .{
.kqueue = k,
.fiber = fiber,
.start = start,
.result_align = result_alignment,
.already_awaited = false,
};
@memcpy(closure.contextPointer(), context);
k.schedule(.current(), .{ .head = fiber, .tail = fiber });
return @ptrCast(fiber);
}
fn await(
userdata: ?*anyopaque,
any_future: *Io.AnyFuture,
result: []u8,
result_alignment: std.mem.Alignment,
) void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
const future_fiber: *Fiber = @ptrCast(@alignCast(any_future));
if (@atomicLoad(?*Fiber, &future_fiber.awaiter, .acquire) != Fiber.finished)
k.yield(null, .{ .register_awaiter = &future_fiber.awaiter });
@memcpy(result, future_fiber.resultBytes(result_alignment));
k.recycle(future_fiber);
}
fn cancel(
userdata: ?*anyopaque,
any_future: *Io.AnyFuture,
result: []u8,
result_alignment: std.mem.Alignment,
) void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = any_future;
_ = result;
_ = result_alignment;
@panic("TODO");
}
fn cancelRequested(userdata: ?*anyopaque) bool {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
return false; // TODO
}
fn groupAsync(
userdata: ?*anyopaque,
type_erased: *Io.Group,
context: []const u8,
context_alignment: Alignment,
start: *const fn (context: *const anyopaque) void,
) void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = type_erased;
_ = context;
_ = context_alignment;
_ = start;
@panic("TODO");
}
fn groupConcurrent(
userdata: ?*anyopaque,
type_erased: *Io.Group,
context: []const u8,
context_alignment: Alignment,
start: *const fn (context: *const anyopaque) void,
) Io.ConcurrentError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = type_erased;
_ = context;
_ = context_alignment;
_ = start;
@panic("TODO");
}
fn groupAwait(userdata: ?*anyopaque, type_erased: *Io.Group, initial_token: *anyopaque) Io.Cancelable!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = type_erased;
_ = initial_token;
@panic("TODO");
}
fn groupCancel(userdata: ?*anyopaque, group: *Io.Group, token: *anyopaque) void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = group;
_ = token;
@panic("TODO");
}
fn dirCreateDir(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, permissions: Dir.Permissions) Dir.CreateDirError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = permissions;
@panic("TODO");
}
fn dirCreateDirPath(
userdata: ?*anyopaque,
dir: Dir,
sub_path: []const u8,
permissions: Dir.Permissions,
) Dir.CreateDirPathError!Dir.CreatePathStatus {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = permissions;
@panic("TODO");
}
fn dirCreateDirPathOpen(
userdata: ?*anyopaque,
dir: Dir,
sub_path: []const u8,
permissions: Dir.Permissions,
options: Dir.OpenOptions,
) Dir.CreateDirPathOpenError!Dir {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = permissions;
_ = options;
@panic("TODO");
}
fn dirStat(userdata: ?*anyopaque, dir: Dir) Dir.StatError!Dir.Stat {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
@panic("TODO");
}
fn dirStatFile(
userdata: ?*anyopaque,
dir: Dir,
sub_path: []const u8,
options: Dir.StatFileOptions,
) Dir.StatFileError!File.Stat {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = options;
@panic("TODO");
}
fn dirAccess(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, options: Dir.AccessOptions) Dir.AccessError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = options;
@panic("TODO");
}
fn dirCreateFile(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, flags: Dir.CreateFileOptions) File.OpenError!File {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = flags;
@panic("TODO");
}
fn dirOpenFile(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, flags: Dir.OpenFileOptions) File.OpenError!File {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = flags;
@panic("TODO");
}
fn dirOpenDir(userdata: ?*anyopaque, dir: Dir, sub_path: []const u8, options: Dir.OpenOptions) Dir.OpenError!Dir {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dir;
_ = sub_path;
_ = options;
@panic("TODO");
}
fn dirClose(userdata: ?*anyopaque, dirs: []const Dir) void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = dirs;
@panic("TODO");
}
fn fileStat(userdata: ?*anyopaque, file: File) File.StatError!File.Stat {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = file;
@panic("TODO");
}
fn fileClose(userdata: ?*anyopaque, files: []const File) void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = files;
@panic("TODO");
}
fn fileWriteStreaming(
userdata: ?*anyopaque,
file: File,
header: []const u8,
data: []const []const u8,
splat: usize,
) File.Writer.Error!usize {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = file;
_ = header;
_ = data;
_ = splat;
@panic("TODO");
}
fn fileWritePositional(
userdata: ?*anyopaque,
file: File,
header: []const u8,
data: []const []const u8,
splat: usize,
offset: u64,
) File.WritePositionalError!usize {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = file;
_ = header;
_ = data;
_ = splat;
_ = offset;
@panic("TODO");
}
fn fileReadStreaming(userdata: ?*anyopaque, file: File, data: []const []u8) File.Reader.Error!usize {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = file;
_ = data;
@panic("TODO");
}
fn fileReadPositional(userdata: ?*anyopaque, file: File, data: []const []u8, offset: u64) File.ReadPositionalError!usize {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = file;
_ = data;
_ = offset;
@panic("TODO");
}
fn fileSeekBy(userdata: ?*anyopaque, file: File, relative_offset: i64) File.SeekError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = file;
_ = relative_offset;
@panic("TODO");
}
fn fileSeekTo(userdata: ?*anyopaque, file: File, absolute_offset: u64) File.SeekError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = file;
_ = absolute_offset;
@panic("TODO");
}
fn now(userdata: ?*anyopaque, clock: Io.Clock) Io.Clock.Error!Io.Timestamp {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = clock;
@panic("TODO");
}
fn sleep(userdata: ?*anyopaque, timeout: Io.Timeout) Io.SleepError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = timeout;
@panic("TODO");
}
fn netListenIp(
userdata: ?*anyopaque,
address: *const net.IpAddress,
options: net.IpAddress.ListenOptions,
) net.IpAddress.ListenError!net.Socket {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = address;
_ = options;
@panic("TODO");
}
fn netAccept(userdata: ?*anyopaque, server: net.Socket.Handle, options: net.Server.AcceptOptions) net.Server.AcceptError!net.Socket {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = server;
_ = options;
@panic("TODO");
}
fn netBindIp(
userdata: ?*anyopaque,
address: *const net.IpAddress,
options: net.IpAddress.BindOptions,
) net.IpAddress.BindError!net.Socket {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
const family = Io.Threaded.posixAddressFamily(address);
const socket_fd = try openSocketPosix(k, family, options);
errdefer closeFd(socket_fd);
var storage: Io.Threaded.PosixAddress = undefined;
var addr_len = Io.Threaded.addressToPosix(address, &storage);
try posixBind(k, socket_fd, &storage.any, addr_len);
if (options.allow_broadcast) try setSocketOption(k, socket_fd, posix.SOL.SOCKET, posix.SO.BROADCAST, 1);
try posixGetSockName(k, socket_fd, &storage.any, &addr_len);
return .{ .handle = socket_fd, .address = Io.Threaded.addressFromPosix(&storage) };
}
fn netConnectIp(userdata: ?*anyopaque, address: *const net.IpAddress, options: net.IpAddress.ConnectOptions) net.IpAddress.ConnectError!net.Socket {
if (options.timeout != .none) @panic("TODO");
const k: *Kqueue = @ptrCast(@alignCast(userdata));
const family = Io.Threaded.posixAddressFamily(address);
const socket_fd = try openSocketPosix(k, family, .{
.mode = options.mode,
.protocol = options.protocol,
});
errdefer closeFd(socket_fd);
var storage: Io.Threaded.PosixAddress = undefined;
var addr_len = Io.Threaded.addressToPosix(address, &storage);
try posixConnect(k, socket_fd, &storage.any, addr_len);
try posixGetSockName(k, socket_fd, &storage.any, &addr_len);
return .{ .handle = socket_fd, .address = Io.Threaded.addressFromPosix(&storage) };
}
fn posixConnect(k: *Kqueue, socket_fd: posix.socket_t, addr: *const posix.sockaddr, addr_len: posix.socklen_t) !void {
while (true) {
try k.checkCancel();
switch (posix.errno(posix.system.connect(socket_fd, addr, addr_len))) {
.SUCCESS => return,
.INTR => continue,
.CANCELED => return error.Canceled,
.AGAIN => @panic("TODO"),
.INPROGRESS => return, // Due to TCP fast open, we find out possible error later.
.ADDRNOTAVAIL => return error.AddressUnavailable,
.AFNOSUPPORT => return error.AddressFamilyUnsupported,
.ALREADY => return error.ConnectionPending,
.BADF => |err| return errnoBug(err), // File descriptor used after closed.
.CONNREFUSED => return error.ConnectionRefused,
.CONNRESET => return error.ConnectionResetByPeer,
.FAULT => |err| return errnoBug(err),
.ISCONN => |err| return errnoBug(err),
.HOSTUNREACH => return error.HostUnreachable,
.NETUNREACH => return error.NetworkUnreachable,
.NOTSOCK => |err| return errnoBug(err),
.PROTOTYPE => |err| return errnoBug(err),
.TIMEDOUT => return error.Timeout,
.CONNABORTED => |err| return errnoBug(err),
.ACCES => return error.AccessDenied,
.PERM => |err| return errnoBug(err),
.NOENT => |err| return errnoBug(err),
.NETDOWN => return error.NetworkDown,
else => |err| return posix.unexpectedErrno(err),
}
}
}
fn netListenUnix(
userdata: ?*anyopaque,
unix_address: *const net.UnixAddress,
options: net.UnixAddress.ListenOptions,
) net.UnixAddress.ListenError!net.Socket.Handle {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = unix_address;
_ = options;
@panic("TODO");
}
fn netConnectUnix(
userdata: ?*anyopaque,
unix_address: *const net.UnixAddress,
) net.UnixAddress.ConnectError!net.Socket.Handle {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = unix_address;
@panic("TODO");
}
fn netSend(
userdata: ?*anyopaque,
handle: net.Socket.Handle,
outgoing_messages: []net.OutgoingMessage,
flags: net.SendFlags,
) struct { ?net.Socket.SendError, usize } {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
const posix_flags: u32 =
@as(u32, if (@hasDecl(posix.MSG, "CONFIRM") and flags.confirm) posix.MSG.CONFIRM else 0) |
@as(u32, if (@hasDecl(posix.MSG, "DONTROUTE") and flags.dont_route) posix.MSG.DONTROUTE else 0) |
@as(u32, if (@hasDecl(posix.MSG, "EOR") and flags.eor) posix.MSG.EOR else 0) |
@as(u32, if (@hasDecl(posix.MSG, "OOB") and flags.oob) posix.MSG.OOB else 0) |
@as(u32, if (@hasDecl(posix.MSG, "FASTOPEN") and flags.fastopen) posix.MSG.FASTOPEN else 0) |
posix.MSG.NOSIGNAL;
for (outgoing_messages, 0..) |*msg, i| {
netSendOne(k, handle, msg, posix_flags) catch |err| return .{ err, i };
}
return .{ null, outgoing_messages.len };
}
fn netSendOne(
k: *Kqueue,
handle: net.Socket.Handle,
message: *net.OutgoingMessage,
flags: u32,
) net.Socket.SendError!void {
var addr: Io.Threaded.PosixAddress = undefined;
var iovec: posix.iovec_const = .{ .base = @constCast(message.data_ptr), .len = message.data_len };
const msg: posix.msghdr_const = .{
.name = &addr.any,
.namelen = Io.Threaded.addressToPosix(message.address, &addr),
.iov = (&iovec)[0..1],
.iovlen = 1,
// OS returns EINVAL if this pointer is invalid even if controllen is zero.
.control = if (message.control.len == 0) null else @constCast(message.control.ptr),
.controllen = @intCast(message.control.len),
.flags = 0,
};
while (true) {
try k.checkCancel();
const rc = posix.system.sendmsg(handle, &msg, flags);
switch (posix.errno(rc)) {
.SUCCESS => {
message.data_len = @intCast(rc);
return;
},
.INTR => continue,
.CANCELED => return error.Canceled,
.AGAIN => @panic("TODO register kevent"),
.ACCES => return error.AccessDenied,
.ALREADY => return error.FastOpenAlreadyInProgress,
.BADF => |err| return errnoBug(err), // File descriptor used after closed.
.CONNRESET => return error.ConnectionResetByPeer,
.DESTADDRREQ => |err| return errnoBug(err),
.FAULT => |err| return errnoBug(err),
.INVAL => |err| return errnoBug(err),
.ISCONN => |err| return errnoBug(err),
.MSGSIZE => return error.MessageOversize,
.NOBUFS => return error.SystemResources,
.NOMEM => return error.SystemResources,
.NOTSOCK => |err| return errnoBug(err),
.OPNOTSUPP => |err| return errnoBug(err),
.PIPE => return error.SocketUnconnected,
.AFNOSUPPORT => return error.AddressFamilyUnsupported,
.HOSTUNREACH => return error.HostUnreachable,
.NETUNREACH => return error.NetworkUnreachable,
.NOTCONN => return error.SocketUnconnected,
.NETDOWN => return error.NetworkDown,
else => |err| return posix.unexpectedErrno(err),
}
}
}
fn netReceive(
userdata: ?*anyopaque,
handle: net.Socket.Handle,
message_buffer: []net.IncomingMessage,
data_buffer: []u8,
flags: net.ReceiveFlags,
timeout: Io.Timeout,
) struct { ?net.Socket.ReceiveTimeoutError, usize } {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = handle;
_ = message_buffer;
_ = data_buffer;
_ = flags;
_ = timeout;
@panic("TODO");
}
fn netRead(userdata: ?*anyopaque, fd: net.Socket.Handle, data: [][]u8) net.Stream.Reader.Error!usize {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
var iovecs_buffer: [max_iovecs_len]posix.iovec = undefined;
var i: usize = 0;
for (data) |buf| {
if (iovecs_buffer.len - i == 0) break;
if (buf.len != 0) {
iovecs_buffer[i] = .{ .base = buf.ptr, .len = buf.len };
i += 1;
}
}
const dest = iovecs_buffer[0..i];
assert(dest[0].len > 0);
while (true) {
try k.checkCancel();
const rc = posix.system.readv(fd, dest.ptr, @intCast(dest.len));
switch (posix.errno(rc)) {
.SUCCESS => return @intCast(rc),
.INTR => continue,
.CANCELED => return error.Canceled,
.AGAIN => {
const thread: *Thread = .current();
const fiber = thread.currentFiber();
const ident: u32 = @bitCast(fd);
const filter = std.c.EVFILT.READ;
const gop = thread.wait_queues.getOrPut(k.gpa, .{
.ident = ident,
.filter = filter,
}) catch return error.SystemResources;
if (gop.found_existing) {
const tail_fiber = gop.value_ptr.*;
assert(tail_fiber.queue_next == null);
tail_fiber.queue_next = fiber;
gop.value_ptr.* = fiber;
} else {
gop.value_ptr.* = fiber;
const changes = [_]posix.Kevent{
.{
.ident = ident,
.filter = filter,
.flags = std.c.EV.ADD | std.c.EV.ONESHOT,
.fflags = 0,
.data = 0,
.udata = @intFromPtr(fiber),
},
};
assert(0 == (kevent(thread.kq_fd, &changes, &.{}, null) catch |err| {
// TODO handle EINTR for cancellation purposes
@panic(@errorName(err)); // TODO
}));
}
yield(k, null, .nothing);
continue;
},
.INVAL => |err| return errnoBug(err),
.FAULT => |err| return errnoBug(err),
.BADF => |err| return errnoBug(err), // File descriptor used after closed.
.NOBUFS => return error.SystemResources,
.NOMEM => return error.SystemResources,
.NOTCONN => return error.SocketUnconnected,
.CONNRESET => return error.ConnectionResetByPeer,
.TIMEDOUT => return error.Timeout,
.PIPE => return error.SocketUnconnected,
.NETDOWN => return error.NetworkDown,
else => |err| return posix.unexpectedErrno(err),
}
}
}
fn netShutdown(userdata: ?*anyopaque, handle: net.Socket.Handle, how: net.ShutdownHow) net.ShutdownError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = handle;
_ = how;
@panic("TODO");
}
fn netInterfaceNameResolve(
userdata: ?*anyopaque,
name: *const net.Interface.Name,
) net.Interface.Name.ResolveError!net.Interface {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = name;
@panic("TODO");
}
fn netInterfaceName(userdata: ?*anyopaque, interface: net.Interface) net.Interface.NameError!net.Interface.Name {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = interface;
@panic("TODO");
}
fn netLookup(
userdata: ?*anyopaque,
host_name: net.HostName,
resolved: *Io.Queue(net.HostName.LookupResult),
options: net.HostName.LookupOptions,
) net.HostName.LookupError!void {
const k: *Kqueue = @ptrCast(@alignCast(userdata));
_ = k;
_ = host_name;
_ = resolved;
_ = options;
@panic("TODO");
}
fn openSocketPosix(
k: *Kqueue,
family: posix.sa_family_t,
options: IpAddress.BindOptions,
) error{
AddressFamilyUnsupported,
ProtocolUnsupportedBySystem,
ProcessFdQuotaExceeded,
SystemFdQuotaExceeded,
SystemResources,
ProtocolUnsupportedByAddressFamily,
SocketModeUnsupported,
OptionUnsupported,
Unexpected,
Canceled,
}!posix.socket_t {
const mode, const protocol = try posixSocketModeProtocol(family, options.mode, options.protocol);
const socket_fd = while (true) {
try k.checkCancel();
const flags: u32 = mode | if (Io.Threaded.socket_flags_unsupported) 0 else posix.SOCK.CLOEXEC;
const socket_rc = posix.system.socket(family, flags, protocol);
switch (posix.errno(socket_rc)) {
.SUCCESS => {
const fd: posix.fd_t = @intCast(socket_rc);
errdefer closeFd(fd);
if (Io.Threaded.socket_flags_unsupported) {
while (true) {
try k.checkCancel();
switch (posix.errno(posix.system.fcntl(fd, posix.F.SETFD, @as(usize, posix.FD_CLOEXEC)))) {
.SUCCESS => break,
.INTR => continue,
.CANCELED => return error.Canceled,
else => |err| return posix.unexpectedErrno(err),
}
}
var fl_flags: usize = while (true) {
try k.checkCancel();
const rc = posix.system.fcntl(fd, posix.F.GETFL, @as(usize, 0));
switch (posix.errno(rc)) {
.SUCCESS => break @intCast(rc),
.INTR => continue,
.CANCELED => return error.Canceled,
else => |err| return posix.unexpectedErrno(err),
}
};
fl_flags |= @as(usize, 1 << @bitOffsetOf(posix.O, "NONBLOCK"));
while (true) {
try k.checkCancel();
switch (posix.errno(posix.system.fcntl(fd, posix.F.SETFL, fl_flags))) {
.SUCCESS => break,
.INTR => continue,
.CANCELED => return error.Canceled,
else => |err| return posix.unexpectedErrno(err),
}
}
}
break fd;
},
.INTR => continue,
.CANCELED => return error.Canceled,
.AFNOSUPPORT => return error.AddressFamilyUnsupported,
.INVAL => return error.ProtocolUnsupportedBySystem,
.MFILE => return error.ProcessFdQuotaExceeded,
.NFILE => return error.SystemFdQuotaExceeded,
.NOBUFS => return error.SystemResources,
.NOMEM => return error.SystemResources,
.PROTONOSUPPORT => return error.ProtocolUnsupportedByAddressFamily,
.PROTOTYPE => return error.SocketModeUnsupported,
else => |err| return posix.unexpectedErrno(err),
}
};
errdefer closeFd(socket_fd);
if (options.ip6_only) |ip6_only| {
if (posix.IPV6 == void) return error.OptionUnsupported;
try setSocketOption(k, socket_fd, posix.IPPROTO.IPV6, posix.IPV6.V6ONLY, @intFromBool(ip6_only));
}
return socket_fd;
}
fn posixBind(
k: *Kqueue,
socket_fd: posix.socket_t,
addr: *const posix.sockaddr,
addr_len: posix.socklen_t,
) !void {
while (true) {
try k.checkCancel();
switch (posix.errno(posix.system.bind(socket_fd, addr, addr_len))) {
.SUCCESS => break,
.INTR => continue,
.CANCELED => return error.Canceled,
.ACCES => return error.AccessDenied,
.ADDRINUSE => return error.AddressInUse,
.BADF => |err| return errnoBug(err), // File descriptor used after closed.
.INVAL => |err| return errnoBug(err), // invalid parameters
.NOTSOCK => |err| return errnoBug(err), // invalid `sockfd`
.AFNOSUPPORT => return error.AddressFamilyUnsupported,
.ADDRNOTAVAIL => return error.AddressUnavailable,
.FAULT => |err| return errnoBug(err), // invalid `addr` pointer
.NOMEM => return error.SystemResources,
else => |err| return posix.unexpectedErrno(err),
}
}
}
fn posixGetSockName(k: *Kqueue, socket_fd: posix.fd_t, addr: *posix.sockaddr, addr_len: *posix.socklen_t) !void {
while (true) {
try k.checkCancel();
switch (posix.errno(posix.system.getsockname(socket_fd, addr, addr_len))) {
.SUCCESS => break,
.INTR => continue,
.CANCELED => return error.Canceled,
.BADF => |err| return errnoBug(err), // File descriptor used after closed.
.FAULT => |err| return errnoBug(err),
.INVAL => |err| return errnoBug(err), // invalid parameters
.NOTSOCK => |err| return errnoBug(err), // always a race condition
.NOBUFS => return error.SystemResources,
else => |err| return posix.unexpectedErrno(err),
}
}
}
fn setSocketOption(k: *Kqueue, fd: posix.fd_t, level: i32, opt_name: u32, option: u32) !void {
const o: []const u8 = @ptrCast(&option);
while (true) {
try k.checkCancel();
switch (posix.errno(posix.system.setsockopt(fd, level, opt_name, o.ptr, @intCast(o.len)))) {
.SUCCESS => return,
.INTR => continue,
.CANCELED => return error.Canceled,
.BADF => |err| return errnoBug(err), // File descriptor used after closed.
.NOTSOCK => |err| return errnoBug(err),
.INVAL => |err| return errnoBug(err),
.FAULT => |err| return errnoBug(err),
else => |err| return posix.unexpectedErrno(err),
}
}
}
fn checkCancel(k: *Kqueue) error{Canceled}!void {
if (cancelRequested(k)) return error.Canceled;
}
pub const KEventError = error{
/// The process does not have permission to register a filter.
AccessDenied,
/// The event could not be found to be modified or deleted.
EventNotFound,
/// No memory was available to register the event.
SystemResources,
/// The specified process to attach to does not exist.
ProcessNotFound,
/// changelist or eventlist had too many items on it.
/// TODO remove this possibility
Overflow,
};
pub fn kevent(
kq: i32,
changelist: []const posix.Kevent,
eventlist: []posix.Kevent,
timeout: ?*const posix.timespec,
) KEventError!usize {
while (true) {
const rc = posix.system.kevent(
kq,
changelist.ptr,
std.math.cast(c_int, changelist.len) orelse return error.Overflow,
eventlist.ptr,
std.math.cast(c_int, eventlist.len) orelse return error.Overflow,
timeout,
);
switch (posix.errno(rc)) {
.SUCCESS => return @intCast(rc),
.ACCES => return error.AccessDenied,
.FAULT => unreachable, // TODO use error.Unexpected for these
.BADF => unreachable, // Always a race condition.
.INTR => continue, // TODO handle cancelation
.INVAL => unreachable,
.NOENT => return error.EventNotFound,
.NOMEM => return error.SystemResources,
.SRCH => return error.ProcessNotFound,
else => unreachable,
}
}
}