Skip to content

Commit

Permalink
Address locking primitive todos
Browse files Browse the repository at this point in the history
NSLock is fine for now. Perhaps in the future something better will be readily available.
  • Loading branch information
davedelong committed Feb 15, 2024
1 parent ced2dcd commit 7563ff7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Sources/Time/8-Formatting/FormatTemplates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import Foundation

#warning("TODO: document these")

public struct Template<F>: Format {
internal let template: String
internal init(_ template: String) {
Expand Down
49 changes: 24 additions & 25 deletions Sources/Time/Internals/DateFormatterCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<DateFormatter.Key, DateFormatter>()

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
}

}
2 changes: 0 additions & 2 deletions Sources/Time/Internals/SimpleCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ extension TimeZone {
private class SimpleCache<Key: Hashable, T> {

private var storage = Dictionary<Key, T>()

#warning("TODO: better synchronization primitive?")
private let lock = NSLock()

init() { }
Expand Down
2 changes: 0 additions & 2 deletions Sources/Time/Internals/Snapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ private class Snapshot<T> {

private var _snapshot: T?
private var observationToken: NSObjectProtocol?

#warning("TODO: better synchronization primitive?")
private let lock = NSLock()

var snapshot: T {
Expand Down

0 comments on commit 7563ff7

Please sign in to comment.