Skip to content

SR-8572: Rework OperationQueue.isSuspended to stop suspending underly… #1672

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 1 commit into from
Aug 27, 2018
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: 21 additions & 19 deletions Foundation/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ open class OperationQueue: NSObject {
}
}
let queueGroup = DispatchGroup()
var unscheduledWorkItems: [DispatchWorkItem] = []
#endif

var _operations = _OperationList()
Expand Down Expand Up @@ -364,9 +365,6 @@ 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 @@ -432,13 +430,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()
}

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

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

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

if !newValue {
items.forEach {
_underlyingQueue.async(group: queueGroup, execute: $0)
}
#endif
}
lock.unlock()
}
}

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

Expand Down Expand Up @@ -155,6 +156,29 @@ 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