Skip to content

Commit

Permalink
Fix locking in requests, os_unfair_lock not to be directly used in Sw…
Browse files Browse the repository at this point in the history
…ift (#429)
  • Loading branch information
julianlocke authored Jul 29, 2024
1 parent 40885da commit b16fc06
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
17 changes: 13 additions & 4 deletions Source/SwiftyDropbox/Shared/Handwritten/Request+TokenRefresh.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,25 @@ protocol RequestControlling {
class RequestWithTokenRefresh: ApiRequest {
class StateAccess {
private var mutableState: MutableState
private var lock = os_unfair_lock()
private var lock = { // heap allocate the lock for use in Swift
let lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
lock.initialize(to: .init())
return lock
}()

init(mutableState: MutableState) {
self.mutableState = mutableState
}

deinit {
lock.deinitialize(count: 1)
lock.deallocate()
}

fileprivate func accessStateWithLock<T>(block: (MutableState) throws -> T) rethrows -> T {
try lock.sync {
try block(mutableState)
}
os_unfair_lock_lock(lock)
defer { os_unfair_lock_unlock(lock) }
return try block(mutableState)
}
}

Expand Down
10 changes: 0 additions & 10 deletions Source/SwiftyDropbox/Shared/Handwritten/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,6 @@ extension Optional where Wrapped: SomeOptional {
}
}

// MARK: Concurrency

public extension os_unfair_lock {
mutating func sync<T>(execute: () throws -> T) rethrows -> T {
os_unfair_lock_lock(&self)
defer { os_unfair_lock_unlock(&self) }
return try execute()
}
}

// MARK: Logging

public enum LogLevel {
Expand Down

0 comments on commit b16fc06

Please sign in to comment.