Skip to content

Commit 347b874

Browse files
authored
Merge pull request #2831 from spevans/pr_operation_queue_53
[5.3] OperationQueue fixes
2 parents c0db176 + 62d77d8 commit 347b874

File tree

2 files changed

+390
-7
lines changed

2 files changed

+390
-7
lines changed

Sources/Foundation/Operation.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ open class Operation : NSObject {
125125
internal var _queue: OperationQueue? {
126126
_lock()
127127
defer { _unlock() }
128-
return __queue?.takeRetainedValue()
128+
return __queue?.takeUnretainedValue()
129129
}
130130

131131
internal func _adopt(queue: OperationQueue, schedule: DispatchWorkItem) {
@@ -230,7 +230,7 @@ open class Operation : NSObject {
230230
let kind: Transition?
231231
if keyPath == _NSOperationIsFinished || keyPath == _NSOperationIsFinishedAlternate {
232232
kind = .toFinished
233-
} else if keyPath == _NSOperationIsExecuting || keyPath == _NSOperationIsReadyAlternate {
233+
} else if keyPath == _NSOperationIsExecuting || keyPath == _NSOperationIsExecutingAlternate {
234234
kind = .toExecuting
235235
} else if keyPath == _NSOperationIsReady || keyPath == _NSOperationIsReadyAlternate {
236236
kind = .toReady
@@ -474,7 +474,7 @@ open class Operation : NSObject {
474474

475475
internal func changePriority(_ newPri: Operation.QueuePriority.RawValue) {
476476
_lock()
477-
guard let oq = __queue?.takeRetainedValue() else {
477+
guard let oq = __queue?.takeUnretainedValue() else {
478478
__priorityValue = newPri
479479
_unlock()
480480
return
@@ -963,6 +963,11 @@ open class OperationQueue : NSObject, ProgressReporting {
963963
OperationQueue._currentQueue.set(self)
964964
op.start()
965965
OperationQueue._currentQueue.clear()
966+
// We've just cleared _currentQueue storage.
967+
// NSThreadSpecific doesn't release stored value on clear.
968+
// This means `self` will leak if we don't release manually.
969+
Unmanaged.passUnretained(self).release()
970+
966971
// unset current tsd
967972
if op.isFinished && op._state.rawValue < Operation.__NSOperationState.finishing.rawValue {
968973
Operation.observeValue(forKeyPath: _NSOperationIsFinished, ofObject: op)
@@ -1086,7 +1091,7 @@ open class OperationQueue : NSObject, ProgressReporting {
10861091
defer { _unlock() }
10871092
var op = __firstOperation
10881093
var cnt = 0
1089-
if let operation = op?.takeUnretainedValue() {
1094+
while let operation = op?.takeUnretainedValue() {
10901095
if !(operation is _BarrierOperation) {
10911096
cnt += 1
10921097
}
@@ -1100,7 +1105,7 @@ open class OperationQueue : NSObject, ProgressReporting {
11001105
defer { _unlock() }
11011106
var operations = [Operation]()
11021107
var op = __firstOperation
1103-
if let operation = op?.takeUnretainedValue() {
1108+
while let operation = op?.takeUnretainedValue() {
11041109
if includingBarriers || !(operation is _BarrierOperation) {
11051110
operations.append(operation)
11061111
}

0 commit comments

Comments
 (0)