Skip to content

Commit

Permalink
rename _mutex to mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
niil-ohlin committed Mar 30, 2020
1 parent 90c9e79 commit 0397903
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 99 deletions.
22 changes: 11 additions & 11 deletions Flow/Callbacker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ public final class Callbacker<Value> {
}

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
Expand All @@ -45,8 +45,8 @@ public final class Callbacker<Value> {
/// 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()

Expand All @@ -62,8 +62,8 @@ public final class Callbacker<Value> {
}

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:
Expand All @@ -81,9 +81,9 @@ public final class Callbacker<Value> {

/// 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
Expand Down
32 changes: 16 additions & 16 deletions Flow/Disposable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?()
}
}
Expand All @@ -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<S: Sequence>(_ 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)
}
}
Expand Down
16 changes: 8 additions & 8 deletions Flow/Future.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class Future<Value> {

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 {
Expand Down Expand Up @@ -107,7 +107,7 @@ public final class Future<Value> {
try onResult(completion, Mover(shouldClone: true))
}
}
_mutex.initialize()
mutex.initialize()

scheduler.async {
do {
Expand Down Expand Up @@ -143,13 +143,13 @@ public final class Future<Value> {

state = .completed(result)
clone = { Future(result: result) }
_mutex.initialize()
mutex.initialize()
}

deinit {
OSAtomicDecrement32(&futureUnitTestAliveCount)
memPrint("Future deinit", futureUnitTestAliveCount)
_mutex.deinitialize()
mutex.deinitialize()
}
}

Expand Down Expand Up @@ -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<Value>) {
Expand Down
32 changes: 16 additions & 16 deletions Flow/FutureQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public final class FutureQueue<Resource> {
private let queueScheduler: Scheduler
private var _closedError: Error?
private let isEmptyCallbacker = Callbacker<Bool>()
private var _mutex = pthread_mutex_t()
private var mutex = pthread_mutex_t()

// enqueued items.
private var items: [Executable] = [] {
Expand Down Expand Up @@ -61,9 +61,9 @@ public extension FutureQueue {
return Future { completion in
let item = QueueItem<Output>(operation: operation, completion: completion)

self._mutex.lock()
self.mutex.lock()
self.items.append(item)
self._mutex.unlock()
self.mutex.unlock()

self.executeNextItem()

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -217,25 +217,25 @@ private final class QueueItem<Output>: Executable {
private let completion: (Result<Output>) -> ()
private weak var future: Future<Output>?
private var hasBeenCancelled = false
private var _mutex = pthread_mutex_t()
private var mutex = pthread_mutex_t()

init(operation: @escaping () throws -> Future<Output>, completion: @escaping (Result<Output>) -> ()) {
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<Output>)) {
lock()
Expand Down
28 changes: 14 additions & 14 deletions Flow/Locking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -99,16 +99,16 @@ final class StateAndCallback<Value, State>: 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()
}

Expand All @@ -118,27 +118,27 @@ final class StateAndCallback<Value, State>: Disposable {
}

func lock() {
_mutex.lock()
mutex.lock()
}

func unlock() {
_mutex.unlock()
mutex.unlock()
}

@discardableResult
func protect<T>(_ 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() }
}

Expand Down
Loading

0 comments on commit 0397903

Please sign in to comment.