Skip to content

Revert "SR-8572: Rework OperationQueue.isSuspended to stop suspending underly…" #1671

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions Foundation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ open class OperationQueue: NSObject {
}
}
let queueGroup = DispatchGroup()
var unscheduledWorkItems: [DispatchWorkItem] = []
#endif

var _operations = _OperationList()
Expand Down Expand Up @@ -365,6 +364,9 @@ open class OperationQueue: NSObject {
}
}
let queue = DispatchQueue(label: effectiveName, attributes: attr)
if _suspended {
queue.suspend()
}
__underlyingQueue = queue
lock.unlock()
return queue
Expand Down Expand Up @@ -430,13 +432,13 @@ open class OperationQueue: NSObject {
_operations.insert(operation)
}
lock.unlock()
ops.forEach { (operation: Operation) -> Void in
#if DEPLOYMENT_ENABLE_LIBDISPATCH
let items = ops.map { (operation: Operation) -> DispatchWorkItem in
if let group = waitGroup {
group.enter()
}

return DispatchWorkItem(flags: .enforceQoS) { () -> Void in
let block = DispatchWorkItem(flags: .enforceQoS) { () -> Void in
if let sema = self._concurrencyGate {
sema.wait()
self._runOperation()
Expand All @@ -448,17 +450,10 @@ open class OperationQueue: NSObject {
group.leave()
}
}
_underlyingQueue.async(group: queueGroup, execute: block)
#endif
}

let queue = _underlyingQueue
lock.lock()
if _suspended {
unscheduledWorkItems += items
} else {
items.forEach { queue.async(group: queueGroup, execute: $0) }
}
lock.unlock()

#if DEPLOYMENT_ENABLE_LIBDISPATCH
if let group = waitGroup {
group.wait()
}
Expand Down Expand Up @@ -503,16 +498,19 @@ open class OperationQueue: NSObject {
}
set {
lock.lock()
_suspended = newValue
let items = unscheduledWorkItems
unscheduledWorkItems.removeAll()
lock.unlock()

if !newValue {
items.forEach {
_underlyingQueue.async(group: queueGroup, execute: $0)
if _suspended != newValue {
_suspended = newValue
#if DEPLOYMENT_ENABLE_LIBDISPATCH
if let queue = __underlyingQueue {
if newValue {
queue.suspend()
} else {
queue.resume()
}
}
#endif
}
lock.unlock()
}
}

Expand Down
24 changes: 0 additions & 24 deletions TestFoundation/TestOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class TestOperationQueue : XCTestCase {
("test_CurrentQueueOnBackgroundQueueWithSelfCancel", test_CurrentQueueOnBackgroundQueueWithSelfCancel),
("test_CurrentQueueWithCustomUnderlyingQueue", test_CurrentQueueWithCustomUnderlyingQueue),
("test_CurrentQueueWithUnderlyingQueueResetToNil", test_CurrentQueueWithUnderlyingQueueResetToNil),
("test_isSuspended", test_isSuspended),
]
}

Expand Down Expand Up @@ -156,29 +155,6 @@ class TestOperationQueue : XCTestCase {
waitForExpectations(timeout: 1)
}

func test_isSuspended() {
let expectation1 = self.expectation(description: "DispatchQueue execution")
let expectation2 = self.expectation(description: "OperationQueue execution")

let dispatchQueue = DispatchQueue(label: "underlying_queue")
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
operationQueue.underlyingQueue = dispatchQueue
operationQueue.isSuspended = true

operationQueue.addOperation {
XCTAssert(OperationQueue.current?.underlyingQueue === dispatchQueue)
expectation2.fulfill()
}

dispatchQueue.async {
operationQueue.isSuspended = false
expectation1.fulfill()
}

waitForExpectations(timeout: 1)
}

func test_CurrentQueueWithUnderlyingQueueResetToNil() {
let expectation = self.expectation(description: "Background execution")

Expand Down