|
1 | 1 | # swift-dispatch-async |
2 | 2 |
|
3 | | -## ⚠️ WARNING - This is an 🧪experimental🧪 repository and should not be adopted at large. |
| 3 | +DispatchAsync provides a pure-swift implementation of `Dispatch`, powered by Swift Concurrency. It is currently intended for use only |
| 4 | +in WASM targets, but could be used in other scenarios should the need arise. |
4 | 5 |
|
5 | | -DispatchAsync is a temporary experimental repository aimed at implementing missing Dispatch support in the SwiftWasm toolchain. |
6 | | -Currently, [SwiftWasm doesn't include Dispatch](https://book.swiftwasm.org/getting-started/porting.html#swift-foundation-and-dispatch). |
7 | | -But, SwiftWasm does support Swift Concurrency. DispatchAsync implements a number of common Dispatch API's using Swift Concurrency |
8 | | -under the hood. |
| 6 | +The Swift for WebAssembly SDK [doesn't currenty include Dispatch](https://book.swiftwasm.org/getting-started/porting.html#swift-foundation-and-dispatch). |
| 7 | +But, it does support Swift Concurrency. DispatchAsync implements a number of common Dispatch API's using Swift Concurrency |
| 8 | +under the hood. This allows `import DispatchAsync` to be used in wasm targets as a drop-in replacement anywhere `import Dispatch` is |
| 9 | +currently in use. |
9 | 10 |
|
10 | 11 | Dispatch Async does not provide blocking API's such as `DispatchQueue.sync`, primarily due to the intentional lack of blocking |
11 | 12 | API's in Swift Concurrency. |
12 | 13 |
|
13 | | -# Toolchain Adoption Plans |
14 | | - |
15 | | -DispatchAsync is not meant for consumption abroad directly as a new Swift Module. Rather, the intention is to provide eventual integration |
16 | | -as a drop-in replacement for Dispatch when compiling to Wasm. |
17 | | - |
18 | | -There are a few paths to adoption into the Swift toolchain |
19 | | - |
20 | | -- DispatchAsync can be emplaced inside the [libDispatch repository](https://github.com/swiftlang/swift-corelibs-libdispatch), and compiled |
21 | | -into the toolchain only for wasm targets. |
22 | | -- DispatchAsync can be consumed in place of libDispatch when building the Swift toolchain. |
23 | | - |
24 | | -Ideally, with either approach, this repository would transfer ownership to the swiftlang organization. |
25 | | - |
26 | | -In the interim, to move wasm support forward, portions of DispatchAsync may be inlined (copy-pasted) |
27 | | -into various libraries to enable wasm support. DispatchAsync is designed for this purpose, and has |
28 | | -special `#if` handling to ensure that existing temporary usages will be elided without breakage |
29 | | -the moment SwiftWasm adds support for `Dispatch` into the toolchain. |
30 | | - |
31 | | -# DispatchSemaphore Limitations |
32 | | - |
33 | | -The current implementation of `DispatchSemaphore` has some limitations. Blocking threads goes against the design goals of Swift Concurrency. |
34 | | -The `wait` function on `DispatchSemaphore` goes against this goal. Furthermore, most wasm targets run on a single thread from the web |
35 | | -browser, so any time the `wait` function ends up blocking the calling thread, it would almost certainly freeze the single-threaded wasm |
36 | | -executable. |
37 | | - |
38 | | -To navigate these issues, there are some limitations: |
39 | | - |
40 | | -- For wasm compilation targets, `DispatchSemaphore` assumes single-threaded execution, and lacks various safeguards that would otherwise |
41 | | -be needed for multi-threaded execution. This makes the implementation much easier. |
42 | | -- For wasm targets, calls to `signal` and `wait` must be balanced. An assertion triggers if `wait` is called more times than `signal`. |
43 | | -- DispatchSemaphore is deprecated for wasm targets, and AsyncSemaphore is encouraged as the replacement. |
44 | | -- For non-wasm targets, DispatchSemaphore is simply a typealias for `AsyncSemaphore`, and provides only a non-blocking async `wait` |
45 | | -function. This reduces potential issues that can arise from wait being a thread-blocking function. |
46 | | - |
47 | 14 | # Usage |
48 | 15 |
|
49 | | -If you've scrolled this far, you probably saw the warning. But just to make sure… |
50 | | - |
51 | | -> ⚠️ WARNING - This is an 🧪experimental🧪 repository and should not be adopted at large at the present time. |
52 | | -
|
53 | | -PassiveLogic is [actively working](https://github.com/PassiveLogic/swift-web-examples/issues/1) to mainstream this into the SwiftWasm |
54 | | -toolchain. But if you can't wait, here are some tips. |
| 16 | +PassiveLogic plans to mainstream this into the SwiftWasm toolchain. If possible, it's better to wait until this is part of the |
| 17 | +official Swift for WebAssembly SDK. But if you need this now, below are some usage tips. |
55 | 18 |
|
56 | 19 | ## 1. Only use this for WASI platforms, and only if Dispatch cannot be imported. |
57 | 20 |
|
@@ -133,6 +96,40 @@ DispatchQueue.main.async { |
133 | 96 | } |
134 | 97 | ``` |
135 | 98 |
|
| 99 | +# Toolchain Adoption Plans |
| 100 | + |
| 101 | +DispatchAsync is not meant for consumption abroad directly as a new Swift Module. Rather, the intention is to provide eventual integration |
| 102 | +as a drop-in replacement for Dispatch when compiling to Wasm. |
| 103 | + |
| 104 | +There are a few paths to adoption into the Swift toolchain |
| 105 | + |
| 106 | +- DispatchAsync can be emplaced inside the [libDispatch repository](https://github.com/swiftlang/swift-corelibs-libdispatch), and compiled |
| 107 | +into the toolchain only for wasm targets. |
| 108 | +- DispatchAsync can be consumed in place of libDispatch when building the Swift toolchain. |
| 109 | + |
| 110 | +Ideally, with either approach, this repository would transfer ownership to the swiftlang organization. |
| 111 | + |
| 112 | +In the interim, to move wasm support forward, portions of DispatchAsync may be inlined (copy-pasted) |
| 113 | +into various libraries to enable wasm support. DispatchAsync is designed for this purpose, and has |
| 114 | +special `#if` handling to ensure that existing temporary usages will be elided without breakage |
| 115 | +the moment SwiftWasm adds support for `Dispatch` into the toolchain. |
| 116 | + |
| 117 | +# DispatchSemaphore Limitations |
| 118 | + |
| 119 | +The current implementation of `DispatchSemaphore` has some limitations. Blocking threads goes against the design goals of Swift Concurrency. |
| 120 | +The `wait` function on `DispatchSemaphore` goes against this goal. Furthermore, most wasm targets run on a single thread from the web |
| 121 | +browser, so any time the `wait` function ends up blocking the calling thread, it would almost certainly freeze the single-threaded wasm |
| 122 | +executable. |
| 123 | + |
| 124 | +To navigate these issues, there are some limitations: |
| 125 | + |
| 126 | +- For wasm compilation targets, `DispatchSemaphore` assumes single-threaded execution, and lacks various safeguards that would otherwise |
| 127 | +be needed for multi-threaded execution. This makes the implementation much easier. |
| 128 | +- For wasm targets, calls to `signal` and `wait` must be balanced. An assertion triggers if `wait` is called more times than `signal`. |
| 129 | +- DispatchSemaphore is deprecated for wasm targets, and AsyncSemaphore is encouraged as the replacement. |
| 130 | +- For non-wasm targets, DispatchSemaphore is simply a typealias for `AsyncSemaphore`, and provides only a non-blocking async `wait` |
| 131 | +function. This reduces potential issues that can arise from wait being a thread-blocking function. |
| 132 | + |
136 | 133 | # LICENSE |
137 | 134 |
|
138 | 135 | This project is distributed by PassiveLogic under the Apache-2.0 license. See |
|
0 commit comments