Skip to content

Commit

Permalink
Async apis for NonBlockingFileIO (#2576)
Browse files Browse the repository at this point in the history
* Async versions of NonBlockingFileIO functions

I have not produced async versions of the readChunked functions. These
would likely be replaced with an AsyncSequence though.

* Add tests for async versions of NonBlockingFileIO

* Add wrapper for FileRegion to avoid Sendable issues

* Allocate ByteBuffer in readSync

* Update read_10000_chunks_from_file alloc count

* Add UnsafeTransfer to NIOPosix.

Use in NonBlockingFileIO.openFile functions

* Update Sources/NIOPosix/NonBlockingFileIO.swift

Co-authored-by: George Barnett <[email protected]>

* formatting, docc

* update comment

* replace openFile with withOpenFile

* More indentation

* Update comments

* Remove docc links to NIOCore types

* Fix read docc links

* Catch errors when closing file handle in withOpenFile

* rename to withFileHandle

* Fix double close on error

Also
- move @available above each function
- Remove wording about separate from Task pool

* Update Sources/NIOPosix/NonBlockingFileIO.swift

Co-authored-by: George Barnett <[email protected]>

* withOpenFile is now withFileRegion

---------

Co-authored-by: George Barnett <[email protected]>
  • Loading branch information
adam-fowler and glbrntt authored Nov 16, 2023
1 parent 702cd7c commit e69354f
Show file tree
Hide file tree
Showing 10 changed files with 1,166 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Sources/NIOPosix/NIOThreadPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,4 @@ extension NIOThreadPool {
}
}
}
}
}
536 changes: 464 additions & 72 deletions Sources/NIOPosix/NonBlockingFileIO.swift

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions Sources/NIOPosix/UnsafeTransfer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===https://
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===https://

/// ``UnsafeTransfer`` can be used to make non-`Sendable` values `Sendable`.
/// As the name implies, the usage of this is unsafe because it disables the sendable checking of the compiler.
/// It can be used similar to `@unsafe Sendable` but for values instead of types.
@usableFromInline
struct UnsafeTransfer<Wrapped> {
@usableFromInline
var wrappedValue: Wrapped

@inlinable
init(_ wrappedValue: Wrapped) {
self.wrappedValue = wrappedValue
}
}

extension UnsafeTransfer: @unchecked Sendable {}

extension UnsafeTransfer: Equatable where Wrapped: Equatable {}
extension UnsafeTransfer: Hashable where Wrapped: Hashable {}

Loading

0 comments on commit e69354f

Please sign in to comment.