@@ -26,24 +26,24 @@ public class Operation: NSObject {
26
26
internal var _ready = false
27
27
internal var _dependencies = Set < Operation > ( )
28
28
#if DEPLOYMENT_ENABLE_LIBDISPATCH
29
- internal var _group = dispatch_group_create ( )
30
- internal var _depGroup = dispatch_group_create ( )
31
- internal var _groups = [ dispatch_group_t ] ( )
29
+ internal var _group = DispatchGroup ( )
30
+ internal var _depGroup = DispatchGroup ( )
31
+ internal var _groups = [ DispatchGroup ] ( )
32
32
#endif
33
33
34
34
public override init ( ) {
35
35
super. init ( )
36
36
#if DEPLOYMENT_ENABLE_LIBDISPATCH
37
- dispatch_group_enter ( _group)
37
+ _group. enter ( )
38
38
#endif
39
39
}
40
40
41
41
internal func _leaveGroups( ) {
42
42
// assumes lock is taken
43
43
#if DEPLOYMENT_ENABLE_LIBDISPATCH
44
- _groups. forEach ( ) { dispatch_group_leave ( $0 ) }
44
+ _groups. forEach ( ) { $0 . leave ( ) }
45
45
_groups. removeAll ( )
46
- dispatch_group_leave ( _group)
46
+ _group. leave ( )
47
47
#endif
48
48
}
49
49
@@ -65,7 +65,7 @@ public class Operation: NSObject {
65
65
// The completion block property is a bit cagey and can not be executed locally on the queue due to thread exhaust potentials.
66
66
// This sets up for some strange behavior of finishing operations since the handler will be executed on a different queue
67
67
if let completion = completionBlock {
68
- dispatch_async ( dispatch_get_global_queue ( DISPATCH_QUEUE_PRIORITY_BACKGROUND , 0 ) ) { ( ) -> Void in
68
+ DispatchQueue . global ( attributes : . qosBackground ) . async { ( ) -> Void in
69
69
completion ( )
70
70
}
71
71
}
@@ -107,7 +107,7 @@ public class Operation: NSObject {
107
107
_dependencies. insert ( op)
108
108
op. lock. lock ( )
109
109
#if DEPLOYMENT_ENABLE_LIBDISPATCH
110
- dispatch_group_enter ( _depGroup)
110
+ _depGroup. enter ( )
111
111
op. _groups. append ( _depGroup)
112
112
#endif
113
113
op. lock. unlock ( )
@@ -122,7 +122,7 @@ public class Operation: NSObject {
122
122
let groupIndex = op. _groups. index ( where: { $0 === self . _depGroup } )
123
123
if let idx = groupIndex {
124
124
let group = op. _groups. remove ( at: idx)
125
- dispatch_group_leave ( group)
125
+ group. leave ( )
126
126
}
127
127
#endif
128
128
op. lock. unlock ( )
@@ -140,7 +140,7 @@ public class Operation: NSObject {
140
140
public var completionBlock : ( ( ) -> Void ) ?
141
141
public func waitUntilFinished( ) {
142
142
#if DEPLOYMENT_ENABLE_LIBDISPATCH
143
- dispatch_group_wait ( _group, DISPATCH_TIME_FOREVER )
143
+ _group. wait ( )
144
144
#endif
145
145
}
146
146
@@ -153,7 +153,7 @@ public class Operation: NSObject {
153
153
154
154
internal func _waitUntilReady( ) {
155
155
#if DEPLOYMENT_ENABLE_LIBDISPATCH
156
- dispatch_group_wait ( _depGroup, DISPATCH_TIME_FOREVER )
156
+ _depGroup. wait ( )
157
157
#endif
158
158
_ready = true
159
159
}
@@ -296,14 +296,14 @@ internal struct _OperationList {
296
296
public class OperationQueue : NSObject {
297
297
let lock = Lock ( )
298
298
#if DEPLOYMENT_ENABLE_LIBDISPATCH
299
- var __concurrencyGate : dispatch_semaphore_t ?
300
- var __underlyingQueue : dispatch_queue_t ?
301
- let queueGroup = dispatch_group_create ( )
299
+ var __concurrencyGate : DispatchSemaphore ?
300
+ var __underlyingQueue : DispatchQueue ?
301
+ let queueGroup = DispatchGroup ( )
302
302
#endif
303
303
304
304
var _operations = _OperationList ( )
305
305
#if DEPLOYMENT_ENABLE_LIBDISPATCH
306
- internal var _concurrencyGate : dispatch_semaphore_t ? {
306
+ internal var _concurrencyGate : DispatchSemaphore ? {
307
307
get {
308
308
lock. lock ( )
309
309
let val = __concurrencyGate
@@ -314,7 +314,7 @@ public class OperationQueue: NSObject {
314
314
315
315
// This is NOT the behavior of the objective-c variant; it will never re-use a queue and instead for every operation it will create a new one.
316
316
// However this is considerably faster and probably more effecient.
317
- internal var _underlyingQueue : dispatch_queue_t {
317
+ internal var _underlyingQueue : DispatchQueue {
318
318
lock. lock ( )
319
319
if let queue = __underlyingQueue {
320
320
lock. unlock ( )
@@ -326,18 +326,18 @@ public class OperationQueue: NSObject {
326
326
} else {
327
327
effectiveName = " NSOperationQueue:: \( unsafeAddress ( of: self ) ) "
328
328
}
329
- let attr : dispatch_queue_attr_t ?
329
+ let attr : DispatchQueueAttributes
330
330
if maxConcurrentOperationCount == 1 {
331
- attr = DISPATCH_QUEUE_SERIAL
331
+ attr = . serial
332
332
} else {
333
- attr = DISPATCH_QUEUE_CONCURRENT
333
+ attr = . concurrent
334
334
if maxConcurrentOperationCount != NSOperationQueueDefaultMaxConcurrentOperationCount {
335
- __concurrencyGate = dispatch_semaphore_create ( maxConcurrentOperationCount)
335
+ __concurrencyGate = DispatchSemaphore ( value : maxConcurrentOperationCount)
336
336
}
337
337
}
338
- let queue = dispatch_queue_create ( effectiveName, attr)
338
+ let queue = DispatchQueue ( label : effectiveName, attributes : attr)
339
339
if _suspended {
340
- dispatch_suspend ( queue)
340
+ queue. suspend ( )
341
341
}
342
342
__underlyingQueue = queue
343
343
lock. unlock ( )
@@ -351,12 +351,11 @@ public class OperationQueue: NSObject {
351
351
}
352
352
353
353
#if DEPLOYMENT_ENABLE_LIBDISPATCH
354
- internal init ( _queue queue: dispatch_queue_t , maxConcurrentOperations: Int = NSOperationQueueDefaultMaxConcurrentOperationCount) {
354
+ internal init ( _queue queue: DispatchQueue , maxConcurrentOperations: Int = NSOperationQueueDefaultMaxConcurrentOperationCount) {
355
355
__underlyingQueue = queue
356
356
maxConcurrentOperationCount = maxConcurrentOperations
357
357
super. init ( )
358
- dispatch_queue_set_specific ( queue, OperationQueue . OperationQueueKey, unsafeBitCast ( Unmanaged . passUnretained ( self ) , to: UnsafeMutablePointer< Void> . self ) , nil )
359
-
358
+ queue. setSpecific ( key: OperationQueue . OperationQueueKey, value: Unmanaged . passUnretained ( self ) )
360
359
}
361
360
#endif
362
361
@@ -384,9 +383,9 @@ public class OperationQueue: NSObject {
384
383
385
384
public func addOperations( _ ops: [ Operation ] , waitUntilFinished wait: Bool ) {
386
385
#if DEPLOYMENT_ENABLE_LIBDISPATCH
387
- var waitGroup : dispatch_group_t ?
386
+ var waitGroup : DispatchGroup ?
388
387
if wait {
389
- waitGroup = dispatch_group_create ( )
388
+ waitGroup = DispatchGroup ( )
390
389
}
391
390
#endif
392
391
/*
@@ -406,27 +405,27 @@ public class OperationQueue: NSObject {
406
405
lock. unlock ( )
407
406
#if DEPLOYMENT_ENABLE_LIBDISPATCH
408
407
if let group = waitGroup {
409
- dispatch_group_enter ( group)
408
+ group. enter ( )
410
409
}
411
-
412
- let block = dispatch_block_create ( DISPATCH_BLOCK_ENFORCE_QOS_CLASS ) { ( ) -> Void in
410
+
411
+ let block = DispatchWorkItem ( group : queueGroup , flags : . enforceQoS ) { ( ) -> Void in
413
412
if let sema = self . _concurrencyGate {
414
- dispatch_semaphore_wait ( sema, DISPATCH_TIME_FOREVER )
413
+ sema. wait ( )
415
414
self . _runOperation ( )
416
- dispatch_semaphore_signal ( sema)
415
+ sema. signal ( )
417
416
} else {
418
417
self . _runOperation ( )
419
418
}
420
419
if let group = waitGroup {
421
- dispatch_group_leave ( group)
420
+ group. leave ( )
422
421
}
423
422
}
424
- dispatch_group_async ( queueGroup , _underlyingQueue, block)
423
+ _underlyingQueue. async ( execute : block)
425
424
#endif
426
425
}
427
426
#if DEPLOYMENT_ENABLE_LIBDISPATCH
428
427
if let group = waitGroup {
429
- dispatch_group_wait ( group, DISPATCH_TIME_FOREVER )
428
+ group. wait ( )
430
429
}
431
430
#endif
432
431
}
@@ -474,9 +473,9 @@ public class OperationQueue: NSObject {
474
473
#if DEPLOYMENT_ENABLE_LIBDISPATCH
475
474
if let queue = __underlyingQueue {
476
475
if newValue {
477
- dispatch_suspend ( queue)
476
+ queue. suspend ( )
478
477
} else {
479
- dispatch_resume ( queue)
478
+ queue. resume ( )
480
479
}
481
480
}
482
481
#endif
@@ -507,7 +506,7 @@ public class OperationQueue: NSObject {
507
506
#if DEPLOYMENT_ENABLE_LIBDISPATCH
508
507
// Note: this will return non nil whereas the objective-c version will only return non nil when it has been set.
509
508
// it uses a target queue assignment instead of returning the actual underlying queue.
510
- public var underlyingQueue : dispatch_queue_t ? {
509
+ public var underlyingQueue : DispatchQueue ? {
511
510
get {
512
511
lock. lock ( )
513
512
let queue = __underlyingQueue
@@ -531,23 +530,25 @@ public class OperationQueue: NSObject {
531
530
532
531
public func waitUntilAllOperationsAreFinished( ) {
533
532
#if DEPLOYMENT_ENABLE_LIBDISPATCH
534
- dispatch_group_wait ( queueGroup, DISPATCH_TIME_FOREVER )
533
+ queueGroup. wait ( )
535
534
#endif
536
535
}
537
536
538
- static let OperationQueueKey = UnsafePointer < Void > ( UnsafeMutablePointer < Void > ( allocatingCapacity: 1 ) )
539
-
537
+ #if DEPLOYMENT_ENABLE_LIBDISPATCH
538
+ static let OperationQueueKey = DispatchSpecificKey < Unmanaged < OperationQueue > > ( )
539
+ #endif
540
+
540
541
public class func currentQueue( ) -> OperationQueue ? {
541
542
#if DEPLOYMENT_ENABLE_LIBDISPATCH
542
- let specific = dispatch_get_specific ( OperationQueue . OperationQueueKey)
543
+ let specific = DispatchQueue . getSpecific ( key : OperationQueue . OperationQueueKey)
543
544
if specific == nil {
544
545
if pthread_main_np ( ) == 1 {
545
546
return OperationQueue . mainQueue ( )
546
547
} else {
547
548
return nil
548
549
}
549
550
} else {
550
- return Unmanaged < OperationQueue > . fromOpaque ( unsafeBitCast ( specific, to : UnsafePointer < Void > . self ) ) . takeUnretainedValue ( )
551
+ return specific! . takeUnretainedValue ( )
551
552
}
552
553
#else
553
554
return nil
@@ -556,11 +557,11 @@ public class OperationQueue: NSObject {
556
557
557
558
public class func mainQueue( ) -> OperationQueue {
558
559
#if DEPLOYMENT_ENABLE_LIBDISPATCH
559
- let specific = dispatch_queue_get_specific ( dispatch_get_main_queue ( ) , OperationQueue . OperationQueueKey)
560
+ let specific = DispatchQueue . main . getSpecific ( key : OperationQueue . OperationQueueKey)
560
561
if specific == nil {
561
- return OperationQueue ( _queue: dispatch_get_main_queue ( ) , maxConcurrentOperations: 1 )
562
+ return OperationQueue ( _queue: DispatchQueue . main , maxConcurrentOperations: 1 )
562
563
} else {
563
- return Unmanaged < OperationQueue > . fromOpaque ( unsafeBitCast ( specific, to : UnsafePointer < Void > . self ) ) . takeUnretainedValue ( )
564
+ return specific! . takeUnretainedValue ( )
564
565
}
565
566
#else
566
567
fatalError ( " NSOperationQueue requires libdispatch " )
0 commit comments