@@ -170,27 +170,92 @@ public extension DispatchQueue {
170
170
return String ( validatingUTF8: __dispatch_queue_get_label ( self ) ) !
171
171
}
172
172
173
+ ///
174
+ /// Submits a block for synchronous execution on this queue.
175
+ ///
176
+ /// Submits a work item to a dispatch queue like `async(execute:)`, however
177
+ /// `sync(execute:)` will not return until the work item has finished.
178
+ ///
179
+ /// Work items submitted to a queue with `sync(execute:)` do not observe certain
180
+ /// queue attributes of that queue when invoked (such as autorelease frequency
181
+ /// and QoS class).
182
+ ///
183
+ /// Calls to `sync(execute:)` targeting the current queue will result
184
+ /// in deadlock. Use of `sync(execute:)` is also subject to the same
185
+ /// multi-party deadlock problems that may result from the use of a mutex.
186
+ /// Use of `async(execute:)` is preferred.
187
+ ///
188
+ /// As an optimization, `sync(execute:)` invokes the work item on the thread which
189
+ /// submitted it, except when the queue is the main queue or
190
+ /// a queue targetting it.
191
+ ///
192
+ /// - parameter execute: The work item to be invoked on the queue.
193
+ /// - SeeAlso: `async(execute:)`
194
+ ///
173
195
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
174
196
public func sync( execute workItem: DispatchWorkItem ) {
175
197
// _swift_dispatch_sync preserves the @convention(block) for
176
198
// work item blocks.
177
199
_swift_dispatch_sync ( self , workItem. _block)
178
200
}
179
201
202
+ ///
203
+ /// Submits a work item for asynchronous execution on a dispatch queue.
204
+ ///
205
+ /// `async(execute:)` is the fundamental mechanism for submitting
206
+ /// work items to a dispatch queue.
207
+ ///
208
+ /// Calls to `async(execute:)` always return immediately after the work item has
209
+ /// been submitted, and never wait for the work item to be invoked.
210
+ ///
211
+ /// The target queue determines whether the work item will be invoked serially or
212
+ /// concurrently with respect to other work items submitted to that same queue.
213
+ /// Serial queues are processed concurrently with respect to each other.
214
+ ///
215
+ /// - parameter execute: The work item to be invoked on the queue.
216
+ /// - SeeAlso: `sync(execute:)`
217
+ ///
218
+ ///
180
219
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
181
220
public func async ( execute workItem: DispatchWorkItem ) {
182
221
// _swift_dispatch_async preserves the @convention(block)
183
222
// for work item blocks.
184
223
_swift_dispatch_async ( self , workItem. _block)
185
224
}
186
225
226
+ ///
227
+ /// Submits a work item to a dispatch queue and associates it with the given
228
+ /// dispatch group. The dispatch group may be used to wait for the completion
229
+ /// of the work items it references.
230
+ ///
231
+ /// - parameter group: the dispatch group to associate with the submitted block.
232
+ /// - parameter execute: The work item to be invoked on the queue.
233
+ /// - SeeAlso: `sync(execute:)`
234
+ ///
187
235
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
188
236
public func async ( group: DispatchGroup , execute workItem: DispatchWorkItem ) {
189
237
// _swift_dispatch_group_async preserves the @convention(block)
190
238
// for work item blocks.
191
239
_swift_dispatch_group_async ( group, self , workItem. _block)
192
240
}
193
241
242
+ ///
243
+ /// Submits a work item to a dispatch queue and optionally associates it with a
244
+ /// dispatch group. The dispatch group may be used to wait for the completion
245
+ /// of the work items it references.
246
+ ///
247
+ /// - parameter group: the dispatch group to associate with the submitted
248
+ /// work item. If this is `nil`, the work item is not associated with a group.
249
+ /// - parameter flags: flags that control the execution environment of the
250
+ /// - parameter qos: the QoS at which the work item should be executed.
251
+ /// Defaults to `DispatchQoS.unspecified`.
252
+ /// - parameter flags: flags that control the execution environment of the
253
+ /// work item.
254
+ /// - parameter execute: The work item to be invoked on the queue.
255
+ /// - SeeAlso: `sync(execute:)`
256
+ /// - SeeAlso: `DispatchQoS`
257
+ /// - SeeAlso: `DispatchWorkItemFlags`
258
+ ///
194
259
public func async (
195
260
group: DispatchGroup ? = nil ,
196
261
qos: DispatchQoS = . unspecified,
@@ -272,10 +337,32 @@ public extension DispatchQueue {
272
337
}
273
338
}
274
339
340
+ ///
341
+ /// Submits a block for synchronous execution on this queue.
342
+ ///
343
+ /// Submits a work item to a dispatch queue like `sync(execute:)`, and returns
344
+ /// the value, of type `T`, returned by that work item.
345
+ ///
346
+ /// - parameter execute: The work item to be invoked on the queue.
347
+ /// - returns the value returned by the work item.
348
+ /// - SeeAlso: `sync(execute:)`
349
+ ///
275
350
public func sync< T> ( execute work: ( ) throws -> T ) rethrows -> T {
276
351
return try self . _syncHelper ( fn: sync, execute: work, rescue: { throw $0 } )
277
352
}
278
353
354
+ ///
355
+ /// Submits a block for synchronous execution on this queue.
356
+ ///
357
+ /// Submits a work item to a dispatch queue like `sync(execute:)`, and returns
358
+ /// the value, of type `T`, returned by that work item.
359
+ ///
360
+ /// - parameter flags: flags that control the execution environment of the
361
+ /// - parameter execute: The work item to be invoked on the queue.
362
+ /// - returns the value returned by the work item.
363
+ /// - SeeAlso: `sync(execute:)`
364
+ /// - SeeAlso: `DispatchWorkItemFlags`
365
+ ///
279
366
public func sync< T> ( flags: DispatchWorkItemFlags , execute work: ( ) throws -> T ) rethrows -> T {
280
367
if flags == . barrier {
281
368
return try self . _syncHelper ( fn: _syncBarrier, execute: work, rescue: { throw $0 } )
@@ -286,6 +373,23 @@ public extension DispatchQueue {
286
373
}
287
374
}
288
375
376
+ ///
377
+ /// Submits a work item to a dispatch queue for asynchronous execution after
378
+ /// a specified time.
379
+ ///
380
+ /// - parameter: deadline the time after which the work item should be executed,
381
+ /// given as a `DispatchTime`.
382
+ /// - parameter qos: the QoS at which the work item should be executed.
383
+ /// Defaults to `DispatchQoS.unspecified`.
384
+ /// - parameter flags: flags that control the execution environment of the
385
+ /// work item.
386
+ /// - parameter execute: The work item to be invoked on the queue.
387
+ /// - SeeAlso: `async(execute:)`
388
+ /// - SeeAlso: `asyncAfter(deadline:execute:)`
389
+ /// - SeeAlso: `DispatchQoS`
390
+ /// - SeeAlso: `DispatchWorkItemFlags`
391
+ /// - SeeAlso: `DispatchTime`
392
+ ///
289
393
public func asyncAfter(
290
394
deadline: DispatchTime ,
291
395
qos: DispatchQoS = . unspecified,
@@ -300,6 +404,23 @@ public extension DispatchQueue {
300
404
}
301
405
}
302
406
407
+ ///
408
+ /// Submits a work item to a dispatch queue for asynchronous execution after
409
+ /// a specified time.
410
+ ///
411
+ /// - parameter: deadline the time after which the work item should be executed,
412
+ /// given as a `DispatchWallTime`.
413
+ /// - parameter qos: the QoS at which the work item should be executed.
414
+ /// Defaults to `DispatchQoS.unspecified`.
415
+ /// - parameter flags: flags that control the execution environment of the
416
+ /// work item.
417
+ /// - parameter execute: The work item to be invoked on the queue.
418
+ /// - SeeAlso: `async(execute:)`
419
+ /// - SeeAlso: `asyncAfter(wallDeadline:execute:)`
420
+ /// - SeeAlso: `DispatchQoS`
421
+ /// - SeeAlso: `DispatchWorkItemFlags`
422
+ /// - SeeAlso: `DispatchWallTime`
423
+ ///
303
424
public func asyncAfter(
304
425
wallDeadline: DispatchWallTime ,
305
426
qos: DispatchQoS = . unspecified,
@@ -314,11 +435,31 @@ public extension DispatchQueue {
314
435
}
315
436
}
316
437
438
+ ///
439
+ /// Submits a work item to a dispatch queue for asynchronous execution after
440
+ /// a specified time.
441
+ ///
442
+ /// - parameter: deadline the time after which the work item should be executed,
443
+ /// given as a `DispatchTime`.
444
+ /// - parameter execute: The work item to be invoked on the queue.
445
+ /// - SeeAlso: `asyncAfter(deadline:qos:flags:execute:)`
446
+ /// - SeeAlso: `DispatchTime`
447
+ ///
317
448
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
318
449
public func asyncAfter( deadline: DispatchTime , execute: DispatchWorkItem ) {
319
450
_swift_dispatch_after ( deadline. rawValue, self , execute. _block)
320
451
}
321
452
453
+ ///
454
+ /// Submits a work item to a dispatch queue for asynchronous execution after
455
+ /// a specified time.
456
+ ///
457
+ /// - parameter: deadline the time after which the work item should be executed,
458
+ /// given as a `DispatchWallTime`.
459
+ /// - parameter execute: The work item to be invoked on the queue.
460
+ /// - SeeAlso: `asyncAfter(wallDeadline:qos:flags:execute:)`
461
+ /// - SeeAlso: `DispatchTime`
462
+ ///
322
463
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
323
464
public func asyncAfter( wallDeadline: DispatchWallTime , execute: DispatchWorkItem ) {
324
465
_swift_dispatch_after ( wallDeadline. rawValue, self , execute. _block)
0 commit comments