Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NIOSendableBox: allow off-loop initialisation iff Value is Sendable (#…
…2753) ### Motivation: `NIOLoopBound` and `NIOLoopBoundBox` are important tools for making mutable types `Sendable`. The power of `NIOLoopBoundBox` is that it allows correct mutation, whilst remaining `Sendable` without locks and without having to use `@unchecked` if the mutable class makes sure to have all accesses (reading & writing) run on one particular `EventLoop`. This is safe as `EventLoop`s guarantee sequentially consistent memory order across executions of different work items/events. Typically `EventLoop`s achieve this by just being thread-bound. These types are well used already but there's a small and common pattern which is safe but unsupported as of yet: Initialise a `NIOLoopBoundBox` with a `Sendable` value _off_ the loop. All further accesses (reads & writes) however happen _on_ the loop. That's safe because of Swift's Definitive Initialisation (DI) but `NIOLoopBoundBox` didn't support this pattern (apart from `makeEmptyBox` which always initialises with `nil`). ### Modifications: - Allow `Sendable` values to be provided whilst creating the type off loop. ### Result: - Even more types can be safely & correctly made `Sendable` without using `@unchecked.
- Loading branch information