@@ -326,6 +326,7 @@ open class OperationQueue: NSObject {
326
326
}
327
327
}
328
328
let queueGroup = DispatchGroup ( )
329
+ var unscheduledWorkItems : [ DispatchWorkItem ] = [ ]
329
330
#endif
330
331
331
332
var _operations = _OperationList ( )
@@ -364,9 +365,6 @@ open class OperationQueue: NSObject {
364
365
}
365
366
}
366
367
let queue = DispatchQueue ( label: effectiveName, attributes: attr)
367
- if _suspended {
368
- queue. suspend ( )
369
- }
370
368
__underlyingQueue = queue
371
369
lock. unlock ( )
372
370
return queue
@@ -432,13 +430,13 @@ open class OperationQueue: NSObject {
432
430
_operations. insert ( operation)
433
431
}
434
432
lock. unlock ( )
435
- ops. forEach { ( operation: Operation ) -> Void in
436
433
#if DEPLOYMENT_ENABLE_LIBDISPATCH
434
+ let items = ops. map { ( operation: Operation ) -> DispatchWorkItem in
437
435
if let group = waitGroup {
438
436
group. enter ( )
439
437
}
440
438
441
- let block = DispatchWorkItem ( flags: . enforceQoS) { ( ) -> Void in
439
+ return DispatchWorkItem ( flags: . enforceQoS) { ( ) -> Void in
442
440
if let sema = self . _concurrencyGate {
443
441
sema. wait ( )
444
442
self . _runOperation ( )
@@ -450,10 +448,17 @@ open class OperationQueue: NSObject {
450
448
group. leave ( )
451
449
}
452
450
}
453
- _underlyingQueue. async ( group: queueGroup, execute: block)
454
- #endif
455
451
}
456
- #if DEPLOYMENT_ENABLE_LIBDISPATCH
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
+
457
462
if let group = waitGroup {
458
463
group. wait ( )
459
464
}
@@ -498,19 +503,16 @@ open class OperationQueue: NSObject {
498
503
}
499
504
set {
500
505
lock. lock ( )
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
- }
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)
510
514
}
511
- #endif
512
515
}
513
- lock. unlock ( )
514
516
}
515
517
}
516
518
0 commit comments