Skip to content

Commit

Permalink
Rework exclusivity lock
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanbaird committed May 26, 2024
1 parent 0976a3e commit 3fa347b
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 16 deletions.
79 changes: 79 additions & 0 deletions Sources/ColorWellKit/Utilities/LockedState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// LockedState.swift
// ColorWellKit
//

import os

/// A locking wrapper around a state.
///
/// This implementation is heavily inspired by Foundation's `LockedState` type:
/// https://github.com/apple/swift-foundation/blob/main/Sources/FoundationEssentials/LockedState.swift
struct LockedState<State> {
private enum Lock {
typealias UnfairLock = os_unfair_lock

Check warning on line 14 in Sources/ColorWellKit/Utilities/LockedState.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Nesting Violation: Types should be nested at most 1 level deep (nesting)

static func initialize(_ lock: UnsafeMutablePointer<UnfairLock>) {
lock.initialize(to: UnfairLock())
}

static func deinitialize(_ lock: UnsafeMutablePointer<UnfairLock>) {
lock.deinitialize(count: 1)
}

static func lock(_ lock: UnsafeMutablePointer<UnfairLock>) {
os_unfair_lock_lock(lock)
}

static func unlock(_ lock: UnsafeMutablePointer<UnfairLock>) {
os_unfair_lock_unlock(lock)
}
}

private class Buffer: ManagedBuffer<State, Lock.UnfairLock> {
deinit {
withUnsafeMutablePointerToElements { lock in
Lock.deinitialize(lock)
}
}
}

private let buffer: ManagedBuffer<State, Lock.UnfairLock>

var state: State {
buffer.withUnsafeMutablePointerToHeader { state in
state.pointee
}
}

init(initialState: State) {
self.buffer = Buffer.create(minimumCapacity: 1) { buffer in
buffer.withUnsafeMutablePointerToElements { lock in
Lock.initialize(lock)
}
return initialState
}
}

func withLock<T>(_ body: (inout State) throws -> T) rethrows -> T {
try buffer.withUnsafeMutablePointers { state, lock in
Lock.lock(lock)
defer {
Lock.unlock(lock)
}
return try body(&state.pointee)
}
}

func withLockExtendingLifetimeOfState<T>(_ body: (inout State) throws -> T) rethrows -> T {
try buffer.withUnsafeMutablePointers { state, lock in
Lock.lock(lock)
return try withExtendedLifetime(state.pointee) {
defer {
Lock.unlock(lock)
}
return try body(&state.pointee)
}
}
}
}
27 changes: 11 additions & 16 deletions Sources/ColorWellKit/Views/Cocoa/CWColorWell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ public class CWColorWell: _CWColorWellBaseControl {

// MARK: Instance Properties

private let exclusivityLock = NSRecursiveLock()

private var isExclusive = true
private let exclusivity = LockedState(initialState: true)

private var popover: NSPopover?

Expand Down Expand Up @@ -143,7 +141,7 @@ public class CWColorWell: _CWColorWellBaseControl {
}
}
if shouldActivate {
if isExclusive && allowsMultipleSelection {
if exclusivity.state && allowsMultipleSelection {
NSColorPanel.shared.enforceExclusivity(of: self)
}
NSColorPanel.shared.attach(self)
Expand Down Expand Up @@ -213,25 +211,22 @@ public class CWColorWell: _CWColorWellBaseControl {
/// Until the color well is activated again, changes to the color panel will not
/// affect the color well's state.
public func deactivate() {
withExclusivityLock(isExclusive) { isActive = false }
withExclusivityLock(exclusivity.state) { isActive = false }
}

// MARK: Private/Internal Instance Methods

/// Performs the given closure while locking the exclusivity of the color well
/// to the given state in a thread-safe manner.
private func withExclusivityLock<T>(
_ isExclusive: @autoclosure () -> Bool,
body: () throws -> T
) rethrows -> T {
exclusivityLock.lock()
let cachedIsExclusive = self.isExclusive
self.isExclusive = isExclusive()
defer {
self.isExclusive = cachedIsExclusive
exclusivityLock.unlock()
private func withExclusivityLock<T>(_ isExclusive: Bool, body: () throws -> T) rethrows -> T {
try exclusivity.withLock { state in
let cachedState = state
state = isExclusive
defer {
state = cachedState
}
return try body()
}
return try body()
}

@objc(deactivate) // exposed to Objective-C as `deactivate`
Expand Down

0 comments on commit 3fa347b

Please sign in to comment.