Skip to content

Commit

Permalink
Add Atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed May 6, 2024
1 parent 43d63c6 commit f098b45
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Pulse.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
0C11EAE22971EBEA0059786A /* MockStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF0D5B0296F189500EED9D4 /* MockStore.swift */; };
0C11EAE72971EBED0059786A /* MockStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF0D5B0296F189500EED9D4 /* MockStore.swift */; };
0C11EAE92971EBEE0059786A /* MockTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0CF0D5AF296F189500EED9D4 /* MockTask.swift */; };
0C1D52412BE99169006A0EB7 /* Atomic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C1D52402BE99169006A0EB7 /* Atomic.swift */; };
0C20F3E42A2CE9D900F2F67D /* Keychain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C20F3E32A2CE9D900F2F67D /* Keychain.swift */; };
0C2343152978B6B900AE7CE6 /* ConsoleSearchSuggestionsService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C2343142978B6B900AE7CE6 /* ConsoleSearchSuggestionsService.swift */; };
0C2720892AF832C6005DDB1C /* StatusLabelViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C2720882AF832C6005DDB1C /* StatusLabelViewModel.swift */; };
Expand Down Expand Up @@ -508,6 +509,7 @@
0C11EAC12971E9B30059786A /* PulsePerformanceTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = PulsePerformanceTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
0C11EACB2971E9CE0059786A /* TextRendererPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextRendererPerformanceTests.swift; sourceTree = "<group>"; };
0C1221E525CE09C100B81D7A /* URLSessionAutomatedIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionAutomatedIntegration.swift; sourceTree = "<group>"; };
0C1D52402BE99169006A0EB7 /* Atomic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Atomic.swift; sourceTree = "<group>"; };
0C20F3E32A2CE9D900F2F67D /* Keychain.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Keychain.swift; sourceTree = "<group>"; };
0C2343142978B6B900AE7CE6 /* ConsoleSearchSuggestionsService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConsoleSearchSuggestionsService.swift; sourceTree = "<group>"; };
0C25FB6E25CDDCAB00A895C2 /* URLSessionManualIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionManualIntegration.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1153,6 +1155,7 @@
0CF0D4A7296F14CF00EED9D4 /* PulseDocument.swift */,
0CF0D4A9296F14CF00EED9D4 /* Regex.swift */,
0C20F3E32A2CE9D900F2F67D /* Keychain.swift */,
0C1D52402BE99169006A0EB7 /* Atomic.swift */,
);
path = Helpers;
sourceTree = "<group>";
Expand Down Expand Up @@ -1965,6 +1968,7 @@
0CF0D54F296F15F200EED9D4 /* NetworkLogger+Redacting.swift in Sources */,
0CF0D55C296F15FA00EED9D4 /* LoggerStore+Info.swift in Sources */,
0CF0D560296F15FA00EED9D4 /* LoggerStore+Configuration.swift in Sources */,
0C1D52412BE99169006A0EB7 /* Atomic.swift in Sources */,
0CF0D561296F15FD00EED9D4 /* LoggerSession.swift in Sources */,
0CF0D558296F15F600EED9D4 /* RemoteLogger-Protocol.swift in Sources */,
0C20F3E42A2CE9D900F2F67D /* Keychain.swift in Sources */,
Expand Down
40 changes: 40 additions & 0 deletions Sources/Pulse/Helpers/Atomic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// The MIT License (MIT)
//
// Copyright (c) 2020-2024 Alexander Grebenyuk (github.com/kean).

import Foundation

final class Atomic<T>: @unchecked Sendable {
private var _value: T
private let lock: os_unfair_lock_t

init(value: T) {
self._value = value
self.lock = .allocate(capacity: 1)
self.lock.initialize(to: os_unfair_lock())
}

deinit {
lock.deinitialize(count: 1)
lock.deallocate()
}

var value: T {
get {
os_unfair_lock_lock(lock)
defer { os_unfair_lock_unlock(lock) }
return _value
}
set {
os_unfair_lock_lock(lock)
defer { os_unfair_lock_unlock(lock) }
_value = newValue
}
}

func withLock<U>(_ closure: (inout T) -> U) -> U {
os_unfair_lock_lock(lock)
defer { os_unfair_lock_unlock(lock) }
return closure(&_value)
}
}
10 changes: 8 additions & 2 deletions Sources/Pulse/LoggerStore/LoggerStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ public final class LoggerStore: @unchecked Sendable, Identifiable {
/// You can replace the default store with a custom one. If you replace the
/// shared store, it automatically gets registered as the default store
/// for ``RemoteLogger``.
public static var shared = LoggerStore.makeDefault() {
didSet { register(store: shared) }
public static var shared: LoggerStore {
get { _shared.value }
set {
_shared.value = newValue
register(store: newValue)
}
}

private static let _shared = Atomic(value: LoggerStore.makeDefault())

private static func register(store: LoggerStore) {
guard Thread.isMainThread else {
return DispatchQueue.main.async { register(store: store) }
Expand Down

0 comments on commit f098b45

Please sign in to comment.