2020-10-17 17:10:10 +00:00
|
|
|
//
|
|
|
|
// MultiThreadDictionary.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 5/6/20.
|
|
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2022-09-12 02:09:04 +00:00
|
|
|
import os
|
2020-10-17 17:10:10 +00:00
|
|
|
|
2022-09-12 02:09:04 +00:00
|
|
|
// once we target iOS 16, replace uses of this with OSAllocatedUnfairLock<[Key: Value]>
|
|
|
|
// to make the lock semantics more clear
|
|
|
|
@available(iOS, obsoleted: 16.0)
|
|
|
|
class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
|
2023-02-05 16:04:11 +00:00
|
|
|
private let lock: any Lock<[Key: Value]>
|
2020-10-17 17:10:10 +00:00
|
|
|
|
2022-09-12 02:09:04 +00:00
|
|
|
init() {
|
2023-02-05 16:04:11 +00:00
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
self.lock = OSAllocatedUnfairLock(initialState: [:])
|
|
|
|
} else {
|
|
|
|
self.lock = UnfairLock(initialState: [:])
|
|
|
|
}
|
2020-10-17 17:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subscript(key: Key) -> Value? {
|
|
|
|
get {
|
2023-02-05 16:04:11 +00:00
|
|
|
return lock.withLock { dict in
|
2022-09-12 02:09:04 +00:00
|
|
|
dict[key]
|
2023-02-05 16:04:11 +00:00
|
|
|
}
|
2020-10-17 17:10:10 +00:00
|
|
|
}
|
|
|
|
set(value) {
|
2023-02-05 16:04:11 +00:00
|
|
|
_ = lock.withLock { dict in
|
2022-09-12 02:09:04 +00:00
|
|
|
dict[key] = value
|
2020-10-17 17:10:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If the result of this function is unused, it is preferable to use `removeValueWithoutReturning` as it executes asynchronously and doesn't block the calling thread.
|
|
|
|
func removeValue(forKey key: Key) -> Value? {
|
2023-02-05 16:04:11 +00:00
|
|
|
return lock.withLock { dict in
|
2022-09-12 02:09:04 +00:00
|
|
|
dict.removeValue(forKey: key)
|
2023-02-05 16:04:11 +00:00
|
|
|
}
|
2020-10-17 17:10:10 +00:00
|
|
|
}
|
2021-01-18 19:50:56 +00:00
|
|
|
|
|
|
|
func contains(key: Key) -> Bool {
|
2023-02-05 16:04:11 +00:00
|
|
|
return lock.withLock { dict in
|
2022-09-12 02:09:04 +00:00
|
|
|
dict.keys.contains(key)
|
2023-02-05 16:04:11 +00:00
|
|
|
}
|
2022-09-12 02:09:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 01:53:58 +00:00
|
|
|
// TODO: this should really be throws/rethrows but the stupid type-erased lock makes that not possible
|
2023-02-05 16:04:11 +00:00
|
|
|
func withLock<R>(_ body: @Sendable (inout [Key: Value]) throws -> R) rethrows -> R where R: Sendable {
|
|
|
|
return try lock.withLock { dict in
|
|
|
|
return try body(&dict)
|
2022-10-28 03:06:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-10 01:53:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-12 02:09:04 +00:00
|
|
|
// TODO: replace this only with OSAllocatedUnfairLock
|
|
|
|
@available(iOS, obsoleted: 16.0)
|
|
|
|
fileprivate protocol Lock<State> {
|
|
|
|
associatedtype State
|
|
|
|
func withLock<R>(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R: Sendable
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(iOS 16.0, *)
|
|
|
|
extension OSAllocatedUnfairLock: Lock {
|
|
|
|
}
|
|
|
|
|
2022-10-10 01:53:58 +00:00
|
|
|
// from http://www.russbishop.net/the-law
|
|
|
|
fileprivate class UnfairLock<State>: Lock {
|
|
|
|
private var lock: UnsafeMutablePointer<os_unfair_lock>
|
2022-09-12 02:09:04 +00:00
|
|
|
private var state: State
|
|
|
|
init(initialState: State) {
|
|
|
|
self.state = initialState
|
2022-10-10 01:53:58 +00:00
|
|
|
self.lock = .allocate(capacity: 1)
|
|
|
|
self.lock.initialize(to: os_unfair_lock())
|
2022-09-12 02:09:04 +00:00
|
|
|
}
|
2022-10-10 01:53:58 +00:00
|
|
|
deinit {
|
|
|
|
self.lock.deinitialize(count: 1)
|
|
|
|
self.lock.deallocate()
|
|
|
|
}
|
|
|
|
func withLock<R>(_ body: (inout State) throws -> R) rethrows -> R where R: Sendable {
|
|
|
|
os_unfair_lock_lock(lock)
|
|
|
|
defer { os_unfair_lock_unlock(lock) }
|
2022-09-12 02:09:04 +00:00
|
|
|
return try body(&state)
|
2021-01-18 19:50:56 +00:00
|
|
|
}
|
2020-10-17 17:10:10 +00:00
|
|
|
}
|