Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add thread safety to Clock #110

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Kronos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
930B39DD2051E6D300360BA2 /* TimeStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930B39DC2051E6D300360BA2 /* TimeStorage.swift */; };
930B39E02051F26500360BA2 /* TimeStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 930B39DE2051F25300360BA2 /* TimeStorageTests.swift */; };
C201748E1BD5509D00E4FE18 /* Kronos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C20174831BD5509D00E4FE18 /* Kronos.framework */; };
E5E7A7702BFE559F0025D9D0 /* UnfairLock.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5E7A76F2BFE559F0025D9D0 /* UnfairLock.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -59,6 +60,7 @@
C2C036D41C2B180D003FB853 /* UniversalFramework_Base.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = UniversalFramework_Base.xcconfig; sourceTree = "<group>"; };
C2C036D51C2B180D003FB853 /* UniversalFramework_Framework.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = UniversalFramework_Framework.xcconfig; sourceTree = "<group>"; };
C2C036D61C2B180D003FB853 /* UniversalFramework_Test.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; path = UniversalFramework_Test.xcconfig; sourceTree = "<group>"; };
E5E7A76F2BFE559F0025D9D0 /* UnfairLock.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnfairLock.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -125,6 +127,7 @@
26447D7B1D6E54D400159BEE /* TimeFreeze.swift */,
930B39DC2051E6D300360BA2 /* TimeStorage.swift */,
5DB5A05F2BAAF67D0069CCF9 /* PrivacyInfo.xcprivacy */,
E5E7A76F2BFE559F0025D9D0 /* UnfairLock.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -247,6 +250,7 @@
26447D7E1D6E54D400159BEE /* InternetAddress.swift in Sources */,
26447D811D6E54D400159BEE /* NTPClient.swift in Sources */,
26447D7F1D6E54D400159BEE /* Data+Bytes.swift in Sources */,
E5E7A7702BFE559F0025D9D0 /* UnfairLock.swift in Sources */,
26447D841D6E54D400159BEE /* TimeFreeze.swift in Sources */,
26447D821D6E54D400159BEE /* NTPPacket.swift in Sources */,
26447D801D6E54D400159BEE /* NSTimer+ClosureKit.swift in Sources */,
Expand Down
15 changes: 13 additions & 2 deletions Sources/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,20 @@ public typealias AnnotatedTime = (
/// print(Clock.now)
/// ```
public struct Clock {
private static let lock = UnfairLock()

private static var _stableTime: TimeFreeze?
private static var stableTime: TimeFreeze? {
didSet {
self.storage.stableTime = self.stableTime
get {
self.lock.around {
return self._stableTime
}
}
set {
self.lock.around {
self._stableTime = newValue
self.storage.stableTime = newValue
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions Sources/UnfairLock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

final class UnfairLock {
private let unfairLock: os_unfair_lock_t

init() {
unfairLock = .allocate(capacity: 1)
unfairLock.initialize(to: os_unfair_lock())
}

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

func around<T>(_ block: () -> T) -> T {
lock()
defer { unlock() }
return block()
}

private func lock() {
os_unfair_lock_lock(unfairLock)
}

private func unlock() {
os_unfair_lock_unlock(unfairLock)
}
}
Loading