123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- import Foundation
- private protocol Lock {
- func lock()
- func unlock()
- }
- extension Lock {
-
-
-
-
-
- func around<T>(_ closure: () -> T) -> T {
- lock(); defer { unlock() }
- return closure()
- }
-
-
-
- func around(_ closure: () -> Void) {
- lock(); defer { unlock() }
- closure()
- }
- }
- #if os(Linux) || os(Windows)
- extension NSLock: Lock {}
- #endif
- #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
- final class UnfairLock: Lock {
- 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()
- }
- fileprivate func lock() {
- os_unfair_lock_lock(unfairLock)
- }
- fileprivate func unlock() {
- os_unfair_lock_unlock(unfairLock)
- }
- }
- #endif
- @propertyWrapper
- @dynamicMemberLookup
- final class Protected<T> {
- #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
- private let lock = UnfairLock()
- #elseif os(Linux) || os(Windows)
- private let lock = NSLock()
- #endif
- private var value: T
- init(_ value: T) {
- self.value = value
- }
-
- var wrappedValue: T {
- get { lock.around { value } }
- set { lock.around { value = newValue } }
- }
- var projectedValue: Protected<T> { self }
- init(wrappedValue: T) {
- value = wrappedValue
- }
-
-
-
-
-
- func read<U>(_ closure: (T) -> U) -> U {
- lock.around { closure(self.value) }
- }
-
-
-
-
-
- @discardableResult
- func write<U>(_ closure: (inout T) -> U) -> U {
- lock.around { closure(&self.value) }
- }
- subscript<Property>(dynamicMember keyPath: WritableKeyPath<T, Property>) -> Property {
- get { lock.around { value[keyPath: keyPath] } }
- set { lock.around { value[keyPath: keyPath] = newValue } }
- }
- }
- extension Protected where T: RangeReplaceableCollection {
-
-
-
- func append(_ newElement: T.Element) {
- write { (ward: inout T) in
- ward.append(newElement)
- }
- }
-
-
-
- func append<S: Sequence>(contentsOf newElements: S) where S.Element == T.Element {
- write { (ward: inout T) in
- ward.append(contentsOf: newElements)
- }
- }
-
-
-
- func append<C: Collection>(contentsOf newElements: C) where C.Element == T.Element {
- write { (ward: inout T) in
- ward.append(contentsOf: newElements)
- }
- }
- }
- extension Protected where T == Data? {
-
-
-
- func append(_ data: Data) {
- write { (ward: inout T) in
- ward?.append(data)
- }
- }
- }
- extension Protected where T == Request.MutableState {
-
-
-
-
-
- func attemptToTransitionTo(_ state: Request.State) -> Bool {
- lock.around {
- guard value.state.canTransitionTo(state) else { return false }
- value.state = state
- return true
- }
- }
-
-
-
- func withState(perform: (Request.State) -> Void) {
- lock.around { perform(value.state) }
- }
- }
|