Skip to content

Commit c8d4a96

Browse files
committed
Remove warnings
1 parent e9da9fc commit c8d4a96

12 files changed

+178
-130
lines changed

Flow/Callbacker.swift

+13-11
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,22 @@ public final class Callbacker<Value> {
2121

2222
private var callbacks = Callbacks.none
2323
private var _mutex = pthread_mutex_t()
24-
private var mutex: PThreadMutex { return PThreadMutex(&_mutex) }
24+
private func withMutex<T>(_ body: (PThreadMutex) throws -> T) rethrows -> T {
25+
try withUnsafeMutablePointer(to: &_mutex, body)
26+
}
2527

2628
public init() {
27-
mutex.initialize()
29+
withMutex { $0.initialize() }
2830
}
2931

3032
deinit {
31-
mutex.deinitialize()
33+
withMutex { $0.deinitialize() }
3234
}
3335

3436
/// - Returns: True if no callbacks has been registered.
3537
public var isEmpty: Bool {
36-
mutex.lock()
37-
defer { mutex.unlock() }
38+
withMutex { $0.lock() }
39+
defer { withMutex { $0.unlock() } }
3840

3941
switch callbacks {
4042
case .none: return true
@@ -46,8 +48,8 @@ public final class Callbacker<Value> {
4648
/// Register a callback to be called when `callAll` is executed.
4749
/// - Returns: A `Disposable` to be disposed to unregister the callback.
4850
public func addCallback(_ callback: @escaping (Value) -> Void) -> Disposable {
49-
mutex.lock()
50-
defer { mutex.unlock() }
51+
withMutex { $0.lock() }
52+
defer { withMutex { $0.unlock() } }
5153

5254
let key = generateKey()
5355

@@ -63,8 +65,8 @@ public final class Callbacker<Value> {
6365
}
6466

6567
return NoLockKeyDisposer(key) { key in
66-
self.mutex.lock()
67-
defer { self.mutex.unlock() }
68+
self.withMutex { $0.lock() }
69+
defer { self.withMutex { $0.unlock() } }
6870

6971
switch self.callbacks {
7072
case .single(let singleKey, _) where singleKey == key:
@@ -82,9 +84,9 @@ public final class Callbacker<Value> {
8284

8385
/// Will call all registered callbacks with `value`
8486
public func callAll(with value: Value) {
85-
mutex.lock()
87+
withMutex { $0.lock() }
8688
let callbacks = self.callbacks
87-
mutex.unlock()
89+
withMutex { $0.unlock() }
8890

8991
switch callbacks {
9092
case .none: break

Flow/Disposable.swift

+19-15
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,26 @@ public struct NilDisposer: Disposable {
2929
public final class Disposer: Disposable {
3030
private var disposer: (() -> ())?
3131
private var _mutex = pthread_mutex_t()
32-
private var mutex: PThreadMutex { return PThreadMutex(&_mutex) }
32+
private func withMutex<T>(_ body: (PThreadMutex) throws -> T) rethrows -> T {
33+
try withUnsafeMutablePointer(to: &_mutex, body)
34+
}
3335

3436
/// Pass a closure to be called when being disposed
3537
public init(_ disposer: @escaping () -> () = {}) {
3638
self.disposer = disposer
37-
mutex.initialize()
39+
withMutex { $0.initialize() }
3840
}
3941

4042
deinit {
4143
dispose()
42-
mutex.deinitialize()
44+
withMutex { $0.deinitialize() }
4345
}
4446

4547
public func dispose() {
46-
mutex.lock()
48+
withMutex { $0.lock() }
4749
let disposer = self.disposer
4850
self.disposer = nil
49-
mutex.unlock()
51+
withMutex { $0.unlock() }
5052
disposer?()
5153
}
5254
}
@@ -59,48 +61,50 @@ public final class Disposer: Disposable {
5961
public final class DisposeBag: Disposable {
6062
private var disposables: [Disposable]
6163
private var _mutex = pthread_mutex_t()
62-
private var mutex: PThreadMutex { return PThreadMutex(&_mutex) }
64+
private func withMutex<T>(_ body: (PThreadMutex) throws -> T) rethrows -> T {
65+
try withUnsafeMutablePointer(to: &_mutex, body)
66+
}
6367

6468
/// Create an empty instance
6569
public init() {
6670
self.disposables = []
67-
mutex.initialize()
71+
withMutex { $0.initialize() }
6872
}
6973

7074
/// Create an instance already containing `disposables`
7175
public init<S: Sequence>(_ disposables: S) where S.Iterator.Element == Disposable {
7276
self.disposables = Array(disposables)
73-
mutex.initialize()
77+
withMutex { $0.initialize() }
7478
}
7579

7680
/// Create an instance already containing `disposables`
7781
public init(_ disposables: Disposable...) {
7882
self.disposables = disposables
79-
mutex.initialize()
83+
withMutex { $0.initialize() }
8084
}
8185

8286
deinit {
8387
dispose()
84-
mutex.deinitialize()
88+
withMutex { $0.deinitialize() }
8589
}
8690

8791
/// Returns true if there is currently no disposables to dispose.
8892
public var isEmpty: Bool {
89-
return mutex.protect { disposables.isEmpty }
93+
return withMutex { $0.protect { disposables.isEmpty } }
9094
}
9195

9296
public func dispose() {
93-
mutex.lock()
97+
withMutex { $0.lock() }
9498
let disposables = self.disposables // make sure to make a copy in the case any call to dispose will recursivaly call us back.
9599
self.disposables = []
96-
mutex.unlock()
100+
withMutex { $0.unlock() }
97101
for disposable in disposables { disposable.dispose() }
98102
}
99103

100104
/// Add `disposable` to `self`
101105
public func add(_ disposable: Disposable) {
102-
mutex.lock()
103-
defer { mutex.unlock() }
106+
withMutex { $0.lock() }
107+
defer { withMutex { $0.unlock() } }
104108
disposables.append(disposable)
105109
}
106110
}

Flow/Future+Combiners.swift

+9-5
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public func join<T>(_ futures: [Future<T>], cancelNonCompleted: Bool = true) ->
6565
var results = [T?](repeating: nil, count: futures.count)
6666
let mutex = Mutex()
6767
func onValue(_ i: Int, _ val: T) {
68-
mutex.protect {
69-
results[i] = val
70-
}
68+
mutex.lock()
69+
results[i] = val
70+
mutex.unlock()
7171
}
7272

7373
var future = futures.first!.onValue(on: .none) { onValue(0, $0) }
@@ -220,7 +220,9 @@ public final class SingleTaskPerformer<Value> {
220220

221221
mutex.unlock() // unlock while calling out as we might either recurs or always might execute at once.
222222
let singleFuture = function().always(on: .none) {
223-
self.mutex.protect { self.future = nil }
223+
self.mutex.lock()
224+
self.future = nil
225+
self.mutex.unlock()
224226
}
225227
mutex.lock()
226228

@@ -233,7 +235,9 @@ public final class SingleTaskPerformer<Value> {
233235
}
234236

235237
public var isPerforming: Bool {
236-
return mutex.protect { self.future != nil }
238+
mutex.lock()
239+
defer { mutex.unlock() }
240+
return self.future != nil
237241
}
238242
}
239243

Flow/Future.swift

+9-7
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public final class Future<Value> {
107107
try onResult(completion, Mover(shouldClone: true))
108108
}
109109
}
110-
mutex.initialize()
110+
withMutex { $0.initialize() }
111111

112112
scheduler.async {
113113
do {
@@ -143,13 +143,13 @@ public final class Future<Value> {
143143

144144
state = .completed(result)
145145
clone = { Future(result: result) }
146-
mutex.initialize()
146+
withMutex { $0.initialize() }
147147
}
148148

149149
deinit {
150150
OSAtomicDecrement32(&futureUnitTestAliveCount)
151151
memPrint("Future deinit", futureUnitTestAliveCount)
152-
mutex.deinitialize()
152+
withMutex { $0.deinitialize() }
153153
}
154154
}
155155

@@ -327,18 +327,20 @@ func memPrint(_ str: String, _ count: Int32) {
327327
}
328328

329329
private extension Future {
330-
var mutex: PThreadMutex { return PThreadMutex(&_mutex) }
330+
private func withMutex<T>(_ body: (PThreadMutex) throws -> T) rethrows -> T {
331+
try withUnsafeMutablePointer(to: &_mutex, body)
332+
}
331333

332334
private var protectedState: State {
333-
return mutex.protect { state }
335+
return withMutex { $0.protect { state } }
334336
}
335337

336338
func lock() {
337-
mutex.lock()
339+
withMutex { $0.lock() }
338340
}
339341

340342
func unlock() {
341-
mutex.unlock()
343+
withMutex { $0.unlock() }
342344
}
343345

344346
func completeWithResult(_ result: Result<Value>) {

Flow/FutureQueue.swift

+24-19
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ public extension FutureQueue {
6161
return Future { completion in
6262
let item = QueueItem<Output>(operation: operation, completion: completion)
6363

64-
self.mutex.protect {
65-
self.items.append(item)
66-
}
64+
self.withMutex { $0.lock() }
65+
self.items.append(item)
66+
self.withMutex { $0.unlock() }
6767

6868
self.executeNextItem()
6969

@@ -119,7 +119,7 @@ public extension FutureQueue {
119119
public extension FutureQueue {
120120
/// Do we have any enqueued operations?
121121
var isEmpty: Bool {
122-
return mutex.protect { items.isEmpty }
122+
return withMutex { $0.protect { items.isEmpty } }
123123
}
124124

125125
/// Returns a signal that will signal when `isEmpty` is changed.
@@ -164,19 +164,22 @@ public extension FutureQueue {
164164

165165
/// The error passed to `abortQueuedExecutionWithError()` if called with `shouldCloseQueue` as true.
166166
var closedError: Error? {
167-
return mutex.protect { _closedError }
167+
return withMutex { $0.protect { _closedError } }
168168
}
169169
}
170170

171171
private extension FutureQueue {
172-
var mutex: PThreadMutex { return PThreadMutex(&_mutex) }
173-
func lock() { mutex.lock() }
174-
func unlock() { mutex.unlock() }
172+
private func withMutex<T>(_ body: (PThreadMutex) throws -> T) rethrows -> T {
173+
try withUnsafeMutablePointer(to: &_mutex, body)
174+
}
175+
176+
func lock() { withMutex { $0.lock() } }
177+
func unlock() { withMutex { $0.unlock() } }
175178

176179
func removeItem(_ item: Executable) {
177-
mutex.protect {
178-
_ = items.firstIndex { $0 === item }.map { items.remove(at: $0) }
179-
}
180+
withMutex { $0.lock() }
181+
_ = items.firstIndex { $0 === item }.map { items.remove(at: $0) }
182+
withMutex { $0.unlock() }
180183
}
181184

182185
func executeNextItem() {
@@ -188,9 +191,9 @@ private extension FutureQueue {
188191
unlock()
189192

190193
item.execute(on: queueScheduler) {
191-
self.mutex.protect {
192-
self.concurrentCount -= 1
193-
}
194+
self.lock()
195+
self.concurrentCount -= 1
196+
self.unlock()
194197
self.removeItem(item)
195198
self.executeNextItem()
196199
}
@@ -215,25 +218,27 @@ private final class QueueItem<Output>: Executable {
215218
private weak var future: Future<Output>?
216219
private var hasBeenCancelled = false
217220
private var _mutex = pthread_mutex_t()
221+
private func withMutex<T>(_ body: (PThreadMutex) throws -> T) rethrows -> T {
222+
try withUnsafeMutablePointer(to: &_mutex, body)
223+
}
218224

219225
init(operation: @escaping () throws -> Future<Output>, completion: @escaping (Result<Output>) -> ()) {
220226
self.completion = completion
221227
self.operation = operation
222-
mutex.initialize()
228+
withMutex { $0.initialize() }
223229

224230
OSAtomicIncrement32(&queueItemUnitTestAliveCount)
225231
memPrint("Queue Item init", queueItemUnitTestAliveCount)
226232
}
227233

228234
deinit {
229-
mutex.deinitialize()
235+
withMutex { $0.deinitialize() }
230236
OSAtomicDecrement32(&queueItemUnitTestAliveCount)
231237
memPrint("Queue Item deinit", queueItemUnitTestAliveCount)
232238
}
233239

234-
private var mutex: PThreadMutex { return PThreadMutex(&_mutex) }
235-
private func lock() { mutex.lock() }
236-
private func unlock() { mutex.unlock() }
240+
private func lock() { withMutex { $0.lock() } }
241+
private func unlock() { withMutex { $0.unlock() } }
237242

238243
private func complete(_ result: (Result<Output>)) {
239244
lock()

0 commit comments

Comments
 (0)