mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
refactor(mobile): introduce image request registry on iOS (#27486)
* refactor: replace DispatchQueue + DispatchSemaphore with OperationQueue for image processing * implement RequestRegistry and UnfairLock for managing cancellable requests * implement requests registry for local and remote image processing * remove Cancellable protocol and cancel method from request registry * use mutex --------- Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
import Darwin
|
||||
|
||||
// Can be replaced with std Mutex when the deployment target is iOS 18+
|
||||
struct Mutex<Value: ~Copyable>: ~Copyable, @unchecked Sendable {
|
||||
struct _Buffer: ~Copyable {
|
||||
var lock: os_unfair_lock = .init()
|
||||
var value: Value
|
||||
|
||||
init(value: consuming Value) {
|
||||
self.value = value
|
||||
}
|
||||
|
||||
deinit {}
|
||||
}
|
||||
|
||||
let _buffer: UnsafeMutablePointer<_Buffer>
|
||||
|
||||
init(_ initialValue: consuming sending Value) {
|
||||
_buffer = .allocate(capacity: 1)
|
||||
_buffer.initialize(to: _Buffer(value: initialValue))
|
||||
}
|
||||
|
||||
deinit {
|
||||
_buffer.deinitialize(count: 1)
|
||||
_buffer.deallocate()
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
borrowing func withLock<Result: ~Copyable, E: Error>(
|
||||
_ body: (inout sending Value) throws(E) -> sending Result
|
||||
) throws(E) -> sending Result {
|
||||
os_unfair_lock_lock(&_buffer.pointee.lock)
|
||||
defer { os_unfair_lock_unlock(&_buffer.pointee.lock) }
|
||||
return try body(&_buffer.pointee.value)
|
||||
}
|
||||
}
|
||||
|
||||
// Can be replaced with OSAllocatedUnfairLock when the deployment target is iOS 16+
|
||||
typealias UnfairLock = Mutex<Void>
|
||||
|
||||
extension Mutex where Value == Void {
|
||||
init() {
|
||||
self.init(())
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
borrowing func withLock<Result: ~Copyable, E: Error>(
|
||||
_ body: () throws(E) -> sending Result
|
||||
) throws(E) -> sending Result {
|
||||
os_unfair_lock_lock(&_buffer.pointee.lock)
|
||||
defer { os_unfair_lock_unlock(&_buffer.pointee.lock) }
|
||||
return try body()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user