diff --git a/Sources/Time/8-Formatting/FormatTemplates.swift b/Sources/Time/8-Formatting/FormatTemplates.swift index fe1229e..fbe2373 100644 --- a/Sources/Time/8-Formatting/FormatTemplates.swift +++ b/Sources/Time/8-Formatting/FormatTemplates.swift @@ -7,6 +7,8 @@ import Foundation +#warning("TODO: document these") + public struct Template: Format { internal let template: String internal init(_ template: String) { diff --git a/Sources/Time/Internals/DateFormatterCache.swift b/Sources/Time/Internals/DateFormatterCache.swift index 768123d..9994db3 100644 --- a/Sources/Time/Internals/DateFormatterCache.swift +++ b/Sources/Time/Internals/DateFormatterCache.swift @@ -39,38 +39,37 @@ private class DateFormatterCache { static let shared = DateFormatterCache() - #warning("TODO: better locking primitive?") - private let queue = DispatchQueue(label: "DateFormatterCache") - + private let lock = NSLock() private var formatters = Dictionary() private init() { } - private func onqueue_nonupdatingFormatter(for key: DateFormatter.Key) -> DateFormatter { - if let existing = formatters[key] { return existing } + func formatter(for key: DateFormatter.Key) -> DateFormatter { + lock.lock() + let returnValue: DateFormatter - let formatter = DateFormatter() - formatter.locale = key.region.locale - formatter.calendar = key.region.calendar - formatter.timeZone = key.region.timeZone - switch key.configuration { - case .template(let template): - formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: key.region.locale) - case .raw(let format): - formatter.dateFormat = format - case .styles(let date, let time): - formatter.dateStyle = date - formatter.timeStyle = time + if let existing = formatters[key] { + returnValue = existing + } else { + let formatter = DateFormatter() + formatter.locale = key.region.locale + formatter.calendar = key.region.calendar + formatter.timeZone = key.region.timeZone + switch key.configuration { + case .template(let template): + formatter.dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: key.region.locale) + case .raw(let format): + formatter.dateFormat = format + case .styles(let date, let time): + formatter.dateStyle = date + formatter.timeStyle = time + } + formatters[key] = formatter + returnValue = formatter } - formatters[key] = formatter - return formatter - } - - func formatter(for key: DateFormatter.Key) -> DateFormatter { - return queue.sync { - return onqueue_nonupdatingFormatter(for: key) - } + lock.unlock() + return returnValue } } diff --git a/Sources/Time/Internals/SimpleCache.swift b/Sources/Time/Internals/SimpleCache.swift index 3fc13d9..0e2474f 100644 --- a/Sources/Time/Internals/SimpleCache.swift +++ b/Sources/Time/Internals/SimpleCache.swift @@ -34,8 +34,6 @@ extension TimeZone { private class SimpleCache { private var storage = Dictionary() - - #warning("TODO: better synchronization primitive?") private let lock = NSLock() init() { } diff --git a/Sources/Time/Internals/Snapshot.swift b/Sources/Time/Internals/Snapshot.swift index 83bb69e..fc4ff28 100644 --- a/Sources/Time/Internals/Snapshot.swift +++ b/Sources/Time/Internals/Snapshot.swift @@ -89,8 +89,6 @@ private class Snapshot { private var _snapshot: T? private var observationToken: NSObjectProtocol? - - #warning("TODO: better synchronization primitive?") private let lock = NSLock() var snapshot: T {