Skip to content

Commit 67bd63f

Browse files
author
Sergey Minakov
committed
defer usage where possible
1 parent 8fc2167 commit 67bd63f

File tree

7 files changed

+44
-41
lines changed

7 files changed

+44
-41
lines changed

Foundation/NSCache.swift

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

7777
_lock.lock()
78+
defer { _lock.unlock() }
79+
7880
if let entry = _entries[key] {
7981
object = entry.value
8082
}
81-
_lock.unlock()
8283

8384
return object
8485
}
@@ -192,19 +193,19 @@ open class NSCache<KeyType : AnyObject, ObjectType : AnyObject> : NSObject {
192193
let keyRef = NSCacheKey(key)
193194

194195
_lock.lock()
196+
defer { _lock.unlock() }
195197
if let entry = _entries.removeValue(forKey: keyRef) {
196198
_totalCost -= entry.cost
197199
remove(entry)
198200
}
199-
_lock.unlock()
200201
}
201202

202203
open func removeAllObjects() {
203204
_lock.lock()
205+
defer { _lock.unlock() }
204206
_entries.removeAll()
205207
_byCost = nil
206208
_totalCost = 0
207-
_lock.unlock()
208209
}
209210
}
210211

Foundation/NSKeyedArchiver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,13 @@ open class NSKeyedArchiver : NSCoder {
159159
return false
160160
}
161161

162+
defer { CFWriteStreamClose(writeStream) }
163+
162164
let keyedArchiver = NSKeyedArchiver(output: writeStream)
163165

164166
keyedArchiver.encode(rootObject, forKey: NSKeyedArchiveRootObjectKey)
165167
keyedArchiver.finishEncoding()
166168
finishedEncoding = keyedArchiver._flags.contains(ArchiverFlags.finishedEncoding)
167-
168-
CFWriteStreamClose(writeStream)
169169

170170
return finishedEncoding
171171
}

Foundation/NSKeyedUnarchiver.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ open class NSKeyedUnarchiver : NSCoder {
7474
return nil
7575
}
7676

77+
defer { CFReadStreamClose(readStream) }
78+
7779
let keyedUnarchiver = NSKeyedUnarchiver(stream: Stream.stream(readStream))
7880
do {
7981
try root = keyedUnarchiver.decodeTopLevelObject(forKey: NSKeyedArchiveRootObjectKey)
8082
keyedUnarchiver.finishDecoding()
8183
} catch {
8284
}
8385

84-
CFReadStreamClose(readStream)
85-
8686
return root
8787
}
8888

Foundation/NSLock.swift

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

8585
open func unlock() {
8686
_cond.lock()
87+
defer {
88+
_cond.broadcast()
89+
_cond.unlock()
90+
}
8791
_thread = nil
88-
_cond.broadcast()
89-
_cond.unlock()
9092
}
9193

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

108110
open func unlock(withCondition condition: Int) {
109111
_cond.lock()
112+
defer {
113+
_cond.broadcast()
114+
_cond.unlock()
115+
}
110116
_thread = nil
111117
_value = condition
112-
_cond.broadcast()
113-
_cond.unlock()
114118
}
115119

116120
open func lock(before limit: Date) -> Bool {
117121
_cond.lock()
122+
defer { _cond.unlock() }
118123
while _thread != nil {
119124
if !_cond.wait(until: limit) {
120-
_cond.unlock()
121125
return false
122126
}
123127
}
124128
_thread = pthread_self()
125-
_cond.unlock()
126129
return true
127130
}
128131

129132
open func lock(whenCondition condition: Int, before limit: Date) -> Bool {
130133
_cond.lock()
134+
defer { _cond.unlock() }
131135
while _thread != nil || _value != condition {
132136
if !_cond.wait(until: limit) {
133-
_cond.unlock()
134137
return false
135138
}
136139
}
137140
_thread = pthread_self()
138-
_cond.unlock()
139141
return true
140142
}
141143

Foundation/NSOperation.swift

Lines changed: 18 additions & 19 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() }
8384
_cancelled = true
8485
_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() }
107108
_dependencies.insert(op)
108109
op.lock.lock()
110+
defer { op.lock.unlock() }
109111
#if DEPLOYMENT_ENABLE_LIBDISPATCH
110112
_depGroup.enter()
111113
op._groups.append(_depGroup)
112114
#endif
113-
op.lock.unlock()
114-
lock.unlock()
115115
}
116116

117117
open func removeDependency(_ op: Operation) {
118118
lock.lock()
119+
defer { lock.unlock() }
119120
_dependencies.remove(op)
120121
op.lock.lock()
122+
defer { op.lock.unlock() }
121123
#if DEPLOYMENT_ENABLE_LIBDISPATCH
122124
let groupIndex = op._groups.index(where: { $0 === self._depGroup })
123125
if let idx = groupIndex {
124126
let group = op._groups.remove(at: idx)
125127
group.leave()
126128
}
127129
#endif
128-
op.lock.unlock()
129-
lock.unlock()
130130
}
131131

132132
open var dependencies: [Operation] {
133133
lock.lock()
134+
defer { lock.unlock() }
134135
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() }
192193
_executionBlocks.append(block)
193-
lock.unlock()
194194
}
195195

196196
open var executionBlocks: [() -> Void] {
197197
lock.lock()
198+
defer { lock.unlock() }
198199
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() }
311312
if let queue = __underlyingQueue {
312-
lock.unlock()
313313
return queue
314314
} else {
315315
let effectiveName: String
@@ -333,7 +333,6 @@ open class OperationQueue: NSObject {
333333
queue.suspend()
334334
}
335335
__underlyingQueue = queue
336-
lock.unlock()
337336
return queue
338337
}
339338
}
@@ -354,8 +353,8 @@ open class OperationQueue: NSObject {
354353

355354
internal func _dequeueOperation() -> Operation? {
356355
lock.lock()
356+
defer { lock.unlock() }
357357
let op = _operations.dequeue()
358-
lock.unlock()
359358
return op
360359
}
361360

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

428427
internal func _operationFinished(_ operation: Operation) {
429428
lock.lock()
429+
defer { lock.unlock() }
430430
_operations.remove(operation)
431431
operation._queue = nil
432-
lock.unlock()
433432
}
434433

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

449448
// WARNING: the return value of this property can never be used to reliably do anything sensible
450449
open var operationCount: Int {
451450
lock.lock()
451+
defer { lock.unlock() }
452452
let count = _operations.count
453-
lock.unlock()
454453
return count
455454
}
456455

@@ -463,6 +462,7 @@ open class OperationQueue: NSObject {
463462
}
464463
set {
465464
lock.lock()
465+
defer { lock.unlock() }
466466
if _suspended != newValue {
467467
_suspended = newValue
468468
#if DEPLOYMENT_ENABLE_LIBDISPATCH
@@ -475,25 +475,24 @@ open class OperationQueue: NSObject {
475475
}
476476
#endif
477477
}
478-
lock.unlock()
479478
}
480479
}
481480

482481
internal var _name: String?
483482
open var name: String? {
484483
get {
485484
lock.lock()
485+
defer { lock.unlock() }
486486
let val = _name
487-
lock.unlock()
488487
return val
489488
}
490489
set {
491490
lock.lock()
491+
defer { lock.unlock() }
492492
_name = newValue
493493
#if DEPLOYMENT_ENABLE_LIBDISPATCH
494494
__underlyingQueue = nil
495495
#endif
496-
lock.unlock()
497496
}
498497
}
499498

@@ -504,14 +503,14 @@ open class OperationQueue: NSObject {
504503
open var underlyingQueue: DispatchQueue? {
505504
get {
506505
lock.lock()
506+
defer { lock.unlock() }
507507
let queue = __underlyingQueue
508-
lock.unlock()
509508
return queue
510509
}
511510
set {
512511
lock.lock()
512+
defer { lock.unlock() }
513513
__underlyingQueue = newValue
514-
lock.unlock()
515514
}
516515
}
517516
#endif

Foundation/NSXMLParser.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,12 @@ open class XMLParser : NSObject {
559559
internal func parseFromStream() -> Bool {
560560
var result = true
561561
XMLParser.setCurrentParser(self)
562+
defer { XMLParser.setCurrentParser(nil) }
562563
if let stream = _stream {
563564
stream.open()
565+
defer { stream.close() }
564566
let buffer = malloc(_chunkSize)!.bindMemory(to: UInt8.self, capacity: _chunkSize)
567+
defer { free(buffer) }
565568
var len = stream.read(buffer, maxLength: _chunkSize)
566569
if len != -1 {
567570
while len > 0 {
@@ -572,10 +575,9 @@ open class XMLParser : NSObject {
572575
} else {
573576
result = false
574577
}
575-
free(buffer)
576-
stream.close()
577578
} else if let data = _data {
578579
let buffer = malloc(_chunkSize)!.bindMemory(to: UInt8.self, capacity: _chunkSize)
580+
defer { free(buffer) }
579581
var range = NSMakeRange(0, min(_chunkSize, data.count))
580582
while result {
581583
let chunk = data.withUnsafeBytes { (buffer: UnsafePointer<UInt8>) -> Data in
@@ -588,11 +590,9 @@ open class XMLParser : NSObject {
588590
}
589591
range = NSMakeRange(range.location + range.length, min(_chunkSize, data.count - (range.location + range.length)))
590592
}
591-
free(buffer)
592593
} else {
593594
result = false
594595
}
595-
XMLParser.setCurrentParser(nil)
596596
return result
597597
}
598598

Foundation/Process.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ open class Process: NSObject {
202202
open func launch() {
203203

204204
self.processLaunchedCondition.lock()
205+
defer {
206+
self.processLaunchedCondition.unlock()
207+
self.processLaunchedCondition.broadcast()
208+
}
205209

206210
// Dispatch the manager thread if it isn't already running
207211

@@ -436,9 +440,6 @@ open class Process: NSObject {
436440
isRunning = true
437441

438442
self.processIdentifier = pid
439-
440-
self.processLaunchedCondition.unlock()
441-
self.processLaunchedCondition.broadcast()
442443
}
443444

444445
open func interrupt() { NSUnimplemented() } // Not always possible. Sends SIGINT.

0 commit comments

Comments
 (0)