From 1b89a57c2f272408b84d20132a2ed6628e95d3e2 Mon Sep 17 00:00:00 2001 From: Jon Shier Date: Thu, 13 Jun 2019 20:56:40 -0400 Subject: [PATCH] Update handling of os_unfair_lock to manage the buffer. (#2836) As recommended by Apple at WWDC, using os_unfair_lock by reference will lead to a crash rate of up to 1%. --- Source/Protector.swift | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/Source/Protector.swift b/Source/Protector.swift index c9c33e859..c8acf9f94 100644 --- a/Source/Protector.swift +++ b/Source/Protector.swift @@ -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.