Skip to content

Commit e82d067

Browse files
authored
Revert "SR-8572: Rework OperationQueue.isSuspended to stop suspending underlying DispatchQueue (#1665)" (#1671)
This reverts commit cbb56d1.
1 parent cbb56d1 commit e82d067

File tree

2 files changed

+19
-45
lines changed

2 files changed

+19
-45
lines changed

Foundation/Operation.swift

+19-21
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ open class OperationQueue: NSObject {
326326
}
327327
}
328328
let queueGroup = DispatchGroup()
329-
var unscheduledWorkItems: [DispatchWorkItem] = []
330329
#endif
331330

332331
var _operations = _OperationList()
@@ -365,6 +364,9 @@ open class OperationQueue: NSObject {
365364
}
366365
}
367366
let queue = DispatchQueue(label: effectiveName, attributes: attr)
367+
if _suspended {
368+
queue.suspend()
369+
}
368370
__underlyingQueue = queue
369371
lock.unlock()
370372
return queue
@@ -430,13 +432,13 @@ open class OperationQueue: NSObject {
430432
_operations.insert(operation)
431433
}
432434
lock.unlock()
435+
ops.forEach { (operation: Operation) -> Void in
433436
#if DEPLOYMENT_ENABLE_LIBDISPATCH
434-
let items = ops.map { (operation: Operation) -> DispatchWorkItem in
435437
if let group = waitGroup {
436438
group.enter()
437439
}
438440

439-
return DispatchWorkItem(flags: .enforceQoS) { () -> Void in
441+
let block = DispatchWorkItem(flags: .enforceQoS) { () -> Void in
440442
if let sema = self._concurrencyGate {
441443
sema.wait()
442444
self._runOperation()
@@ -448,17 +450,10 @@ open class OperationQueue: NSObject {
448450
group.leave()
449451
}
450452
}
453+
_underlyingQueue.async(group: queueGroup, execute: block)
454+
#endif
451455
}
452-
453-
let queue = _underlyingQueue
454-
lock.lock()
455-
if _suspended {
456-
unscheduledWorkItems += items
457-
} else {
458-
items.forEach { queue.async(group: queueGroup, execute: $0) }
459-
}
460-
lock.unlock()
461-
456+
#if DEPLOYMENT_ENABLE_LIBDISPATCH
462457
if let group = waitGroup {
463458
group.wait()
464459
}
@@ -503,16 +498,19 @@ open class OperationQueue: NSObject {
503498
}
504499
set {
505500
lock.lock()
506-
_suspended = newValue
507-
let items = unscheduledWorkItems
508-
unscheduledWorkItems.removeAll()
509-
lock.unlock()
510-
511-
if !newValue {
512-
items.forEach {
513-
_underlyingQueue.async(group: queueGroup, execute: $0)
501+
if _suspended != newValue {
502+
_suspended = newValue
503+
#if DEPLOYMENT_ENABLE_LIBDISPATCH
504+
if let queue = __underlyingQueue {
505+
if newValue {
506+
queue.suspend()
507+
} else {
508+
queue.resume()
509+
}
514510
}
511+
#endif
515512
}
513+
lock.unlock()
516514
}
517515
}
518516

TestFoundation/TestOperationQueue.swift

-24
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class TestOperationQueue : XCTestCase {
2222
("test_CurrentQueueOnBackgroundQueueWithSelfCancel", test_CurrentQueueOnBackgroundQueueWithSelfCancel),
2323
("test_CurrentQueueWithCustomUnderlyingQueue", test_CurrentQueueWithCustomUnderlyingQueue),
2424
("test_CurrentQueueWithUnderlyingQueueResetToNil", test_CurrentQueueWithUnderlyingQueueResetToNil),
25-
("test_isSuspended", test_isSuspended),
2625
]
2726
}
2827

@@ -156,29 +155,6 @@ class TestOperationQueue : XCTestCase {
156155
waitForExpectations(timeout: 1)
157156
}
158157

159-
func test_isSuspended() {
160-
let expectation1 = self.expectation(description: "DispatchQueue execution")
161-
let expectation2 = self.expectation(description: "OperationQueue execution")
162-
163-
let dispatchQueue = DispatchQueue(label: "underlying_queue")
164-
let operationQueue = OperationQueue()
165-
operationQueue.maxConcurrentOperationCount = 1
166-
operationQueue.underlyingQueue = dispatchQueue
167-
operationQueue.isSuspended = true
168-
169-
operationQueue.addOperation {
170-
XCTAssert(OperationQueue.current?.underlyingQueue === dispatchQueue)
171-
expectation2.fulfill()
172-
}
173-
174-
dispatchQueue.async {
175-
operationQueue.isSuspended = false
176-
expectation1.fulfill()
177-
}
178-
179-
waitForExpectations(timeout: 1)
180-
}
181-
182158
func test_CurrentQueueWithUnderlyingQueueResetToNil() {
183159
let expectation = self.expectation(description: "Background execution")
184160

0 commit comments

Comments
 (0)