mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-28 04:54:20 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c8bd186f4 | ||
|
|
2503842d68 | ||
|
|
e83857ca55 | ||
|
|
6ae9a051f7 | ||
|
|
46b4464735 | ||
|
|
d162617838 | ||
|
|
14729ea7f9 |
No files matched your search
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
Similar to the `zip` algorithm there is a need to combine the latest values from multiple input asynchronous sequences. Since `AsyncSequence` augments the concept of sequence with the characteristic of time it means that the composition of elements may not just be pairwise emissions but instead be temporal composition. This means that it is useful to emit a new tuple _when_ a value is produced. The `combineLatest` algorithm provides precicely that.
|
Similar to the `zip` algorithm there is a need to combine the latest values from multiple input asynchronous sequences. Since `AsyncSequence` augments the concept of sequence with the characteristic of time it means that the composition of elements may not just be pairwise emissions but instead be temporal composition. This means that it is useful to emit a new tuple _when_ a value is produced. The `combineLatest` algorithm provides precisely that.
|
||||||
|
|
||||||
## Detailed Design
|
## Detailed Design
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ By applying the buffer operator to the previous example, the file can be read as
|
|||||||
|
|
||||||
## Proposed Solution
|
## Proposed Solution
|
||||||
|
|
||||||
We propose to extend `AsyncSequence` with a `buffer()` operator. This operator will return an `AsyncBuffereSequence` that wraps the source `AsyncSequence` and handle the buffering mechanism.
|
We propose to extend `AsyncSequence` with a `buffer()` operator. This operator will return an `AsyncBufferSequence` that wraps the source `AsyncSequence` and handle the buffering mechanism.
|
||||||
|
|
||||||
This operator will accept an `AsyncBufferSequencePolicy`. The policy will dictate the behaviour in case of a buffer overflow.
|
This operator will accept an `AsyncBufferSequencePolicy`. The policy will dictate the behaviour in case of a buffer overflow.
|
||||||
|
|
||||||
@@ -43,7 +43,7 @@ public struct AsyncBufferSequencePolicy: Sendable {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
And the public API of `AsyncBuffereSequence` will be:
|
And the public API of `AsyncBufferSequence` will be:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
extension AsyncSequence where Self: Sendable {
|
extension AsyncSequence where Self: Sendable {
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: Element) {
|
internal init(_ base: Base, every: Int, separator: Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .element(separator)
|
self.separator = .element(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
@@ -186,7 +186,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () -> Element) {
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () -> Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .syncClosure(separator)
|
self.separator = .syncClosure(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
@@ -194,7 +194,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async -> Element) {
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async -> Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .asyncClosure(separator)
|
self.separator = .asyncClosure(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
## Introduction
|
## Introduction
|
||||||
|
|
||||||
When events can potentially happen faster than the desired consumption rate, there are multiple ways to handle the situation. One approach is to only emit values after a given period of time of inactivity, or "quiescence", has elapsed. This algorithm is commonly referred to as debouncing. A very close reelativee is an apporach to emit values after a given period has elapsed. These emitted values can be reduced from the values encountered during the waiting period. This algorithm is commonly referred to as throttling.
|
When events can potentially happen faster than the desired consumption rate, there are multiple ways to handle the situation. One approach is to only emit values after a given period of time of inactivity, or "quiescence", has elapsed. This algorithm is commonly referred to as debouncing. A very close relative is an approach to emit values after a given period has elapsed. These emitted values can be reduced from the values encountered during the waiting period. This algorithm is commonly referred to as throttling.
|
||||||
|
|
||||||
## Proposed Solution
|
## Proposed Solution
|
||||||
|
|
||||||
|
|||||||
+15
-5
@@ -13,14 +13,13 @@ let package = Package(
|
|||||||
products: [
|
products: [
|
||||||
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"]),
|
.library(name: "AsyncAlgorithms", targets: ["AsyncAlgorithms"]),
|
||||||
],
|
],
|
||||||
dependencies: [
|
|
||||||
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.4"),
|
|
||||||
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
|
||||||
],
|
|
||||||
targets: [
|
targets: [
|
||||||
.target(
|
.target(
|
||||||
name: "AsyncAlgorithms",
|
name: "AsyncAlgorithms",
|
||||||
dependencies: [.product(name: "Collections", package: "swift-collections")],
|
dependencies: [
|
||||||
|
.product(name: "OrderedCollections", package: "swift-collections"),
|
||||||
|
.product(name: "DequeModule", package: "swift-collections"),
|
||||||
|
],
|
||||||
swiftSettings: [
|
swiftSettings: [
|
||||||
.enableExperimentalFeature("StrictConcurrency=complete"),
|
.enableExperimentalFeature("StrictConcurrency=complete"),
|
||||||
]
|
]
|
||||||
@@ -49,3 +48,14 @@ let package = Package(
|
|||||||
),
|
),
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if Context.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil {
|
||||||
|
package.dependencies += [
|
||||||
|
.package(url: "https://github.com/apple/swift-collections.git", from: "1.1.0"),
|
||||||
|
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
|
||||||
|
]
|
||||||
|
} else {
|
||||||
|
package.dependencies += [
|
||||||
|
.package(path: "../swift-collections"),
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -67,7 +67,7 @@ To use the `AsyncAlgorithms` library in a SwiftPM project,
|
|||||||
add the following line to the dependencies in your `Package.swift` file:
|
add the following line to the dependencies in your `Package.swift` file:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
.package(url: "https://github.com/apple/swift-async-algorithms"),
|
.package(url: "https://github.com/apple/swift-async-algorithms", from: "1.0.0"),
|
||||||
```
|
```
|
||||||
|
|
||||||
Include `"AsyncAlgorithms"` as a dependency for your executable target:
|
Include `"AsyncAlgorithms"` as a dependency for your executable target:
|
||||||
|
|||||||
@@ -16,16 +16,19 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
typealias SuspendedProducer = UnsafeContinuation<Void, Never>
|
typealias SuspendedProducer = UnsafeContinuation<Void, Never>
|
||||||
typealias SuspendedConsumer = UnsafeContinuation<Result<Base.Element, Error>?, Never>
|
typealias SuspendedConsumer = UnsafeContinuation<Result<Base.Element, Error>?, Never>
|
||||||
|
|
||||||
|
// We are using UnsafeTransfer here since we have to get the elements from the task
|
||||||
|
// into the consumer task. This is a transfer but we cannot prove this to the compiler at this point
|
||||||
|
// since next is not marked as transferring the return value.
|
||||||
fileprivate enum State {
|
fileprivate enum State {
|
||||||
case initial(base: Base)
|
case initial(base: Base)
|
||||||
case buffering(
|
case buffering(
|
||||||
task: Task<Void, Never>,
|
task: Task<Void, Never>,
|
||||||
buffer: Deque<Result<Element, Error>>,
|
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
|
||||||
suspendedProducer: SuspendedProducer?,
|
suspendedProducer: SuspendedProducer?,
|
||||||
suspendedConsumer: SuspendedConsumer?
|
suspendedConsumer: SuspendedConsumer?
|
||||||
)
|
)
|
||||||
case modifying
|
case modifying
|
||||||
case finished(buffer: Deque<Result<Element, Error>>)
|
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var state: State
|
private var state: State
|
||||||
@@ -139,7 +142,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
// we have to stack the new element or suspend the producer if the buffer is full
|
// we have to stack the new element or suspend the producer if the buffer is full
|
||||||
precondition(buffer.count < limit, "Invalid state. The buffer should be available for stacking a new element.")
|
precondition(buffer.count < limit, "Invalid state. The buffer should be available for stacking a new element.")
|
||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
buffer.append(.success(element))
|
buffer.append(.success(.init(element)))
|
||||||
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
return .none
|
return .none
|
||||||
|
|
||||||
@@ -218,7 +221,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
return .returnResult(producerContinuation: suspendedProducer, result: result)
|
return .returnResult(producerContinuation: suspendedProducer, result: result.map { $0.wrapped })
|
||||||
|
|
||||||
case .buffering(_, _, _, .some):
|
case .buffering(_, _, _, .some):
|
||||||
preconditionFailure("Invalid states. There is already a suspended consumer.")
|
preconditionFailure("Invalid states. There is already a suspended consumer.")
|
||||||
@@ -233,7 +236,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .finished(buffer: buffer)
|
self.state = .finished(buffer: buffer)
|
||||||
return .returnResult(producerContinuation: nil, result: result)
|
return .returnResult(producerContinuation: nil, result: result.map { $0.wrapped })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,7 +260,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
|
||||||
return .returnResult(producerContinuation: suspendedProducer, result: result)
|
return .returnResult(producerContinuation: suspendedProducer, result: result.map { $0.wrapped })
|
||||||
|
|
||||||
case .buffering(_, _, _, .some):
|
case .buffering(_, _, _, .some):
|
||||||
preconditionFailure("Invalid states. There is already a suspended consumer.")
|
preconditionFailure("Invalid states. There is already a suspended consumer.")
|
||||||
@@ -272,7 +275,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .finished(buffer: buffer)
|
self.state = .finished(buffer: buffer)
|
||||||
return .returnResult(producerContinuation: nil, result: result)
|
return .returnResult(producerContinuation: nil, result: result.map { $0.wrapped })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,15 +21,18 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
case bufferingOldest(Int)
|
case bufferingOldest(Int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We are using UnsafeTransfer here since we have to get the elements from the task
|
||||||
|
// into the consumer task. This is a transfer but we cannot prove this to the compiler at this point
|
||||||
|
// since next is not marked as transferring the return value.
|
||||||
fileprivate enum State {
|
fileprivate enum State {
|
||||||
case initial(base: Base)
|
case initial(base: Base)
|
||||||
case buffering(
|
case buffering(
|
||||||
task: Task<Void, Never>,
|
task: Task<Void, Never>,
|
||||||
buffer: Deque<Result<Element, Error>>,
|
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
|
||||||
suspendedConsumer: SuspendedConsumer?
|
suspendedConsumer: SuspendedConsumer?
|
||||||
)
|
)
|
||||||
case modifying
|
case modifying
|
||||||
case finished(buffer: Deque<Result<Element, Error>>)
|
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var state: State
|
private var state: State
|
||||||
@@ -84,15 +87,15 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
switch self.policy {
|
switch self.policy {
|
||||||
case .unlimited:
|
case .unlimited:
|
||||||
buffer.append(.success(element))
|
buffer.append(.success(.init(element)))
|
||||||
case .bufferingNewest(let limit):
|
case .bufferingNewest(let limit):
|
||||||
if buffer.count >= limit {
|
if buffer.count >= limit {
|
||||||
_ = buffer.popFirst()
|
_ = buffer.popFirst()
|
||||||
}
|
}
|
||||||
buffer.append(.success(element))
|
buffer.append(.success(.init(element)))
|
||||||
case .bufferingOldest(let limit):
|
case .bufferingOldest(let limit):
|
||||||
if buffer.count < limit {
|
if buffer.count < limit {
|
||||||
buffer.append(.success(element))
|
buffer.append(.success(.init(element)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
||||||
@@ -170,7 +173,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
||||||
return .returnResult(result)
|
return .returnResult(result.map { $0.wrapped })
|
||||||
|
|
||||||
case .modifying:
|
case .modifying:
|
||||||
preconditionFailure("Invalid state.")
|
preconditionFailure("Invalid state.")
|
||||||
@@ -182,7 +185,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .finished(buffer: buffer)
|
self.state = .finished(buffer: buffer)
|
||||||
return .returnResult(result)
|
return .returnResult(result.map { $0.wrapped })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +211,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
|
||||||
return .resumeConsumer(result)
|
return .resumeConsumer(result.map { $0.wrapped })
|
||||||
|
|
||||||
case .modifying:
|
case .modifying:
|
||||||
preconditionFailure("Invalid state.")
|
preconditionFailure("Invalid state.")
|
||||||
@@ -220,7 +223,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
self.state = .modifying
|
self.state = .modifying
|
||||||
let result = buffer.popFirst()!
|
let result = buffer.popFirst()!
|
||||||
self.state = .finished(buffer: buffer)
|
self.state = .finished(buffer: buffer)
|
||||||
return .resumeConsumer(result)
|
return .resumeConsumer(result.map { $0.wrapped })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -251,3 +254,5 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
|
|||||||
|
|
||||||
extension UnboundedBufferStateMachine: Sendable where Base: Sendable { }
|
extension UnboundedBufferStateMachine: Sendable where Base: Sendable { }
|
||||||
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable { }
|
extension UnboundedBufferStateMachine.State: Sendable where Base: Sendable { }
|
||||||
|
|
||||||
|
|
||||||
@@ -10,9 +10,6 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
import OrderedCollections
|
import OrderedCollections
|
||||||
|
|
||||||
// NOTE: this is only marked as unchecked since the swift-collections tag is before auditing for Sendable
|
|
||||||
extension OrderedSet: @unchecked Sendable where Element: Sendable { }
|
|
||||||
|
|
||||||
struct ChannelStateMachine<Element: Sendable, Failure: Error>: Sendable {
|
struct ChannelStateMachine<Element: Sendable, Failure: Error>: Sendable {
|
||||||
private struct SuspendedProducer: Hashable, Sendable {
|
private struct SuspendedProducer: Hashable, Sendable {
|
||||||
let id: UInt64
|
let id: UInt64
|
||||||
|
|||||||
@@ -157,7 +157,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: Element) {
|
internal init(_ base: Base, every: Int, separator: Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .element(separator)
|
self.separator = .element(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
@@ -165,7 +165,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () -> Element) {
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () -> Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .syncClosure(separator)
|
self.separator = .syncClosure(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
@@ -173,7 +173,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async -> Element) {
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async -> Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .asyncClosure(separator)
|
self.separator = .asyncClosure(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
@@ -310,7 +310,7 @@ public struct AsyncThrowingInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () throws -> Element) {
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () throws -> Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .syncClosure(separator)
|
self.separator = .syncClosure(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
@@ -318,7 +318,7 @@ public struct AsyncThrowingInterspersedSequence<Base: AsyncSequence> {
|
|||||||
|
|
||||||
@usableFromInline
|
@usableFromInline
|
||||||
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async throws -> Element) {
|
internal init(_ base: Base, every: Int, separator: @Sendable @escaping () async throws -> Element) {
|
||||||
precondition(every > 0, "Separators can only be interspersed ever 1+ elements")
|
precondition(every > 0, "Separators can only be interspersed every 1+ elements")
|
||||||
self.base = base
|
self.base = base
|
||||||
self.separator = .asyncClosure(separator)
|
self.separator = .asyncClosure(separator)
|
||||||
self.every = every
|
self.every = every
|
||||||
|
|||||||
@@ -13,19 +13,25 @@
|
|||||||
import Darwin
|
import Darwin
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc)
|
||||||
import Glibc
|
import Glibc
|
||||||
|
#elseif canImport(Musl)
|
||||||
|
import Musl
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
import WinSDK
|
import WinSDK
|
||||||
|
#elseif canImport(Bionic)
|
||||||
|
import Bionic
|
||||||
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
internal struct Lock {
|
internal struct Lock {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
typealias Primitive = os_unfair_lock
|
typealias Primitive = os_unfair_lock
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
typealias Primitive = pthread_mutex_t
|
typealias Primitive = pthread_mutex_t
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
typealias Primitive = SRWLOCK
|
typealias Primitive = SRWLOCK
|
||||||
#else
|
#else
|
||||||
typealias Primitive = Int
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typealias PlatformLock = UnsafeMutablePointer<Primitive>
|
typealias PlatformLock = UnsafeMutablePointer<Primitive>
|
||||||
@@ -38,16 +44,18 @@ internal struct Lock {
|
|||||||
fileprivate static func initialize(_ platformLock: PlatformLock) {
|
fileprivate static func initialize(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
platformLock.initialize(to: os_unfair_lock())
|
platformLock.initialize(to: os_unfair_lock())
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
let result = pthread_mutex_init(platformLock, nil)
|
let result = pthread_mutex_init(platformLock, nil)
|
||||||
precondition(result == 0, "pthread_mutex_init failed")
|
precondition(result == 0, "pthread_mutex_init failed")
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
InitializeSRWLock(platformLock)
|
InitializeSRWLock(platformLock)
|
||||||
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
|
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Glibc)
|
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
let result = pthread_mutex_destroy(platformLock)
|
let result = pthread_mutex_destroy(platformLock)
|
||||||
precondition(result == 0, "pthread_mutex_destroy failed")
|
precondition(result == 0, "pthread_mutex_destroy failed")
|
||||||
#endif
|
#endif
|
||||||
@@ -57,21 +65,25 @@ internal struct Lock {
|
|||||||
fileprivate static func lock(_ platformLock: PlatformLock) {
|
fileprivate static func lock(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
os_unfair_lock_lock(platformLock)
|
os_unfair_lock_lock(platformLock)
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_mutex_lock(platformLock)
|
pthread_mutex_lock(platformLock)
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
AcquireSRWLockExclusive(platformLock)
|
AcquireSRWLockExclusive(platformLock)
|
||||||
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate static func unlock(_ platformLock: PlatformLock) {
|
fileprivate static func unlock(_ platformLock: PlatformLock) {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
os_unfair_lock_unlock(platformLock)
|
os_unfair_lock_unlock(platformLock)
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
let result = pthread_mutex_unlock(platformLock)
|
let result = pthread_mutex_unlock(platformLock)
|
||||||
precondition(result == 0, "pthread_mutex_unlock failed")
|
precondition(result == 0, "pthread_mutex_unlock failed")
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
ReleaseSRWLockExclusive(platformLock)
|
ReleaseSRWLockExclusive(platformLock)
|
||||||
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,19 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This source file is part of the Swift Async Algorithms open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Apple Inc. and the Swift project authors
|
||||||
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||||
|
//
|
||||||
|
// See https://swift.org/LICENSE.txt for license information
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
/// A wrapper struct to unconditionally to transfer an non-Sendable value.
|
||||||
|
struct UnsafeTransfer<Element>: @unchecked Sendable {
|
||||||
|
let wrapped: Element
|
||||||
|
|
||||||
|
init(_ wrapped: Element) {
|
||||||
|
self.wrapped = wrapped
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -15,8 +15,14 @@ import _CAsyncSequenceValidationSupport
|
|||||||
import Darwin
|
import Darwin
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc)
|
||||||
import Glibc
|
import Glibc
|
||||||
|
#elseif canImport(Musl)
|
||||||
|
import Musl
|
||||||
|
#elseif canImport(Bionic)
|
||||||
|
import Bionic
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
|
#else
|
||||||
|
#error("Unsupported platform")
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
@@ -24,11 +30,16 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
|
|||||||
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
#elseif canImport(Glibc)
|
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl)
|
||||||
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
|
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
|
||||||
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
|
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
#elseif os(Android)
|
||||||
|
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
|
||||||
|
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
|
||||||
|
return UnsafeMutableRawPointer(bitPattern: 0xdeadbee)!
|
||||||
|
}
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
#endif
|
#endif
|
||||||
@@ -38,7 +49,7 @@ final class TaskDriver {
|
|||||||
let queue: WorkQueue
|
let queue: WorkQueue
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
var thread: pthread_t?
|
var thread: pthread_t?
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
var thread = pthread_t()
|
var thread = pthread_t()
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
@@ -50,7 +61,7 @@ final class TaskDriver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func start() {
|
func start() {
|
||||||
#if canImport(Darwin) || canImport(Glibc)
|
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_create(&thread, nil, start_thread,
|
pthread_create(&thread, nil, start_thread,
|
||||||
Unmanaged.passRetained(self).toOpaque())
|
Unmanaged.passRetained(self).toOpaque())
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
@@ -68,7 +79,7 @@ final class TaskDriver {
|
|||||||
func join() {
|
func join() {
|
||||||
#if canImport(Darwin)
|
#if canImport(Darwin)
|
||||||
pthread_join(thread!, nil)
|
pthread_join(thread!, nil)
|
||||||
#elseif canImport(Glibc)
|
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
|
||||||
pthread_join(thread, nil)
|
pthread_join(thread, nil)
|
||||||
#elseif canImport(WinSDK)
|
#elseif canImport(WinSDK)
|
||||||
#error("TODO: Port TaskDriver threading to windows")
|
#error("TODO: Port TaskDriver threading to windows")
|
||||||
|
|||||||
Reference in new issue
Block a user