mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
Merge branch 'main' into statfs-implementation
This commit is contained in:
27 files changed
+292
-217
No files matched your search
@@ -0,0 +1,20 @@
|
||||
name: Create PR to merge release branch into the main branch
|
||||
# Changes should land on the earliest release branch they will need to ship on and then be automatically forwarded to main
|
||||
permissions:
|
||||
contents: read
|
||||
on:
|
||||
schedule:
|
||||
- cron: '0 9 * * *'
|
||||
workflow_dispatch:
|
||||
jobs:
|
||||
create_merge_pr:
|
||||
name: Create PR to merge release branch into main branch
|
||||
uses: swiftlang/github-workflows/.github/workflows/create_automerge_pr.yml@0.0.15
|
||||
with:
|
||||
base_branch: main
|
||||
head_branch: release/1.8.x
|
||||
pr_message: This PR was automatically opened by a GitHub action. Review the changes included in this PR and determine if they should be included on main. If yes, merge the PR. Otherwise revert changes that should not be included on this branch.
|
||||
permissions:
|
||||
contents: write
|
||||
pull-requests: write
|
||||
if: (github.event_name == 'schedule' && github.repository == 'apple/swift-system') || (github.event_name != 'schedule') # Ensure that we don't run this on a schedule in a fork
|
||||
@@ -5,7 +5,7 @@ permissions:
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
types: [opened, reopened, synchronize, ready_for_review]
|
||||
|
||||
jobs:
|
||||
tests:
|
||||
|
||||
@@ -120,7 +120,7 @@ Changes must land on the branch corresponding to the earliest release that they
|
||||
|
||||
`release/1.7.0` → `release/1.8.x` → `main`
|
||||
|
||||
For example, anything landing on `release/1.6.0` will eventually appear on `release/1.7.0`, then `release/1.8.x` and finally `main` as well. There is no need to file standalone PRs for each release line. (Change propagation currently requires manual work -- it is performed by project maintainers.)
|
||||
For example, anything landing on `release/1.7.0` will eventually appear on `release/1.8.x` and finally `main` as well. There is no need to file standalone PRs for each release line. (Merges into `main` are performed by the [automerger](.github/workflows/automerge_to_main.yml); propagation between release branches is performed by project maintainers.)
|
||||
|
||||
### Code of Conduct
|
||||
|
||||
|
||||
@@ -13,12 +13,9 @@ target_include_directories(CSystem PUBLIC
|
||||
$<INSTALL_INTERFACE:include/CSystem>)
|
||||
|
||||
install(FILES
|
||||
include/CSystemDarwin.h
|
||||
include/CSystemLinux.h
|
||||
include/CSystemShared.h
|
||||
include/CSystemWASI.h
|
||||
include/CSystemWindows.h
|
||||
include/CSystemFreeBSD.h
|
||||
include/io_uring.h
|
||||
include/module.modulemap
|
||||
DESTINATION include/CSystem)
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2026 Apple Inc. and the Swift System project authors
|
||||
Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
#if defined(__APPLE__)
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#ifdef O_CLOFORK
|
||||
#define COMPATIBILITY_O_CLOFORK O_CLOFORK
|
||||
#else
|
||||
#define COMPATIBILITY_O_CLOFORK 0
|
||||
#endif
|
||||
|
||||
#endif // defined(__APPLE__)
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2026 Apple Inc. and the Swift System project authors
|
||||
Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#ifdef O_CLOFORK
|
||||
#define COMPATIBILITY_O_CLOFORK O_CLOFORK
|
||||
#else
|
||||
#define COMPATIBILITY_O_CLOFORK 0
|
||||
#endif
|
||||
|
||||
#endif // defined(__FreeBSD__)
|
||||
@@ -9,30 +9,37 @@
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
#include <sys/epoll.h>
|
||||
// Glibc doesn't export eventfd, which the tests use.
|
||||
#include <sys/eventfd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
#include <unistd.h>
|
||||
#include "io_uring.h"
|
||||
|
||||
// Test-only: libc's `ST_*` values, which glibc gates on _GNU_SOURCE. Defining
|
||||
// that here would clash with SwiftGlibc, so shims.c defines it instead.
|
||||
#include <stdint.h>
|
||||
uint64_t _system_get_ST_RDONLY(void);
|
||||
uint64_t _system_get_ST_NOSUID(void);
|
||||
uint64_t _system_get_ST_NODEV(void);
|
||||
uint64_t _system_get_ST_NOEXEC(void);
|
||||
uint64_t _system_get_ST_SYNCHRONOUS(void);
|
||||
uint64_t _system_get_ST_MANDLOCK(void);
|
||||
uint64_t _system_get_ST_NOATIME(void);
|
||||
uint64_t _system_get_ST_NODIRATIME(void);
|
||||
uint64_t _system_get_ST_RELATIME(void);
|
||||
uint64_t _system_get_ST_NOSYMFOLLOW(void);
|
||||
#endif
|
||||
unsigned long long _system_get_ST_RDONLY(void);
|
||||
unsigned long long _system_get_ST_NOSUID(void);
|
||||
unsigned long long _system_get_ST_NODEV(void);
|
||||
unsigned long long _system_get_ST_NOEXEC(void);
|
||||
unsigned long long _system_get_ST_SYNCHRONOUS(void);
|
||||
unsigned long long _system_get_ST_MANDLOCK(void);
|
||||
unsigned long long _system_get_ST_NOATIME(void);
|
||||
unsigned long long _system_get_ST_NODIRATIME(void);
|
||||
unsigned long long _system_get_ST_RELATIME(void);
|
||||
unsigned long long _system_get_ST_NOSYMFOLLOW(void);
|
||||
|
||||
#if !defined(__ANDROID__)
|
||||
#include <stddef.h>
|
||||
#include "io_uring.h"
|
||||
|
||||
// Defined in shims.c, which can include the libc headers they need.
|
||||
extern int csystem_io_uring_setup(unsigned int entries,
|
||||
struct io_uring_params *p);
|
||||
extern int csystem_io_uring_enter(int fd, unsigned int to_submit,
|
||||
unsigned int min_complete,
|
||||
unsigned int flags, void *sig);
|
||||
extern int csystem_io_uring_enter2(int fd, unsigned int to_submit,
|
||||
unsigned int min_complete,
|
||||
unsigned int flags, void *args, size_t sz);
|
||||
extern int csystem_io_uring_register(int fd, unsigned int opcode, void *arg,
|
||||
unsigned int nr_args);
|
||||
#endif // !defined(__ANDROID__)
|
||||
|
||||
#endif // __linux__
|
||||
@@ -1,17 +0,0 @@
|
||||
/*
|
||||
This source file is part of the Swift System open source project
|
||||
|
||||
Copyright (c) 2020 Apple Inc. and the Swift System project authors
|
||||
Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#define NOMINMAX
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define VC_EXTRA_LEAN
|
||||
#include <Windows.h>
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/uio.h>
|
||||
#include <signal.h>
|
||||
#pragma once
|
||||
|
||||
// Types only: the syscall wrappers need libc headers, so they're in shims.c.
|
||||
|
||||
#define __SWIFT_IORING_SQE_FALLBACK_STRUCT { \
|
||||
__u8 opcode; \
|
||||
@@ -193,19 +192,6 @@ struct io_uring_params {
|
||||
};
|
||||
#endif // __has_include(<linux/io_uring.h>)
|
||||
|
||||
#ifndef SWIFT_IORING_C_WRAPPER
|
||||
#define SWIFT_IORING_C_WRAPPER
|
||||
|
||||
# ifndef __NR_io_uring_setup
|
||||
# define __NR_io_uring_setup 425
|
||||
# endif
|
||||
# ifndef __NR_io_uring_enter
|
||||
# define __NR_io_uring_enter 426
|
||||
# endif
|
||||
# ifndef __NR_io_uring_register
|
||||
# define __NR_io_uring_register 427
|
||||
# endif
|
||||
|
||||
/*
|
||||
struct io_uring_getevents_arg {
|
||||
__u64 sigmask;
|
||||
@@ -220,29 +206,3 @@ struct swift_io_uring_getevents_arg {
|
||||
__u32 min_wait_usec;
|
||||
__u64 ts;
|
||||
};
|
||||
|
||||
static inline int io_uring_register(int fd, unsigned int opcode, void *arg,
|
||||
unsigned int nr_args)
|
||||
{
|
||||
return syscall(__NR_io_uring_register, fd, opcode, arg, nr_args);
|
||||
}
|
||||
|
||||
static inline int io_uring_setup(unsigned int entries, struct io_uring_params *p)
|
||||
{
|
||||
return syscall(__NR_io_uring_setup, entries, p);
|
||||
}
|
||||
|
||||
static inline int io_uring_enter2(int fd, unsigned int to_submit, unsigned int min_complete,
|
||||
unsigned int flags, void *args, size_t sz)
|
||||
{
|
||||
return syscall(__NR_io_uring_enter, fd, to_submit, min_complete,
|
||||
flags, args, sz);
|
||||
}
|
||||
|
||||
static inline int io_uring_enter(int fd, unsigned int to_submit, unsigned int min_complete,
|
||||
unsigned int flags, sigset_t *sig)
|
||||
{
|
||||
return io_uring_enter2(fd, to_submit, min_complete, flags, sig, _NSIG / 8);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,9 +1,17 @@
|
||||
// System imports CSystem with `internal import`. Don't include a header that
|
||||
// the platform's C module also includes: CSystem would then co-own its
|
||||
// declarations, so Swift would limit them to CSystem's import access level and
|
||||
// could credit their members to CSystem under MemberImportVisibility.
|
||||
// (CSystemWASI.h still does; see Constants.swift.)
|
||||
module CSystem {
|
||||
header "CSystemShared.h"
|
||||
header "CSystemDarwin.h"
|
||||
header "CSystemLinux.h"
|
||||
header "CSystemWASI.h"
|
||||
header "CSystemWindows.h"
|
||||
header "CSystemFreeBSD.h"
|
||||
export *
|
||||
}
|
||||
|
||||
// IORing's inlinable code stores these types, so System imports this module
|
||||
// with `public import`. Keep libc headers out of it too.
|
||||
module CSystemIOUring {
|
||||
header "io_uring.h"
|
||||
}
|
||||
+50
-21
@@ -14,37 +14,30 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if defined(__FreeBSD__)
|
||||
#include <CSystemFreeBSD.h>
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
#define _GNU_SOURCE
|
||||
#include <signal.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#include <CSystemLinux.h>
|
||||
|
||||
uint64_t _system_get_ST_RDONLY(void) { return ST_RDONLY; }
|
||||
uint64_t _system_get_ST_NOSUID(void) { return ST_NOSUID; }
|
||||
uint64_t _system_get_ST_NODEV(void) { return ST_NODEV; }
|
||||
uint64_t _system_get_ST_NOEXEC(void) { return ST_NOEXEC; }
|
||||
uint64_t _system_get_ST_SYNCHRONOUS(void) { return ST_SYNCHRONOUS; }
|
||||
uint64_t _system_get_ST_MANDLOCK(void) { return ST_MANDLOCK; }
|
||||
uint64_t _system_get_ST_NOATIME(void) { return ST_NOATIME; }
|
||||
uint64_t _system_get_ST_NODIRATIME(void) { return ST_NODIRATIME; }
|
||||
uint64_t _system_get_ST_RELATIME(void) { return ST_RELATIME; }
|
||||
unsigned long long _system_get_ST_RDONLY(void) { return ST_RDONLY; }
|
||||
unsigned long long _system_get_ST_NOSUID(void) { return ST_NOSUID; }
|
||||
unsigned long long _system_get_ST_NODEV(void) { return ST_NODEV; }
|
||||
unsigned long long _system_get_ST_NOEXEC(void) { return ST_NOEXEC; }
|
||||
unsigned long long _system_get_ST_SYNCHRONOUS(void) { return ST_SYNCHRONOUS; }
|
||||
unsigned long long _system_get_ST_MANDLOCK(void) { return ST_MANDLOCK; }
|
||||
unsigned long long _system_get_ST_NOATIME(void) { return ST_NOATIME; }
|
||||
unsigned long long _system_get_ST_NODIRATIME(void) { return ST_NODIRATIME; }
|
||||
unsigned long long _system_get_ST_RELATIME(void) { return ST_RELATIME; }
|
||||
|
||||
// ST_NOSYMFOLLOW wasn't defined until glibc 2.34. 0x2000 is the bit the
|
||||
// kernel's internal <linux/statfs.h> and newer glibc both use.
|
||||
#ifndef ST_NOSYMFOLLOW
|
||||
#define ST_NOSYMFOLLOW 0x2000
|
||||
#endif
|
||||
uint64_t _system_get_ST_NOSYMFOLLOW(void) { return ST_NOSYMFOLLOW; }
|
||||
|
||||
#include <CSystemLinux.h>
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <CSystemWindows.h>
|
||||
unsigned long long _system_get_ST_NOSYMFOLLOW(void) { return ST_NOSYMFOLLOW; }
|
||||
#endif
|
||||
|
||||
#include <errno.h>
|
||||
@@ -90,3 +83,39 @@ extern int csystem_posix_dup3(int fildes, int fildes2, int flag) {
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(__linux__) && !defined(__ANDROID__)
|
||||
# ifndef __NR_io_uring_setup
|
||||
# define __NR_io_uring_setup 425
|
||||
# endif
|
||||
# ifndef __NR_io_uring_enter
|
||||
# define __NR_io_uring_enter 426
|
||||
# endif
|
||||
# ifndef __NR_io_uring_register
|
||||
# define __NR_io_uring_register 427
|
||||
# endif
|
||||
|
||||
extern int csystem_io_uring_setup(unsigned int entries,
|
||||
struct io_uring_params *p) {
|
||||
return syscall(__NR_io_uring_setup, entries, p);
|
||||
}
|
||||
|
||||
extern int csystem_io_uring_enter2(int fd, unsigned int to_submit,
|
||||
unsigned int min_complete,
|
||||
unsigned int flags, void *args, size_t sz) {
|
||||
return syscall(__NR_io_uring_enter, fd, to_submit, min_complete, flags,
|
||||
args, sz);
|
||||
}
|
||||
|
||||
extern int csystem_io_uring_enter(int fd, unsigned int to_submit,
|
||||
unsigned int min_complete,
|
||||
unsigned int flags, void *sig) {
|
||||
return csystem_io_uring_enter2(fd, to_submit, min_complete, flags, sig,
|
||||
_NSIG / 8);
|
||||
}
|
||||
|
||||
extern int csystem_io_uring_register(int fd, unsigned int opcode, void *arg,
|
||||
unsigned int nr_args) {
|
||||
return syscall(__NR_io_uring_register, fd, opcode, arg, nr_args);
|
||||
}
|
||||
#endif // defined(__linux__) && !defined(__ANDROID__)
|
||||
@@ -9,7 +9,6 @@
|
||||
|
||||
#if !os(Windows)
|
||||
|
||||
import CSystem
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
@@ -17,6 +16,7 @@ import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import Musl
|
||||
#elseif canImport(WASILibc)
|
||||
internal import CSystem // For the members of system_dirent
|
||||
import WASILibc
|
||||
#elseif canImport(Android)
|
||||
import Android
|
||||
|
||||
@@ -15,15 +15,12 @@
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
import CSystem
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import CSystem
|
||||
import Musl
|
||||
#elseif canImport(WASILibc)
|
||||
import WASILibc
|
||||
#elseif canImport(Android)
|
||||
import CSystem
|
||||
import Android
|
||||
#else
|
||||
#error("Unsupported Platform")
|
||||
|
||||
@@ -79,16 +79,13 @@ public struct StatFS {
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
import CSystem
|
||||
#if os(Linux)
|
||||
import CSystemStatFS
|
||||
#endif
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import CSystem
|
||||
import Musl
|
||||
#elseif canImport(Android)
|
||||
import CSystem
|
||||
import Android
|
||||
#else
|
||||
#error("Unsupported Platform")
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#if compiler(>=6.2) && $Lifetimes
|
||||
#if os(Linux)
|
||||
|
||||
import CSystem
|
||||
public import CSystemIOUring
|
||||
|
||||
public extension IORing {
|
||||
struct Completion: ~Copyable {
|
||||
@@ -60,15 +60,21 @@ public extension IORing.Completion {
|
||||
/// for a read or a write, an event mask for a poll, and so on.
|
||||
///
|
||||
/// A negative value is an `errno` code multiplied by -1. Recover the error
|
||||
/// by negating it again: `Errno(rawValue: -completion.result)`. Only
|
||||
/// ``IORing/tryConsumeCompletion()`` returns completions with a negative
|
||||
/// result; the blocking methods report the error as an ``Errno`` instead.
|
||||
/// by negating it again: `Errno(rawValue: -completion.result)`, or use
|
||||
/// ``error``.
|
||||
@inlinable var result: Int32 {
|
||||
get {
|
||||
rawValue.res
|
||||
}
|
||||
}
|
||||
|
||||
/// The error of the completed operation, or `nil` if the operation succeeded.
|
||||
@inlinable var error: Errno? {
|
||||
get {
|
||||
rawValue.res < 0 ? Errno(rawValue: -rawValue.res) : nil
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable var flags: IORing.Completion.Flags {
|
||||
get {
|
||||
Flags(rawValue: rawValue.flags & 0x0000FFFF)
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#if compiler(>=6.2) && $Lifetimes
|
||||
#if os(Linux)
|
||||
|
||||
import CSystem
|
||||
public import CSystemIOUring
|
||||
|
||||
@usableFromInline
|
||||
internal enum IORequestCore {
|
||||
@@ -274,13 +274,17 @@ extension IORing.Request {
|
||||
/// continue
|
||||
/// }
|
||||
/// armed = completion.flags.contains(.moreCompletions)
|
||||
/// if let error = completion.error {
|
||||
/// // The poll ended with an error, such as `.canceled`.
|
||||
/// throw error
|
||||
/// }
|
||||
/// // Handle incoming connection
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// If the poll ends with an error, such as ``Errno/canceled``,
|
||||
/// ``IORing/blockingConsumeCompletion(timeout:)`` throws that error
|
||||
/// instead of returning the completion.
|
||||
/// If the poll ends with an error, such as ``Errno/canceled``, its last
|
||||
/// completion carries that error in ``IORing/Completion/error`` and
|
||||
/// doesn't contain `.moreCompletions`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - file: The file descriptor to monitor for I/O events.
|
||||
|
||||
@@ -10,14 +10,15 @@
|
||||
#if compiler(>=6.2) && $Lifetimes
|
||||
#if os(Linux)
|
||||
|
||||
import CSystem
|
||||
internal import CSystem
|
||||
public import CSystemIOUring
|
||||
#if canImport(Glibc)
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import Musl
|
||||
#endif
|
||||
|
||||
/// Throwing wrapper around the `io_uring_enter2` shim.
|
||||
/// Throwing wrapper around the `csystem_io_uring_enter2` shim.
|
||||
///
|
||||
/// On success returns the syscall's non-negative result (the number of SQEs
|
||||
/// consumed by the kernel). On failure, reads `errno` and throws the matching
|
||||
@@ -32,14 +33,14 @@ internal func _ioUringEnter2(
|
||||
argsSize: Int
|
||||
) throws(Errno) -> Int32 {
|
||||
let result = valueOrErrno(retryOnInterrupt: true) {
|
||||
io_uring_enter2(
|
||||
csystem_io_uring_enter2(
|
||||
ringDescriptor, toSubmit, minComplete, flags, args, argsSize
|
||||
)
|
||||
}
|
||||
return try result.get()
|
||||
}
|
||||
|
||||
/// Throwing wrapper around the `io_uring_enter` shim.
|
||||
/// Throwing wrapper around the `csystem_io_uring_enter` shim.
|
||||
///
|
||||
/// On success returns the syscall's non-negative result (the number of SQEs
|
||||
/// consumed by the kernel). On failure, reads `errno` and throws the matching
|
||||
@@ -53,12 +54,12 @@ internal func _ioUringEnter(
|
||||
sig: UnsafeMutablePointer<sigset_t>?
|
||||
) throws(Errno) -> Int32 {
|
||||
let result = valueOrErrno(retryOnInterrupt: true) {
|
||||
io_uring_enter(ringDescriptor, toSubmit, minComplete, flags, sig)
|
||||
csystem_io_uring_enter(ringDescriptor, toSubmit, minComplete, flags, sig)
|
||||
}
|
||||
return try result.get()
|
||||
}
|
||||
|
||||
/// Throwing wrapper around the `io_uring_register` shim.
|
||||
/// Throwing wrapper around the `csystem_io_uring_register` shim.
|
||||
///
|
||||
/// On success returns the syscall's non-negative result. On failure, reads
|
||||
/// `errno` and throws the matching `Errno`. `EINTR` is retried automatically.
|
||||
@@ -70,7 +71,7 @@ internal func _ioUringRegister(
|
||||
nrArgs: UInt32
|
||||
) throws(Errno) -> Int32 {
|
||||
let result = valueOrErrno(retryOnInterrupt: true) {
|
||||
io_uring_register(ringDescriptor, opcode, arg, nrArgs)
|
||||
csystem_io_uring_register(ringDescriptor, opcode, arg, nrArgs)
|
||||
}
|
||||
return try result.get()
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
#if compiler(>=6.2) && $Lifetimes
|
||||
#if os(Linux)
|
||||
|
||||
import CSystem
|
||||
internal import CSystem
|
||||
public import CSystemIOUring
|
||||
// needed for mmap
|
||||
#if canImport(Glibc)
|
||||
import Glibc
|
||||
@@ -232,7 +233,7 @@ private func setUpRing(
|
||||
|
||||
var err: Errno? = nil
|
||||
let ringDescriptor = withUnsafeMutablePointer(to: ¶ms) {
|
||||
let result = io_uring_setup(queueDepth, $0)
|
||||
let result = csystem_io_uring_setup(queueDepth, $0)
|
||||
if result < 0 {
|
||||
err = Errno.current
|
||||
}
|
||||
@@ -519,11 +520,10 @@ public struct IORing: ~Copyable {
|
||||
var count = 0
|
||||
while let completion = _tryConsumeCompletion(ring: completionRing) {
|
||||
count += 1
|
||||
if completion.result < 0 {
|
||||
try consumer(nil, Errno(rawValue: -completion.result), false)
|
||||
} else {
|
||||
try consumer(completion, nil, false)
|
||||
}
|
||||
// We still return the completion for a failed operation so that the
|
||||
// caller can tell which request failed through its `context`.
|
||||
let error = completion.error
|
||||
try consumer(completion, error, false)
|
||||
if count == maximumCount {
|
||||
try consumer(nil, nil, true)
|
||||
return
|
||||
@@ -575,17 +575,14 @@ public struct IORing: ~Copyable {
|
||||
var count = 0
|
||||
while let completion = _tryConsumeCompletion(ring: completionRing) {
|
||||
count += 1
|
||||
if completion.result < 0 {
|
||||
try consumer(nil, Errno(rawValue: -completion.result), false)
|
||||
} else {
|
||||
try consumer(completion, nil, false)
|
||||
}
|
||||
let error = completion.error
|
||||
try consumer(completion, error, false)
|
||||
if count == maximumCount {
|
||||
break
|
||||
}
|
||||
}
|
||||
try consumer(nil, nil, true)
|
||||
}
|
||||
try consumer(nil, nil, true)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -595,17 +592,23 @@ public struct IORing: ~Copyable {
|
||||
var result: Completion? = nil
|
||||
try _blockingConsumeCompletionGuts(minimumCount: 1, maximumCount: 1, extraArgs: extraArgs) {
|
||||
(completion: consuming Completion?, error, done) throws(Errno) in
|
||||
if let error {
|
||||
throw error
|
||||
}
|
||||
if let completion {
|
||||
result = consume completion
|
||||
} else if let error {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
return result.take()!
|
||||
}
|
||||
|
||||
/// Synchronously waits for an operation to complete for up to `timeout` (or forever if not specified)
|
||||
///
|
||||
/// The completion is returned even if its operation failed. Check
|
||||
/// ``IORing/Completion/error`` to find out whether it did, and
|
||||
/// ``IORing/Completion/context`` to find out which request it belongs to.
|
||||
///
|
||||
/// - Throws: An error of the ring itself, such as ``Errno/timeout`` when
|
||||
/// no operation completed within `timeout`.
|
||||
@inlinable
|
||||
public func blockingConsumeCompletion(
|
||||
timeout: Duration? = nil
|
||||
@@ -629,7 +632,17 @@ public struct IORing: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Synchronously waits for `minimumCount` or more operations to complete for up to `timeout` (or forever if not specified). For each completed operation found, `consumer` is called to handle processing it
|
||||
/// Synchronously waits for `minimumCount` or more operations to complete for up to `timeout`
|
||||
/// (or forever if not specified). For each completed operation found, `consumer` is called to handle
|
||||
/// processing it.
|
||||
///
|
||||
/// `consumer` receives a completion, an error, and whether consuming is done:
|
||||
/// - For each completed operation, it receives the completion. If the
|
||||
/// operation failed, it receives the operation's error alongside the
|
||||
/// completion.
|
||||
/// - If the ring itself fails, such as with ``Errno/timeout``, it receives
|
||||
/// the error without a completion.
|
||||
/// - Once consuming is done, it is called a final time with `done` set to `true`.
|
||||
@inlinable
|
||||
public func blockingConsumeCompletions<Err: Error>(
|
||||
minimumCount: UInt32 = 1,
|
||||
@@ -658,10 +671,6 @@ public struct IORing: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
// public func peekNextCompletion() -> IOCompletion {
|
||||
|
||||
// }
|
||||
|
||||
/// Takes a completed operation from the ring and returns it, if one is ready. Otherwise, returns nil.
|
||||
@inlinable
|
||||
public func tryConsumeCompletion() -> Completion? {
|
||||
@@ -810,6 +819,14 @@ public struct IORing: ~Copyable {
|
||||
}
|
||||
|
||||
/// Sends all prepared requests to the kernel for processing, and then dequeues at least `minimumCount` completions, waiting up to `timeout` for them to become available. `consumer` is called to process each completed IO operation as it becomes available.
|
||||
///
|
||||
/// `consumer` receives a completion, an error, and whether consuming is done:
|
||||
/// - For each completed operation, it receives the completion. If the
|
||||
/// operation failed, it receives the operation's error alongside the
|
||||
/// completion.
|
||||
/// - If submitting the requests or waiting for completions fails, such as
|
||||
/// with ``Errno/timeout``, it receives the error without a completion.
|
||||
/// - Once consuming is done, it is called a final time with `done` set to `true`.
|
||||
@inlinable
|
||||
public func submitPreparedRequestsAndConsumeCompletions<Err: Error>(
|
||||
minimumCount: UInt32 = 1,
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#if compiler(>=6.2) && $Lifetimes
|
||||
#if os(Linux)
|
||||
|
||||
import CSystem
|
||||
public import CSystemIOUring
|
||||
#if canImport(Glibc)
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
|
||||
@@ -10,10 +10,8 @@
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif os(Windows)
|
||||
import CSystem
|
||||
import ucrt
|
||||
#elseif canImport(Glibc)
|
||||
import CSystem
|
||||
#if os(Linux)
|
||||
// SwiftGlibc doesn't export `struct statfs`, so re-export it for clients who
|
||||
// need to use the fields of a `CInterop.StatFS`.
|
||||
@@ -21,13 +19,11 @@ import CSystem
|
||||
#endif
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import CSystem
|
||||
import Musl
|
||||
#elseif canImport(WASILibc)
|
||||
import WASILibc
|
||||
#elseif canImport(Bionic)
|
||||
import CSystem
|
||||
import Bionic
|
||||
#elseif canImport(Android)
|
||||
import Android
|
||||
#else
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
@@ -11,11 +11,11 @@
|
||||
// they can be used anywhere without imports and without confusion to
|
||||
// unavailable local decls.
|
||||
|
||||
import CSystem
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif os(Windows)
|
||||
import ucrt
|
||||
import WinSDK
|
||||
#elseif canImport(Glibc)
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
@@ -28,6 +28,11 @@ import Android
|
||||
#error("Unsupported Platform")
|
||||
#endif
|
||||
|
||||
// Some WASI constants come from CSystem and are inlined into clients.
|
||||
#if os(WASI)
|
||||
public import CSystem
|
||||
#endif
|
||||
|
||||
// MARK: errno
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -629,7 +634,7 @@ internal var _O_CLOEXEC: CInt {
|
||||
@_alwaysEmitIntoClient
|
||||
internal var _O_CLOFORK: CInt {
|
||||
#if SYSTEM_PACKAGE_DARWIN || os(FreeBSD)
|
||||
COMPATIBILITY_O_CLOFORK
|
||||
0x0800_0000 // O_CLOFORK, which SDKs before macOS 27 and older FreeBSD lack
|
||||
#elseif !os(WASI) && !os(Linux) && !os(Android)
|
||||
O_CLOFORK
|
||||
#else
|
||||
|
||||
@@ -10,26 +10,21 @@
|
||||
// Internal wrappers and typedefs which help reduce #if littering in System's
|
||||
// code base.
|
||||
|
||||
// TODO: Should CSystem just include all the header files we need?
|
||||
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif os(Windows)
|
||||
import CSystem
|
||||
import ucrt
|
||||
import WinSDK
|
||||
#elseif canImport(Glibc)
|
||||
import CSystem
|
||||
#if os(Linux)
|
||||
import CSystemStatFS
|
||||
#endif
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import CSystem
|
||||
import Musl
|
||||
#elseif canImport(WASILibc)
|
||||
import WASILibc
|
||||
#elseif canImport(Android)
|
||||
import CSystem
|
||||
import Android
|
||||
#else
|
||||
#error("Unsupported Platform")
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
import CSystem
|
||||
internal import CSystem
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
|
||||
@@ -16,8 +16,9 @@
|
||||
// - Access raw C members of vended types (e.g. CInterop.Stat) as a downstream
|
||||
// consumer would. Don't go through the Swift (e.g. Stat) wrapper since its
|
||||
// accesses happen inside System, where importing CSystem is fine.
|
||||
// - Touch every direct member the public API exposes. One field is not always
|
||||
// a safe proxy for the rest.
|
||||
// - Touch every direct member the public API exposes, and the members of C
|
||||
// types it exposes through them (e.g. timespec). One field is not always a
|
||||
// safe proxy for the rest.
|
||||
|
||||
#if canImport(Glibc) || canImport(Musl) || canImport(Android)
|
||||
#if canImport(Glibc)
|
||||
@@ -43,6 +44,8 @@ func _mivProbe(_ s: CInterop.Stat) {
|
||||
_ = s.st_atim
|
||||
_ = s.st_mtim
|
||||
_ = s.st_ctim
|
||||
_ = s.st_atim.tv_sec
|
||||
_ = s.st_atim.tv_nsec
|
||||
}
|
||||
|
||||
// These are the Linux and Android field names. FreeBSD's libc module is also
|
||||
|
||||
@@ -201,6 +201,22 @@ final class FileOperationsTest: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if canImport(Darwin, _version: 428) // The macOS 27 SDK defines O_CLOFORK.
|
||||
func testAdHocPipeCloseOnFork() throws {
|
||||
guard #available(macOS 27.0, iOS 27.0, watchOS 27.0, tvOS 27.0, visionOS 27.0, *) else {
|
||||
throw XCTSkip("pipe2 and PipeOptions require anyAppleOS 27 or newer")
|
||||
}
|
||||
// System spells O_CLOFORK as a literal on Darwin.
|
||||
XCTAssertEqual(FileDescriptor.PipeOptions.closeOnFork.rawValue, O_CLOFORK)
|
||||
let pipe = try FileDescriptor.pipe(options: .closeOnFork)
|
||||
try pipe.readEnd.closeAfter {
|
||||
try pipe.writeEnd.closeAfter {
|
||||
XCTAssertEqual(fcntl(pipe.readEnd.rawValue, F_GETFD) & FD_CLOFORK, FD_CLOFORK)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#endif // !os(WASI)
|
||||
|
||||
#if !os(Windows)
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
#if os(Linux)
|
||||
|
||||
import XCTest
|
||||
import CSystem //for eventfd
|
||||
import CSystem // for eventfd and csystem_io_uring_setup
|
||||
import CSystemIOUring
|
||||
#if canImport(Glibc)
|
||||
import Glibc // for errno
|
||||
#elseif canImport(Musl)
|
||||
@@ -30,7 +31,7 @@ let failureMessage = "Runtime environment does not support IORing."
|
||||
func isUringEnabled() throws -> Bool {
|
||||
// Even if the kernel supports io_uring, the SystemPackage build may have
|
||||
// been compiled against older kernel headers that lack features it needs
|
||||
// (gated on IORING_TIMEOUT_BOOTTIME in CSystem); in that configuration
|
||||
// (gated on IORING_TIMEOUT_BOOTTIME in io_uring.h); in that configuration
|
||||
// IORing.init throws ENOTSUP. Treat that as disabled so tests skip cleanly
|
||||
// instead of failing.
|
||||
do throws(Errno) {
|
||||
@@ -61,7 +62,7 @@ func isUringEnabled() throws -> Bool {
|
||||
// - EPERM or EACCES -> treat as disabled
|
||||
// - propagate any other error.
|
||||
var params = io_uring_params()
|
||||
let raw = io_uring_setup(1, ¶ms)
|
||||
let raw = csystem_io_uring_setup(1, ¶ms)
|
||||
if raw < 0 {
|
||||
let err = Errno(rawValue: errno)
|
||||
switch err {
|
||||
@@ -286,6 +287,81 @@ final class IORingTests: XCTestCase {
|
||||
XCTAssertGreaterThanOrEqual(elapsed, .milliseconds(50))
|
||||
}
|
||||
|
||||
func testBlockingConsumeCompletionsSendsDone() throws {
|
||||
try XCTSkipIf(!uringEnabled, failureMessage)
|
||||
var ring = try IORing(queueDepth: 4)
|
||||
XCTAssertTrue(try ring.submit(linkedRequests: .nop()))
|
||||
var dones: [Bool] = []
|
||||
ring.blockingConsumeCompletions { (completion: consuming IORing.Completion?, error, done) in
|
||||
dones.append(done)
|
||||
}
|
||||
XCTAssertEqual(dones, [false, true])
|
||||
}
|
||||
|
||||
func testBlockingConsumeCompletionsKeepsContextOfFailedCompletion() throws {
|
||||
try XCTSkipIf(!uringEnabled, failureMessage)
|
||||
var ring = try IORing(queueDepth: 4)
|
||||
XCTAssertTrue(try ring.submit(
|
||||
linkedRequests: .close(FileDescriptor(rawValue: -1), context: 42)
|
||||
))
|
||||
|
||||
var received: [(context: UInt64?, error: Errno?, done: Bool)] = []
|
||||
ring.blockingConsumeCompletions { (completion: consuming IORing.Completion?, error, done) in
|
||||
received.append((completion?.context, error, done))
|
||||
}
|
||||
|
||||
XCTAssertEqual(received.count, 2)
|
||||
XCTAssertEqual(received.first?.context, 42)
|
||||
XCTAssertEqual(received.first?.error, .badFileDescriptor)
|
||||
XCTAssertEqual(received.first?.done, false)
|
||||
XCTAssertEqual(received.last?.context, nil)
|
||||
XCTAssertEqual(received.last?.error, nil)
|
||||
XCTAssertEqual(received.last?.done, true)
|
||||
}
|
||||
|
||||
func testBlockingConsumeCompletionReturnsFailedCompletion() throws {
|
||||
try XCTSkipIf(!uringEnabled, failureMessage)
|
||||
var ring = try IORing(queueDepth: 4)
|
||||
XCTAssertTrue(try ring.submit(
|
||||
linkedRequests: .close(FileDescriptor(rawValue: -1), context: 43)
|
||||
))
|
||||
|
||||
let completion = try ring.blockingConsumeCompletion()
|
||||
XCTAssertEqual(completion.context, 43)
|
||||
XCTAssertEqual(completion.error, .badFileDescriptor)
|
||||
XCTAssertEqual(completion.result, -Errno.badFileDescriptor.rawValue)
|
||||
}
|
||||
|
||||
func testSubmitAndConsumeCompletionsKeepsContextOfFailedCompletion() throws {
|
||||
try XCTSkipIf(!uringEnabled, failureMessage)
|
||||
var ring = try IORing(queueDepth: 4)
|
||||
XCTAssertTrue(ring.prepare(request: .close(FileDescriptor(rawValue: -1), context: 44)))
|
||||
|
||||
var received: [(context: UInt64?, error: Errno?, done: Bool)] = []
|
||||
ring.submitPreparedRequestsAndConsumeCompletions {
|
||||
(completion: consuming IORing.Completion?, error, done) in
|
||||
received.append((completion?.context, error, done))
|
||||
}
|
||||
|
||||
XCTAssertEqual(received.count, 2)
|
||||
XCTAssertEqual(received.first?.context, 44)
|
||||
XCTAssertEqual(received.first?.error, .badFileDescriptor)
|
||||
XCTAssertEqual(received.first?.done, false)
|
||||
XCTAssertEqual(received.last?.context, nil)
|
||||
XCTAssertEqual(received.last?.error, nil)
|
||||
XCTAssertEqual(received.last?.done, true)
|
||||
}
|
||||
|
||||
func testCompletionOfSuccessfulOperationHasNoError() throws {
|
||||
try XCTSkipIf(!uringEnabled, failureMessage)
|
||||
var ring = try IORing(queueDepth: 4)
|
||||
XCTAssertTrue(try ring.submit(linkedRequests: .nop()))
|
||||
|
||||
let completion = try ring.blockingConsumeCompletion()
|
||||
XCTAssertEqual(completion.result, 0)
|
||||
XCTAssertNil(completion.error)
|
||||
}
|
||||
|
||||
func testRegisterEventFDTwiceThrows() throws {
|
||||
try XCTSkipIf(!uringEnabled, failureMessage)
|
||||
var ring = try IORing(queueDepth: 4)
|
||||
|
||||
@@ -16,10 +16,8 @@ import Testing
|
||||
#if SYSTEM_PACKAGE_DARWIN
|
||||
import Darwin
|
||||
#elseif canImport(Glibc)
|
||||
import CSystem
|
||||
import Glibc
|
||||
#elseif canImport(Musl)
|
||||
import CSystem
|
||||
import Musl
|
||||
#elseif canImport(WASILibc)
|
||||
import CSystem
|
||||
|
||||
Reference in new issue
Block a user