Skip to content
Closed
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
28 changes: 8 additions & 20 deletions Sources/AsyncQueue/Utilities/Delivery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Dispatch
import Foundation

actor Delivery<Success: Sendable, Failure: Error> {
func sendValue(_ value: Success) {
Expand Down Expand Up @@ -115,29 +115,17 @@ extension Delivery where Failure == any Error {
// MARK: - Locked

// We'd use `OSAllocatedUnfairLock` or `Mutex` but the minimum supported version is much higher than what we support.
private struct Locked<State>: @unchecked Sendable {
private final class Locked<State>: @unchecked Sendable {
init(value: State) {
container = .init(value: value)
unsafeValue = value
}

func withLock<R>(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R: Sendable {
try lockQueue.sync {
var value = container.unsafeValue
let returnValue = try body(&value)
container.unsafeValue = value
return returnValue
}
lock.lock()
defer { lock.unlock() }
return try body(&unsafeValue)
}

private let container: UnsafeContainer

private final class UnsafeContainer: @unchecked Sendable {
init(value: State) {
unsafeValue = value
}

var unsafeValue: State
}
private var unsafeValue: State
private let lock = NSLock()
}

private let lockQueue = DispatchQueue(label: "LockedValue.lockQueue", target: DispatchQueue.global())