mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: NIOLockedValueBox has a 'safer' API than NIOLock as it only provides scoped access to its boxed value. NIOLock requires users to only access protected state while the lock is acquired. As such NIOLockedValueBox should be preferred where possible. However, there are cases where manual control must be used (such as storing a continuation) and users must use a NIOLock for this. There are two downsides to this: 1. All other access to the protected state must use the NIOLock API putting the onus on the developer to only access the protected state while the lock is held. 2. NIOLock can't store its protected state inline which typically results in users storing it on a class. Modifications: - Add an 'unsafe' view to NIOLockedValueBox which allows users to manually control the lock and access its protected state - Update NIOAsyncWriter and NIOThrowingAsyncSequenceProducer to use NIOLockedValueBox Result: - Safer locking API is used in more places - Fewer allocations