parent
9b83566482
commit
b30f149dc9
|
@ -19,7 +19,7 @@ class MultiThreadDictionary<Key: Hashable & Sendable, Value: Sendable> {
|
|||
if #available(iOS 16.0, *) {
|
||||
self.lock = OSAllocatedUnfairLock(initialState: [:])
|
||||
} else {
|
||||
self.lock = UnfairLock(initialState: [:])
|
||||
self.lock = MutexLock(initialState: [:])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -65,22 +65,41 @@ fileprivate protocol Lock<State> {
|
|||
extension OSAllocatedUnfairLock: Lock {
|
||||
}
|
||||
|
||||
// from http://www.russbishop.net/the-law
|
||||
fileprivate class UnfairLock<State>: Lock {
|
||||
private var lock: UnsafeMutablePointer<os_unfair_lock>
|
||||
// something is wrong with the UnfairLock impl and it results in segv_accerrs
|
||||
fileprivate class MutexLock<State>: Lock {
|
||||
private var state: State
|
||||
private var lock = NSLock()
|
||||
|
||||
init(initialState: State) {
|
||||
self.state = initialState
|
||||
self.lock = .allocate(capacity: 1)
|
||||
self.lock.initialize(to: os_unfair_lock())
|
||||
}
|
||||
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) }
|
||||
|
||||
func withLock<R>(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R : Sendable {
|
||||
if !lock.lock(before: Date(timeIntervalSinceNow: 1)) {
|
||||
// if we can't acquire the lock after 1 second, something has gone catastrophically wrong
|
||||
fatalError()
|
||||
}
|
||||
defer { lock.unlock() }
|
||||
return try body(&state)
|
||||
}
|
||||
}
|
||||
|
||||
//// from http://www.russbishop.net/the-law
|
||||
//fileprivate class UnfairLock<State>: Lock {
|
||||
// private var lock: UnsafeMutablePointer<os_unfair_lock>
|
||||
// private var state: State
|
||||
// init(initialState: State) {
|
||||
// self.state = initialState
|
||||
// self.lock = .allocate(capacity: 1)
|
||||
// self.lock.initialize(to: os_unfair_lock())
|
||||
// }
|
||||
// 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) }
|
||||
// return try body(&state)
|
||||
// }
|
||||
//}
|
||||
|
|
|
@ -30,5 +30,45 @@ class TuskerTests: XCTestCase {
|
|||
// Put the code you want to measure the time of here.
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testFuckingLock() {
|
||||
let lock = MutexLock<[Int: Bool]>(initialState: [:])
|
||||
for i in 0..<100 {
|
||||
Thread.detachNewThread {
|
||||
for j in 0..<50_000 {
|
||||
lock.withLock {
|
||||
$0[i * 50_000 + j] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while true {
|
||||
if lock.withLock({ $0.count }) == 5_000_000 {
|
||||
break
|
||||
}
|
||||
}
|
||||
lock.withLock({ _ in
|
||||
print("WHAT THE FUUUUUUUUUUUUCK")
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fileprivate class MutexLock<State> {
|
||||
private var state: State
|
||||
private var lock = NSLock()
|
||||
|
||||
init(initialState: State) {
|
||||
self.state = initialState
|
||||
}
|
||||
|
||||
func withLock<R>(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R : Sendable {
|
||||
if !lock.lock(before: Date(timeIntervalSinceNow: 1)) {
|
||||
// if we can't acquire the lock after 1 second, something has gone catastrophically wrong
|
||||
fatalError()
|
||||
}
|
||||
defer { lock.unlock() }
|
||||
return try body(&state)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue