Skip to content

Include an queue-backed actor example #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Guide.docc/IncrementalAdoption.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ class PersonalTransportation {
}
```

## Integrating DispatchSerialQueue with Actors

By default, the mechanism actors use to schedule and execute work
is system-defined.
However you can override this to provide a custom implementation.
The `DispatchSerialQueue` type includes built-in support for this facility.

```swift
actor LandingSite {
private let queue = DispatchSerialQueue(label: "something")

nonisolated var unownedExecutor: UnownedSerialExecutor {
queue.asUnownedSerialExecutor()
}

func acceptTransport(_ transport: PersonalTransportation) {
// this function will be running on queue
}
}
```

This can be useful if you want to migrate a type towards the actor model
while maintaining compatibility with code that depends on `DispatchQueue`.

## Backwards Compatibility

It's important to keep in mind that static isolation, being part of the type
Expand Down Expand Up @@ -330,6 +354,7 @@ NS_SWIFT_ASYNC_NAME
NS_SWIFT_ASYNC_NOTHROW
NS_SWIFT_UNAVAILABLE_FROM_ASYNC(msg)
```

### Dealing with missing isolation annotations in Objective-C libraries

While the SDKs and other Objective-C libraries make progress in adopting Swift concurrency,
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ let package = Package(
),
.executableTarget(
name: "Swift5Examples",
dependencies: ["Library"],
dependencies: ["Library", "ObjCLibrary"],
swiftSettings: [
.swiftLanguageVersion(.v5)
]
),
.executableTarget(
name: "Swift6Examples",
dependencies: ["Library"]
dependencies: ["Library", "ObjCLibrary"]
)
]
)
34 changes: 34 additions & 0 deletions Sources/Examples/IncrementalMigration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Dispatch
import ObjCLibrary

/// Example that backs an actor with a queue.
///
/// > Note: `DispatchSerialQueue`'s initializer was only made available in more recent OS versions.
@available(macOS 14.0, iOS 17.0, macCatalyst 17.0, tvOS 17.0, watchOS 10.0, *)
actor LandingSite {
private let queue = DispatchSerialQueue(label: "SerialQueue")

// this currently failed to build because of the @available usage, rdar://116684282
// nonisolated var unownedExecutor: UnownedSerialExecutor {
// queue.asUnownedSerialExecutor()
// }

func acceptTransport(_ transport: JPKJetPack) {
// this function will be running on queue
}
}

func exerciseIncrementalMigrationExamples() async {
print("Incremental Migration Examples")

if #available(macOS 14.0, iOS 17.0, macCatalyst 17.0, tvOS 17.0, watchOS 10.0, *) {
print(" - using an actor with a DispatchSerialQueue executor")
let site = LandingSite()

let transport = JPKJetPack()

await site.acceptTransport(transport)
await site.acceptTransport(transport)
await site.acceptTransport(transport)
}
}
1 change: 1 addition & 0 deletions Sources/Examples/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ let manualSerialQueue = DispatchQueue(label: "com.apple.SwiftMigrationGuide")
await exerciseGlobalExamples()
await exerciseBoundaryCrossingExamples()
await exerciseConformanceMismatchExamples()
await exerciseIncrementalMigrationExamples()