From 016956f83ce382407d047e9ebd214396669f9bdc Mon Sep 17 00:00:00 2001 From: Rachel Brindle Date: Sat, 12 Aug 2023 12:58:49 -0700 Subject: [PATCH 1/2] Make PollingDefaults threadsafe --- Sources/Nimble/Polling.swift | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/Sources/Nimble/Polling.swift b/Sources/Nimble/Polling.swift index 31a03d740..78f004525 100644 --- a/Sources/Nimble/Polling.swift +++ b/Sources/Nimble/Polling.swift @@ -34,9 +34,28 @@ public struct AsyncDefaults { /// or slow down poll interval. Default timeout interval is 1, and poll interval is 0.01. /// /// - Note: This used to be known as ``AsyncDefaults``. -public struct PollingDefaults { - public static var timeout: NimbleTimeInterval = .seconds(1) - public static var pollInterval: NimbleTimeInterval = .milliseconds(10) +public struct PollingDefaults: @unchecked Sendable { + private static let lock = NSRecursiveLock() + + private static var _timeout: NimbleTimeInterval = .seconds(1) + private static var _pollInterval: NimbleTimeInterval = .milliseconds(10) + + public static var timeout: NimbleTimeInterval { + get { + return lock.withLock { return _timeout } + } + set { + lock.withLock { _timeout = newValue } + } + } + public static var pollInterval: NimbleTimeInterval { + get { + return lock.withLock { return _pollInterval } + } + set { + lock.withLock { _pollInterval = newValue } + } + } } internal enum AsyncMatchStyle { From 126d75e81234913113e666dd349556402fcd8c43 Mon Sep 17 00:00:00 2001 From: Rachel Brindle Date: Sun, 25 Feb 2024 23:23:10 -0800 Subject: [PATCH 2/2] Oh. right. NSLocking.withLock wasn't available back in xcode 14... --- Sources/Nimble/Polling.swift | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sources/Nimble/Polling.swift b/Sources/Nimble/Polling.swift index 78f004525..abb64e94e 100644 --- a/Sources/Nimble/Polling.swift +++ b/Sources/Nimble/Polling.swift @@ -42,18 +42,26 @@ public struct PollingDefaults: @unchecked Sendable { public static var timeout: NimbleTimeInterval { get { - return lock.withLock { return _timeout } + lock.lock() + defer { lock.unlock() } + return _timeout } set { - lock.withLock { _timeout = newValue } + lock.lock() + defer { lock.unlock() } + _timeout = newValue } } public static var pollInterval: NimbleTimeInterval { get { - return lock.withLock { return _pollInterval } + lock.lock() + defer { lock.unlock() } + return _pollInterval } set { - lock.withLock { _pollInterval = newValue } + lock.lock() + defer { lock.unlock() } + _pollInterval = newValue } } }