From ae77aece2a985b2a4c3c846ff0526dd0a6949f17 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 7 May 2024 19:58:23 -0700 Subject: [PATCH] Make DatabaseIDCache properly Sendable (instead of unchecked) by using an OSAllocatedUnfairLock. --- Articles/Sources/Articles/DatabaseID.swift | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Articles/Sources/Articles/DatabaseID.swift b/Articles/Sources/Articles/DatabaseID.swift index d036c4ed2..1decef9b8 100644 --- a/Articles/Sources/Articles/DatabaseID.swift +++ b/Articles/Sources/Articles/DatabaseID.swift @@ -8,22 +8,25 @@ import Foundation import FoundationExtras +import os -class DatabaseIDCache: @unchecked Sendable { +final class DatabaseIDCache: Sendable { static let shared = DatabaseIDCache() - private var databaseIDCache = [String: String]() - private let databaseIDCacheLock = NSLock() + private let _databaseIDCache = OSAllocatedUnfairLock(initialState: [String: String]()) + private var databaseIDCache: [String: String] { + get { + _databaseIDCache.withLock { $0 } + } + set { + _databaseIDCache.withLock { $0 = newValue } + } + } /// Generates — or retrieves from cache — a database-suitable ID based on a String. func databaseIDWithString(_ s: String) -> String { - databaseIDCacheLock.lock() - defer { - databaseIDCacheLock.unlock() - } - if let identifier = databaseIDCache[s] { return identifier }