From 0397903ff88bee12c9394f8d72ac88496c175df0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niil=20=C3=96hlin?= Date: Mon, 30 Mar 2020 12:46:10 +0200 Subject: [PATCH] rename _mutex to mutex --- Flow/Callbacker.swift | 22 +++++++++++----------- Flow/Disposable.swift | 32 ++++++++++++++++---------------- Flow/Future.swift | 16 ++++++++-------- Flow/FutureQueue.swift | 32 ++++++++++++++++---------------- Flow/Locking.swift | 28 ++++++++++++++-------------- Flow/OrderedCallbacker.swift | 22 +++++++++++----------- Flow/Signal+Construction.swift | 20 ++++++++++---------- Flow/Signal+Scheduling.swift | 26 +++++++++++++------------- 8 files changed, 99 insertions(+), 99 deletions(-) diff --git a/Flow/Callbacker.swift b/Flow/Callbacker.swift index c29209c..99bfccf 100644 --- a/Flow/Callbacker.swift +++ b/Flow/Callbacker.swift @@ -20,20 +20,20 @@ public final class Callbacker { } private var callbacks = Callbacks.none - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() public init() { - _mutex.initialize() + mutex.initialize() } deinit { - _mutex.deinitialize() + mutex.deinitialize() } /// - Returns: True if no callbacks has been registered. public var isEmpty: Bool { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } switch callbacks { case .none: return true @@ -45,8 +45,8 @@ public final class Callbacker { /// Register a callback to be called when `callAll` is executed. /// - Returns: A `Disposable` to be disposed to unregister the callback. public func addCallback(_ callback: @escaping (Value) -> Void) -> Disposable { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } let key = generateKey() @@ -62,8 +62,8 @@ public final class Callbacker { } return NoLockKeyDisposer(key) { key in - self._mutex.lock() - defer { self._mutex.unlock() } + self.mutex.lock() + defer { self.mutex.unlock() } switch self.callbacks { case .single(let singleKey, _) where singleKey == key: @@ -81,9 +81,9 @@ public final class Callbacker { /// Will call all registered callbacks with `value` public func callAll(with value: Value) { - _mutex.lock() + mutex.lock() let callbacks = self.callbacks - _mutex.unlock() + mutex.unlock() switch callbacks { case .none: break diff --git a/Flow/Disposable.swift b/Flow/Disposable.swift index 71c4149..5949b22 100644 --- a/Flow/Disposable.swift +++ b/Flow/Disposable.swift @@ -28,24 +28,24 @@ public struct NilDisposer: Disposable { /// - Note: Is thread safe and reentrant (dispose callback could call itself) public final class Disposer: Disposable { private var disposer: (() -> ())? - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() /// Pass a closure to be called when being disposed public init(_ disposer: @escaping () -> () = {}) { self.disposer = disposer - _mutex.initialize() + mutex.initialize() } deinit { dispose() - _mutex.deinitialize() + mutex.deinitialize() } public func dispose() { - _mutex.lock() + mutex.lock() let disposer = self.disposer self.disposer = nil - _mutex.unlock() + mutex.unlock() disposer?() } } @@ -57,51 +57,51 @@ public final class Disposer: Disposable { /// - Note: New disposables could be added after a disposal. public final class DisposeBag: Disposable { private var disposables: [Disposable] - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() /// Create an empty instance public init() { self.disposables = [] - _mutex.initialize() + mutex.initialize() } /// Create an instance already containing `disposables` public init(_ disposables: S) where S.Iterator.Element == Disposable { self.disposables = Array(disposables) - _mutex.initialize() + mutex.initialize() } /// Create an instance already containing `disposables` public init(_ disposables: Disposable...) { self.disposables = disposables - _mutex.initialize() + mutex.initialize() } deinit { dispose() - _mutex.deinitialize() + mutex.deinitialize() } /// Returns true if there is currently no disposables to dispose. public var isEmpty: Bool { - _mutex.lock() + mutex.lock() let isEmpty = disposables.isEmpty - _mutex.unlock() + mutex.unlock() return isEmpty } public func dispose() { - _mutex.lock() + mutex.lock() let disposables = self.disposables // make sure to make a copy in the case any call to dispose will recursivaly call us back. self.disposables = [] - _mutex.unlock() + mutex.unlock() for disposable in disposables { disposable.dispose() } } /// Add `disposable` to `self` public func add(_ disposable: Disposable) { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } disposables.append(disposable) } } diff --git a/Flow/Future.swift b/Flow/Future.swift index ce6d755..82bf33f 100644 --- a/Flow/Future.swift +++ b/Flow/Future.swift @@ -57,7 +57,7 @@ public final class Future { private var state: State private let clone: () -> Future - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() /// Helper used to move external futures inside `Future.init`'s `onComplete` closure. Needed for repetition to work properly. public struct Mover { @@ -107,7 +107,7 @@ public final class Future { try onResult(completion, Mover(shouldClone: true)) } } - _mutex.initialize() + mutex.initialize() scheduler.async { do { @@ -143,13 +143,13 @@ public final class Future { state = .completed(result) clone = { Future(result: result) } - _mutex.initialize() + mutex.initialize() } deinit { OSAtomicDecrement32(&futureUnitTestAliveCount) memPrint("Future deinit", futureUnitTestAliveCount) - _mutex.deinitialize() + mutex.deinitialize() } } @@ -328,17 +328,17 @@ func memPrint(_ str: String, _ count: Int32) { private extension Future { private var protectedState: State { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } return state } func lock() { - _mutex.lock() + mutex.lock() } func unlock() { - _mutex.unlock() + mutex.unlock() } func completeWithResult(_ result: Result) { diff --git a/Flow/FutureQueue.swift b/Flow/FutureQueue.swift index eb5b6ff..145ed53 100644 --- a/Flow/FutureQueue.swift +++ b/Flow/FutureQueue.swift @@ -18,7 +18,7 @@ public final class FutureQueue { private let queueScheduler: Scheduler private var _closedError: Error? private let isEmptyCallbacker = Callbacker() - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() // enqueued items. private var items: [Executable] = [] { @@ -61,9 +61,9 @@ public extension FutureQueue { return Future { completion in let item = QueueItem(operation: operation, completion: completion) - self._mutex.lock() + self.mutex.lock() self.items.append(item) - self._mutex.unlock() + self.mutex.unlock() self.executeNextItem() @@ -119,8 +119,8 @@ public extension FutureQueue { public extension FutureQueue { /// Do we have any enqueued operations? var isEmpty: Bool { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } return items.isEmpty } @@ -166,20 +166,20 @@ public extension FutureQueue { /// The error passed to `abortQueuedExecutionWithError()` if called with `shouldCloseQueue` as true. var closedError: Error? { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } return _closedError } } private extension FutureQueue { - func lock() { _mutex.lock() } - func unlock() { _mutex.unlock() } + func lock() { mutex.lock() } + func unlock() { mutex.unlock() } func removeItem(_ item: Executable) { - _mutex.lock() + mutex.lock() _ = items.firstIndex { $0 === item }.map { items.remove(at: $0) } - _mutex.unlock() + mutex.unlock() } func executeNextItem() { @@ -217,25 +217,25 @@ private final class QueueItem: Executable { private let completion: (Result) -> () private weak var future: Future? private var hasBeenCancelled = false - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() init(operation: @escaping () throws -> Future, completion: @escaping (Result) -> ()) { self.completion = completion self.operation = operation - _mutex.initialize() + mutex.initialize() OSAtomicIncrement32(&queueItemUnitTestAliveCount) memPrint("Queue Item init", queueItemUnitTestAliveCount) } deinit { - _mutex.deinitialize() + mutex.deinitialize() OSAtomicDecrement32(&queueItemUnitTestAliveCount) memPrint("Queue Item deinit", queueItemUnitTestAliveCount) } - private func lock() { _mutex.lock() } - private func unlock() { _mutex.unlock() } + private func lock() { mutex.lock() } + private func unlock() { mutex.unlock() } private func complete(_ result: (Result)) { lock() diff --git a/Flow/Locking.swift b/Flow/Locking.swift index 874badf..c045cd1 100644 --- a/Flow/Locking.swift +++ b/Flow/Locking.swift @@ -10,24 +10,24 @@ import Foundation /// A reference wrapper around a POSIX thread mutex public final class Mutex { - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() public init() { - _mutex.initialize() + mutex.initialize() } deinit { - _mutex.deinitialize() + mutex.deinitialize() } /// Attempt to acquire the lock, blocking a thread’s execution until the lock can be acquired. public func lock() { - _mutex.lock() + mutex.lock() } /// Releases a previously acquired lock. public func unlock() { - _mutex.unlock() + mutex.unlock() } } @@ -99,16 +99,16 @@ final class StateAndCallback: Disposable { var callback: ((Value) -> ())? var val: State fileprivate var disposables = [Disposable]() - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() init(state: State, callback: @escaping (Value) -> ()) { val = state self.callback = callback - _mutex.initialize() + mutex.initialize() } deinit { - _mutex.deinitialize() + mutex.deinitialize() dispose() } @@ -118,27 +118,27 @@ final class StateAndCallback: Disposable { } func lock() { - _mutex.lock() + mutex.lock() } func unlock() { - _mutex.unlock() + mutex.unlock() } @discardableResult func protect(_ block: () throws -> T) rethrows -> T { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } return try block() } func dispose() { - _mutex.lock() + mutex.lock() let disposables = self.disposables // make sure to make a copy in the case any call to dispose will recursivaly call us back. callback = nil exclusiveQueue = [] self.disposables = [] - _mutex.unlock() + mutex.unlock() for disposable in disposables { disposable.dispose() } } diff --git a/Flow/OrderedCallbacker.swift b/Flow/OrderedCallbacker.swift index 3d557ab..075db90 100644 --- a/Flow/OrderedCallbacker.swift +++ b/Flow/OrderedCallbacker.swift @@ -14,21 +14,21 @@ import Foundation /// - Note: Is thread safe. public final class OrderedCallbacker { private var callbacks: [Key: (OrderedValue, (CallbackValue) -> Future<()>)] = [:] - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() public init() { - _mutex.initialize() + mutex.initialize() } deinit { - _mutex.deinitialize() + mutex.deinitialize() } /// - Returns: True if no callbacks has been registered. public var isEmpty: Bool { - _mutex.lock() + mutex.lock() let isEmpty = callbacks.isEmpty - _mutex.unlock() + mutex.unlock() return isEmpty } @@ -37,14 +37,14 @@ public final class OrderedCallbacker { /// - Parameter orderedValue: The value used to order this callback /// - Returns: A `Disposable` to be disposed to unregister the callback. public func addCallback(_ callback: @escaping (CallbackValue) -> Future<()>, orderedBy orderedValue: OrderedValue) -> Disposable { - _mutex.lock() - defer { _mutex.unlock() } + mutex.lock() + defer { mutex.unlock() } let key = generateKey() callbacks[key] = (orderedValue, callback) return Disposer { - self._mutex.lock() + self.mutex.lock() self.callbacks[key] = nil - self._mutex.unlock() + self.mutex.unlock() } } @@ -52,9 +52,9 @@ public final class OrderedCallbacker { /// - Returns: A `Future` that will complete when all callbacks has been called. @discardableResult public func callAll(with value: CallbackValue, isOrderedBefore: (OrderedValue, OrderedValue) -> Bool) -> Future<()> { - _mutex.lock() + mutex.lock() let sortedCallbacks = callbacks.values.sorted { isOrderedBefore($0.0, $1.0) }.map { $1 } - _mutex.unlock() + mutex.unlock() return sortedCallbacks.mapToFuture { $0(value) }.toVoid() } } diff --git a/Flow/Signal+Construction.swift b/Flow/Signal+Construction.swift index 7230f4e..0bdfc15 100644 --- a/Flow/Signal+Construction.swift +++ b/Flow/Signal+Construction.swift @@ -112,27 +112,27 @@ private final class CallbackState: Disposable { private var shared: SharedState? let sharedKey: Key - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() init(shared: SharedState? = nil, getValue: (() -> Value)?, callback: @escaping (EventType) -> Void) { self.shared = shared self.sharedKey = shared == nil ? 0 : generateKey() self.getValue = getValue self.callback = callback - _mutex.initialize() + mutex.initialize() } deinit { - _mutex.deinitialize() + mutex.deinitialize() shared?.remove(key: sharedKey) } func lock() { - _mutex.lock() + mutex.lock() } func unlock() { - _mutex.unlock() + mutex.unlock() } // For efficiency `Self` could also also behave as a `NoLockKeyDisposer``, saving us an allocation for each listener. @@ -291,7 +291,7 @@ private final class CallbackState: Disposable { /// Helper to implement sharing of a single `onEvent` if more than one listner, see `SignalOption.shared` final class SharedState { private let getValue: (() -> Value)? - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() typealias Callback = (EventType) -> Void var firstCallback: (key: Key, value: Callback)? @@ -301,19 +301,19 @@ final class SharedState { init(getValue: (() -> Value)? = nil) { self.getValue = getValue - _mutex.initialize() + mutex.initialize() } deinit { - _mutex.deinitialize() + mutex.deinitialize() } func lock() { - _mutex.lock() + mutex.lock() } func unlock() { - _mutex.unlock() + mutex.unlock() } func remove(key: Key) { diff --git a/Flow/Signal+Scheduling.swift b/Flow/Signal+Scheduling.swift index 2209a7d..6db5972 100644 --- a/Flow/Signal+Scheduling.swift +++ b/Flow/Signal+Scheduling.swift @@ -119,7 +119,7 @@ internal extension CoreSignal { // Using custom Disposable instead of DisposeBag for efficiency (less allocations) private final class OnEventTypeDisposer: Disposable { private var disposable: Disposable? - private var _mutex = pthread_mutex_t() + private var mutex = pthread_mutex_t() private let scheduler: Scheduler private var callback: ((EventType) -> Void)? @@ -127,41 +127,41 @@ private final class OnEventTypeDisposer: Disposable { init(on scheduler: Scheduler, callback: @escaping (EventType) -> Void, onEventType: @escaping (@escaping (EventType) -> Void) -> Disposable) { self.scheduler = scheduler self.callback = callback - _mutex.initialize() + mutex.initialize() let disposable = onEventType { [weak self] in self?.handleEventType($0) } - _mutex.lock() + mutex.lock() if self.callback == nil { disposable.dispose() } else { self.disposable = disposable } - _mutex.unlock() + mutex.unlock() } deinit { dispose() - _mutex.deinitialize() + mutex.deinitialize() } public func dispose() { - _mutex.lock() + mutex.lock() let disposable = self.disposable self.disposable = nil callback = nil - _mutex.unlock() + mutex.unlock() disposable?.dispose() } func handleEventType(_ eventType: EventType) { - _mutex.lock() + mutex.lock() guard let callback = self.callback else { - return _mutex.unlock() + return mutex.unlock() } - _mutex.unlock() + mutex.unlock() if scheduler.isImmediate { validate(eventType: eventType) @@ -176,14 +176,14 @@ private final class OnEventTypeDisposer: Disposable { scheduler.async { [weak self] in guard let `self` = self else { return } // At the time we are scheduled, we might already been disposed - self._mutex.lock() + self.mutex.lock() guard let callback = self.callback else { - return self._mutex.unlock() + return self.mutex.unlock() } self.validate(eventType: eventType) - self._mutex.unlock() + self.mutex.unlock() callback(eventType) if case .event(.end) = eventType { self.dispose()