Skip to content

Instantly share code, notes, and snippets.

View LK-Simon's full-sized avatar

Simon J Stuart LK-Simon

  • England, United Kingdom
View GitHub Profile
@LK-Simon
LK-Simon / ThreadSafePropertyWrapper.swift
Last active July 13, 2022 10:51
Swift Property Wrapper to make a Var Thread-Safe using an enforced Semaphore Lock
import Foundation
@propertyWrapper
public struct ThreadSafe<T> {
public var lock = DispatchSemaphore(value: 1)
private var value: T
public var wrappedValue: T {
get {
lock.wait()
@LK-Simon
LK-Simon / Observable.swift
Last active July 11, 2022 14:40
Generic Observable Class and Thread types, elegant and protocol-conformance-driven
import Foundation
public protocol Observable {
/// Registers an Observer against this Observable Type
func addObserver<TObservationProtocol: AnyObject>(_ observer: TObservationProtocol)
/// Removes an Observer from this Observable Type
func removeObserver<TObservationProtocol: AnyObject>(_ observer: TObservationProtocol)
}
@LK-Simon
LK-Simon / MachTimer.swift
Created June 19, 2022 14:48
High Precision "Mach Timer" for MacOS and iOS (Swift implementation)
import Foundation
enum MachTimerError: Error {
case TimebaseInfoError // Only thrown where we cannot initialize the Mach Timer (shouldn't ever happen though)
}
struct MachTimer {
// We need to know whether or not the Timer is still running or Stopped at the point of requesting a Result
enum MachTimerState {
case NotRunning