Compare commits

...
7 Commits
Author SHA1 Message Date
Mason Kim 5c8bd186f4 Fix a few typos (#329)
* [DOCS] fix typo: precicely → precisely

* [DOCS] fix typo: ever → every

* [DOCS] fix typo: AsyncBufferSequence, relative
2024-09-02 14:15:02 +02:00
finagolfin 2503842d68 Use Bionic module from new Android overlay in Swift 6 instead (#326)
The new module and overlay were merged into Swift 6 in swiftlang/swift#74758.
2024-08-07 11:18:51 +01:00
Adam Fowler e83857ca55 Add Musl import, error if unrecognised platform (#325)
* Add Musl import, error if unrecognised platform

* Add #else #error for all platform checks
2024-08-06 12:26:55 +02:00
Yuta Saito 6ae9a051f7 Add support for SWIFTCI_USE_LOCAL_DEPS convention (#311)
To use this package in utils/build-script pipeline
2024-04-10 14:41:45 -07:00
Franz Busch 46b4464735 Fix some strict concurrency warnings (#310)
# Motivation

There were a few new strict concurrency warnings that this PR fixes.
2024-04-04 20:46:16 +01:00
Eric Horacek d162617838 Depend on specific products from swift-collections (#307)
* Only depend on OrderedCollections from swift-collections

Before this change, once consumers update to SwiftCollections 1.1.0 they'll pull in all of the new collection dependencies (HashTree, BitCollections, etc.). In our case, this increased our binary size by ~1MB. To fix this, we switch to only depending on what we need.

* Add DequeModule dependency
2024-02-28 12:47:12 +00:00
leogdion 14729ea7f9 Update README.md (#306)
Missing Version Number
2024-01-24 11:28:26 +00:00
13 changed files with 104 additions and 47 deletions

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@
## 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
+2 -2
View File
@@ -28,7 +28,7 @@ By applying the buffer operator to the previous example, the file can be read as
## 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.
@@ -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
extension AsyncSequence where Self: Sendable {
+3 -3
View File
@@ -178,7 +178,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .element(separator)
self.every = every
@@ -186,7 +186,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .syncClosure(separator)
self.every = every
@@ -194,7 +194,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .asyncClosure(separator)
self.every = every
+1 -1
View File
@@ -18,7 +18,7 @@
## 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
+15 -5
View File
@@ -13,14 +13,13 @@ let package = Package(
products: [
.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: [
.target(
name: "AsyncAlgorithms",
dependencies: [.product(name: "Collections", package: "swift-collections")],
dependencies: [
.product(name: "OrderedCollections", package: "swift-collections"),
.product(name: "DequeModule", package: "swift-collections"),
],
swiftSettings: [
.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"),
]
}
+1 -1
View File
@@ -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:
```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:
@@ -16,16 +16,19 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
typealias SuspendedProducer = UnsafeContinuation<Void, 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 {
case initial(base: Base)
case buffering(
task: Task<Void, Never>,
buffer: Deque<Result<Element, Error>>,
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
suspendedProducer: SuspendedProducer?,
suspendedConsumer: SuspendedConsumer?
)
case modifying
case finished(buffer: Deque<Result<Element, Error>>)
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
}
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
precondition(buffer.count < limit, "Invalid state. The buffer should be available for stacking a new element.")
self.state = .modifying
buffer.append(.success(element))
buffer.append(.success(.init(element)))
self.state = .buffering(task: task, buffer: buffer, suspendedProducer: nil, suspendedConsumer: nil)
return .none
@@ -218,7 +221,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
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):
preconditionFailure("Invalid states. There is already a suspended consumer.")
@@ -233,7 +236,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
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
let result = buffer.popFirst()!
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):
preconditionFailure("Invalid states. There is already a suspended consumer.")
@@ -272,7 +275,7 @@ struct BoundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
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)
}
// 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 {
case initial(base: Base)
case buffering(
task: Task<Void, Never>,
buffer: Deque<Result<Element, Error>>,
buffer: Deque<Result<UnsafeTransfer<Element>, Error>>,
suspendedConsumer: SuspendedConsumer?
)
case modifying
case finished(buffer: Deque<Result<Element, Error>>)
case finished(buffer: Deque<Result<UnsafeTransfer<Element>, Error>>)
}
private var state: State
@@ -84,15 +87,15 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
switch self.policy {
case .unlimited:
buffer.append(.success(element))
buffer.append(.success(.init(element)))
case .bufferingNewest(let limit):
if buffer.count >= limit {
_ = buffer.popFirst()
}
buffer.append(.success(element))
buffer.append(.success(.init(element)))
case .bufferingOldest(let limit):
if buffer.count < limit {
buffer.append(.success(element))
buffer.append(.success(.init(element)))
}
}
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
@@ -170,7 +173,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
return .returnResult(result)
return .returnResult(result.map { $0.wrapped })
case .modifying:
preconditionFailure("Invalid state.")
@@ -182,7 +185,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
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
let result = buffer.popFirst()!
self.state = .buffering(task: task, buffer: buffer, suspendedConsumer: nil)
return .resumeConsumer(result)
return .resumeConsumer(result.map { $0.wrapped })
case .modifying:
preconditionFailure("Invalid state.")
@@ -220,7 +223,7 @@ struct UnboundedBufferStateMachine<Base: AsyncSequence> {
self.state = .modifying
let result = buffer.popFirst()!
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.State: Sendable where Base: Sendable { }
@@ -10,9 +10,6 @@
//===----------------------------------------------------------------------===//
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 {
private struct SuspendedProducer: Hashable, Sendable {
let id: UInt64
@@ -157,7 +157,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .element(separator)
self.every = every
@@ -165,7 +165,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .syncClosure(separator)
self.every = every
@@ -173,7 +173,7 @@ public struct AsyncInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .asyncClosure(separator)
self.every = every
@@ -310,7 +310,7 @@ public struct AsyncThrowingInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .syncClosure(separator)
self.every = every
@@ -318,7 +318,7 @@ public struct AsyncThrowingInterspersedSequence<Base: AsyncSequence> {
@usableFromInline
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.separator = .asyncClosure(separator)
self.every = every
+18 -6
View File
@@ -13,19 +13,25 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(WinSDK)
import WinSDK
#elseif canImport(Bionic)
import Bionic
#else
#error("Unsupported platform")
#endif
internal struct Lock {
#if canImport(Darwin)
typealias Primitive = os_unfair_lock
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
typealias Primitive = pthread_mutex_t
#elseif canImport(WinSDK)
typealias Primitive = SRWLOCK
#else
typealias Primitive = Int
#error("Unsupported platform")
#endif
typealias PlatformLock = UnsafeMutablePointer<Primitive>
@@ -38,16 +44,18 @@ internal struct Lock {
fileprivate static func initialize(_ platformLock: PlatformLock) {
#if canImport(Darwin)
platformLock.initialize(to: os_unfair_lock())
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
let result = pthread_mutex_init(platformLock, nil)
precondition(result == 0, "pthread_mutex_init failed")
#elseif canImport(WinSDK)
InitializeSRWLock(platformLock)
#else
#error("Unsupported platform")
#endif
}
fileprivate static func deinitialize(_ platformLock: PlatformLock) {
#if canImport(Glibc)
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
let result = pthread_mutex_destroy(platformLock)
precondition(result == 0, "pthread_mutex_destroy failed")
#endif
@@ -57,21 +65,25 @@ internal struct Lock {
fileprivate static func lock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_lock(platformLock)
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_mutex_lock(platformLock)
#elseif canImport(WinSDK)
AcquireSRWLockExclusive(platformLock)
#else
#error("Unsupported platform")
#endif
}
fileprivate static func unlock(_ platformLock: PlatformLock) {
#if canImport(Darwin)
os_unfair_lock_unlock(platformLock)
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
let result = pthread_mutex_unlock(platformLock)
precondition(result == 0, "pthread_mutex_unlock failed")
#elseif canImport(WinSDK)
ReleaseSRWLockExclusive(platformLock)
#else
#error("Unsupported platform")
#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
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(Bionic)
import Bionic
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#else
#error("Unsupported platform")
#endif
#if canImport(Darwin)
@@ -24,11 +30,16 @@ func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return nil
}
#elseif canImport(Glibc)
#elseif (canImport(Glibc) && !os(Android)) || canImport(Musl)
func start_thread(_ raw: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
Unmanaged<TaskDriver>.fromOpaque(raw!).takeRetainedValue().run()
return nil
}
#elseif os(Android)
func start_thread(_ raw: UnsafeMutableRawPointer) -> UnsafeMutableRawPointer {
Unmanaged<TaskDriver>.fromOpaque(raw).takeRetainedValue().run()
return UnsafeMutableRawPointer(bitPattern: 0xdeadbee)!
}
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
#endif
@@ -38,7 +49,7 @@ final class TaskDriver {
let queue: WorkQueue
#if canImport(Darwin)
var thread: pthread_t?
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
var thread = pthread_t()
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")
@@ -50,7 +61,7 @@ final class TaskDriver {
}
func start() {
#if canImport(Darwin) || canImport(Glibc)
#if canImport(Darwin) || canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_create(&thread, nil, start_thread,
Unmanaged.passRetained(self).toOpaque())
#elseif canImport(WinSDK)
@@ -68,7 +79,7 @@ final class TaskDriver {
func join() {
#if canImport(Darwin)
pthread_join(thread!, nil)
#elseif canImport(Glibc)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_join(thread, nil)
#elseif canImport(WinSDK)
#error("TODO: Port TaskDriver threading to windows")