Skip to content

Commit c7e70da

Browse files
committed
Revert 'defer' for lock()/unlock()
1 parent 67bd63f commit c7e70da

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

Foundation/NSCache.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,10 @@ open class NSCache<KeyType : AnyObject, ObjectType : AnyObject> : NSObject {
7575
let key = NSCacheKey(key)
7676

7777
_lock.lock()
78-
defer { _lock.unlock() }
79-
8078
if let entry = _entries[key] {
8179
object = entry.value
8280
}
81+
_lock.unlock()
8382

8483
return object
8584
}
@@ -193,19 +192,19 @@ open class NSCache<KeyType : AnyObject, ObjectType : AnyObject> : NSObject {
193192
let keyRef = NSCacheKey(key)
194193

195194
_lock.lock()
196-
defer { _lock.unlock() }
197195
if let entry = _entries.removeValue(forKey: keyRef) {
198196
_totalCost -= entry.cost
199197
remove(entry)
200198
}
199+
_lock.unlock()
201200
}
202201

203202
open func removeAllObjects() {
204203
_lock.lock()
205-
defer { _lock.unlock() }
206204
_entries.removeAll()
207205
_byCost = nil
208206
_totalCost = 0
207+
_lock.unlock()
209208
}
210209
}
211210

Foundation/NSLock.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ open class NSConditionLock : NSObject, NSLocking {
8484

8585
open func unlock() {
8686
_cond.lock()
87-
defer {
88-
_cond.broadcast()
89-
_cond.unlock()
90-
}
9187
_thread = nil
88+
_cond.broadcast()
89+
_cond.unlock()
9290
}
9391

9492
open var condition: Int {
@@ -109,35 +107,35 @@ open class NSConditionLock : NSObject, NSLocking {
109107

110108
open func unlock(withCondition condition: Int) {
111109
_cond.lock()
112-
defer {
113-
_cond.broadcast()
114-
_cond.unlock()
115-
}
116110
_thread = nil
117111
_value = condition
112+
_cond.broadcast()
113+
_cond.unlock()
118114
}
119115

120116
open func lock(before limit: Date) -> Bool {
121117
_cond.lock()
122-
defer { _cond.unlock() }
123118
while _thread != nil {
124119
if !_cond.wait(until: limit) {
120+
_cond.unlock()
125121
return false
126122
}
127123
}
128124
_thread = pthread_self()
125+
_cond.unlock()
129126
return true
130127
}
131128

132129
open func lock(whenCondition condition: Int, before limit: Date) -> Bool {
133130
_cond.lock()
134-
defer { _cond.unlock() }
135131
while _thread != nil || _value != condition {
136132
if !_cond.wait(until: limit) {
133+
_cond.unlock()
137134
return false
138135
}
139136
}
140137
_thread = pthread_self()
138+
_cond.unlock()
141139
return true
142140
}
143141

Foundation/NSOperation.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ open class Operation : NSObject {
8080

8181
open func cancel() {
8282
lock.lock()
83-
defer { lock.unlock() }
8483
_cancelled = true
8584
_leaveGroups()
85+
lock.unlock()
8686
}
8787

8888
open var isExecuting: Bool {
@@ -104,35 +104,35 @@ open class Operation : NSObject {
104104

105105
open func addDependency(_ op: Operation) {
106106
lock.lock()
107-
defer { lock.unlock() }
108107
_dependencies.insert(op)
109108
op.lock.lock()
110-
defer { op.lock.unlock() }
111109
#if DEPLOYMENT_ENABLE_LIBDISPATCH
112110
_depGroup.enter()
113111
op._groups.append(_depGroup)
114112
#endif
113+
op.lock.unlock()
114+
lock.unlock()
115115
}
116116

117117
open func removeDependency(_ op: Operation) {
118118
lock.lock()
119-
defer { lock.unlock() }
120119
_dependencies.remove(op)
121120
op.lock.lock()
122-
defer { op.lock.unlock() }
123121
#if DEPLOYMENT_ENABLE_LIBDISPATCH
124122
let groupIndex = op._groups.index(where: { $0 === self._depGroup })
125123
if let idx = groupIndex {
126124
let group = op._groups.remove(at: idx)
127125
group.leave()
128126
}
129127
#endif
128+
op.lock.unlock()
129+
lock.unlock()
130130
}
131131

132132
open var dependencies: [Operation] {
133133
lock.lock()
134-
defer { lock.unlock() }
135134
let ops = _dependencies.map() { $0 }
135+
lock.unlock()
136136
return ops
137137
}
138138

@@ -189,14 +189,14 @@ open class BlockOperation: Operation {
189189

190190
open func addExecutionBlock(_ block: @escaping () -> Void) {
191191
lock.lock()
192-
defer { lock.unlock() }
193192
_executionBlocks.append(block)
193+
lock.unlock()
194194
}
195195

196196
open var executionBlocks: [() -> Void] {
197197
lock.lock()
198-
defer { lock.unlock() }
199198
let blocks = _executionBlocks
199+
lock.unlock()
200200
return blocks
201201
}
202202
}
@@ -308,8 +308,8 @@ open class OperationQueue: NSObject {
308308
// However this is considerably faster and probably more effecient.
309309
internal var _underlyingQueue: DispatchQueue {
310310
lock.lock()
311-
defer { lock.unlock() }
312311
if let queue = __underlyingQueue {
312+
lock.unlock()
313313
return queue
314314
} else {
315315
let effectiveName: String
@@ -333,6 +333,7 @@ open class OperationQueue: NSObject {
333333
queue.suspend()
334334
}
335335
__underlyingQueue = queue
336+
lock.unlock()
336337
return queue
337338
}
338339
}
@@ -353,8 +354,8 @@ open class OperationQueue: NSObject {
353354

354355
internal func _dequeueOperation() -> Operation? {
355356
lock.lock()
356-
defer { lock.unlock() }
357357
let op = _operations.dequeue()
358+
lock.unlock()
358359
return op
359360
}
360361

@@ -426,9 +427,9 @@ open class OperationQueue: NSObject {
426427

427428
internal func _operationFinished(_ operation: Operation) {
428429
lock.lock()
429-
defer { lock.unlock() }
430430
_operations.remove(operation)
431431
operation._queue = nil
432+
lock.unlock()
432433
}
433434

434435
open func addOperation(_ block: @escaping () -> Swift.Void) {
@@ -440,16 +441,16 @@ open class OperationQueue: NSObject {
440441
// WARNING: the return value of this property can never be used to reliably do anything sensible
441442
open var operations: [Operation] {
442443
lock.lock()
443-
defer { lock.unlock() }
444444
let ops = _operations.map() { $0 }
445+
lock.unlock()
445446
return ops
446447
}
447448

448449
// WARNING: the return value of this property can never be used to reliably do anything sensible
449450
open var operationCount: Int {
450451
lock.lock()
451-
defer { lock.unlock() }
452452
let count = _operations.count
453+
lock.unlock()
453454
return count
454455
}
455456

@@ -462,7 +463,6 @@ open class OperationQueue: NSObject {
462463
}
463464
set {
464465
lock.lock()
465-
defer { lock.unlock() }
466466
if _suspended != newValue {
467467
_suspended = newValue
468468
#if DEPLOYMENT_ENABLE_LIBDISPATCH
@@ -475,24 +475,25 @@ open class OperationQueue: NSObject {
475475
}
476476
#endif
477477
}
478+
lock.unlock()
478479
}
479480
}
480481

481482
internal var _name: String?
482483
open var name: String? {
483484
get {
484485
lock.lock()
485-
defer { lock.unlock() }
486486
let val = _name
487+
lock.unlock()
487488
return val
488489
}
489490
set {
490491
lock.lock()
491-
defer { lock.unlock() }
492492
_name = newValue
493493
#if DEPLOYMENT_ENABLE_LIBDISPATCH
494494
__underlyingQueue = nil
495495
#endif
496+
lock.unlock()
496497
}
497498
}
498499

@@ -503,14 +504,14 @@ open class OperationQueue: NSObject {
503504
open var underlyingQueue: DispatchQueue? {
504505
get {
505506
lock.lock()
506-
defer { lock.unlock() }
507507
let queue = __underlyingQueue
508+
lock.unlock()
508509
return queue
509510
}
510511
set {
511512
lock.lock()
512-
defer { lock.unlock() }
513513
__underlyingQueue = newValue
514+
lock.unlock()
514515
}
515516
}
516517
#endif

0 commit comments

Comments
 (0)