Skip to content

Commit

Permalink
Update handling of os_unfair_lock to manage the buffer. (Alamofire#2836)
Browse files Browse the repository at this point in the history
As recommended by Apple at WWDC, using os_unfair_lock by reference will lead to a crash rate of up to 1%.
  • Loading branch information
jshier authored Jun 14, 2019
1 parent a516974 commit 1b89a57
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions Source/Protector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ import Foundation

/// An `os_unfair_lock` wrapper.
final class UnfairLock {
private var unfairLock = os_unfair_lock()
private let unfairLock: os_unfair_lock_t

init() {
unfairLock = .allocate(capacity: 1)
unfairLock.initialize(to: os_unfair_lock())
}

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

fileprivate func lock() {
os_unfair_lock_lock(&unfairLock)
os_unfair_lock_lock(unfairLock)
}

fileprivate func unlock() {
os_unfair_lock_unlock(&unfairLock)
os_unfair_lock_unlock(unfairLock)
}

/// Executes a closure returning a value while acquiring the lock.
Expand Down

0 comments on commit 1b89a57

Please sign in to comment.