@@ -161,21 +161,85 @@ public extension DispatchQueue {
161
161
return String ( validatingUTF8: dispatch_queue_get_label ( self . __wrapped) ) !
162
162
}
163
163
164
+ ///
165
+ /// Submits a block for synchronous execution on this queue.
166
+ ///
167
+ /// Submits a work item to a dispatch queue like `async(execute:)`, however
168
+ /// `sync(execute:)` will not return until the work item has finished.
169
+ ///
170
+ /// Work items submitted to a queue with `sync(execute:)` do not observe certain
171
+ /// queue attributes of that queue when invoked (such as autorelease frequency
172
+ /// and QoS class).
173
+ ///
174
+ /// Calls to `sync(execute:)` targeting the current queue will result
175
+ /// in deadlock. Use of `sync(execute:)` is also subject to the same
176
+ /// multi-party deadlock problems that may result from the use of a mutex.
177
+ /// Use of `async(execute:)` is preferred.
178
+ ///
179
+ /// As an optimization, `sync(execute:)` invokes the work item on the thread which
180
+ /// submitted it, except when the queue is the main queue or
181
+ /// a queue targetting it.
182
+ ///
183
+ /// - parameter execute: The work item to be invoked on the queue.
184
+ /// - SeeAlso: `async(execute:)`
185
+ ///
164
186
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
165
187
public func sync( execute workItem: DispatchWorkItem ) {
166
188
CDispatch . dispatch_sync ( self . __wrapped, workItem. _block)
167
189
}
168
190
191
+ ///
192
+ /// Submits a work item for asynchronous execution on a dispatch queue.
193
+ ///
194
+ /// `async(execute:)` is the fundamental mechanism for submitting
195
+ /// work items to a dispatch queue.
196
+ ///
197
+ /// Calls to `async(execute:)` always return immediately after the work item has
198
+ /// been submitted, and never wait for the work item to be invoked.
199
+ ///
200
+ /// The target queue determines whether the work item will be invoked serially or
201
+ /// concurrently with respect to other work items submitted to that same queue.
202
+ /// Serial queues are processed concurrently with respect to each other.
203
+ ///
204
+ /// - parameter execute: The work item to be invoked on the queue.
205
+ /// - SeeAlso: `sync(execute:)`
206
+ ///
169
207
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
170
208
public func async ( execute workItem: DispatchWorkItem ) {
171
209
CDispatch . dispatch_async ( self . __wrapped, workItem. _block)
172
210
}
173
211
212
+ ///
213
+ /// Submits a work item to a dispatch queue and associates it with the given
214
+ /// dispatch group. The dispatch group may be used to wait for the completion
215
+ /// of the work items it references.
216
+ ///
217
+ /// - parameter group: the dispatch group to associate with the submitted block.
218
+ /// - parameter execute: The work item to be invoked on the queue.
219
+ /// - SeeAlso: `sync(execute:)`
220
+ ///
174
221
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
175
222
public func async ( group: DispatchGroup , execute workItem: DispatchWorkItem ) {
176
223
CDispatch . dispatch_group_async ( group. __wrapped, self . __wrapped, workItem. _block)
177
224
}
178
225
226
+ ///
227
+ /// Submits a work item to a dispatch queue and optionally associates it with a
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
232
+ /// work item. If this is `nil`, the work item is not associated with a group.
233
+ /// - parameter flags: flags that control the execution environment of the
234
+ /// - parameter qos: the QoS at which the work item should be executed.
235
+ /// Defaults to `DispatchQoS.unspecified`.
236
+ /// - parameter flags: flags that control the execution environment of the
237
+ /// work item.
238
+ /// - parameter execute: The work item to be invoked on the queue.
239
+ /// - SeeAlso: `sync(execute:)`
240
+ /// - SeeAlso: `DispatchQoS`
241
+ /// - SeeAlso: `DispatchWorkItemFlags`
242
+ ///
179
243
public func async (
180
244
group: DispatchGroup ? = nil ,
181
245
qos: DispatchQoS = . unspecified,
@@ -257,10 +321,32 @@ public extension DispatchQueue {
257
321
}
258
322
}
259
323
324
+ ///
325
+ /// Submits a block for synchronous execution on this queue.
326
+ ///
327
+ /// Submits a work item to a dispatch queue like `sync(execute:)`, and returns
328
+ /// the value, of type `T`, returned by that work item.
329
+ ///
330
+ /// - parameter execute: The work item to be invoked on the queue.
331
+ /// - returns the value returned by the work item.
332
+ /// - SeeAlso: `sync(execute:)`
333
+ ///
260
334
public func sync< T> ( execute work: ( ) throws -> T ) rethrows -> T {
261
335
return try self . _syncHelper ( fn: sync, execute: work, rescue: { throw $0 } )
262
336
}
263
337
338
+ ///
339
+ /// Submits a block for synchronous execution on this queue.
340
+ ///
341
+ /// Submits a work item to a dispatch queue like `sync(execute:)`, and returns
342
+ /// the value, of type `T`, returned by that work item.
343
+ ///
344
+ /// - parameter flags: flags that control the execution environment of the
345
+ /// - parameter execute: The work item to be invoked on the queue.
346
+ /// - returns the value returned by the work item.
347
+ /// - SeeAlso: `sync(execute:)`
348
+ /// - SeeAlso: `DispatchWorkItemFlags`
349
+ ///
264
350
public func sync< T> ( flags: DispatchWorkItemFlags , execute work: ( ) throws -> T ) rethrows -> T {
265
351
if flags == . barrier {
266
352
return try self . _syncHelper ( fn: _syncBarrier, execute: work, rescue: { throw $0 } )
@@ -271,6 +357,23 @@ public extension DispatchQueue {
271
357
}
272
358
}
273
359
360
+ ///
361
+ /// Submits a work item to a dispatch queue for asynchronous execution after
362
+ /// a specified time.
363
+ ///
364
+ /// - parameter: deadline the time after which the work item should be executed,
365
+ /// given as a `DispatchTime`.
366
+ /// - parameter qos: the QoS at which the work item should be executed.
367
+ /// Defaults to `DispatchQoS.unspecified`.
368
+ /// - parameter flags: flags that control the execution environment of the
369
+ /// work item.
370
+ /// - parameter execute: The work item to be invoked on the queue.
371
+ /// - SeeAlso: `async(execute:)`
372
+ /// - SeeAlso: `asyncAfter(deadline:execute:)`
373
+ /// - SeeAlso: `DispatchQoS`
374
+ /// - SeeAlso: `DispatchWorkItemFlags`
375
+ /// - SeeAlso: `DispatchTime`
376
+ ///
274
377
public func asyncAfter(
275
378
deadline: DispatchTime ,
276
379
qos: DispatchQoS = . unspecified,
@@ -285,6 +388,23 @@ public extension DispatchQueue {
285
388
}
286
389
}
287
390
391
+ ///
392
+ /// Submits a work item to a dispatch queue for asynchronous execution after
393
+ /// a specified time.
394
+ ///
395
+ /// - parameter: deadline the time after which the work item should be executed,
396
+ /// given as a `DispatchWallTime`.
397
+ /// - parameter qos: the QoS at which the work item should be executed.
398
+ /// Defaults to `DispatchQoS.unspecified`.
399
+ /// - parameter flags: flags that control the execution environment of the
400
+ /// work item.
401
+ /// - parameter execute: The work item to be invoked on the queue.
402
+ /// - SeeAlso: `async(execute:)`
403
+ /// - SeeAlso: `asyncAfter(wallDeadline:execute:)`
404
+ /// - SeeAlso: `DispatchQoS`
405
+ /// - SeeAlso: `DispatchWorkItemFlags`
406
+ /// - SeeAlso: `DispatchWallTime`
407
+ ///
288
408
public func asyncAfter(
289
409
wallDeadline: DispatchWallTime ,
290
410
qos: DispatchQoS = . unspecified,
@@ -299,11 +419,31 @@ public extension DispatchQueue {
299
419
}
300
420
}
301
421
422
+ ///
423
+ /// Submits a work item to a dispatch queue for asynchronous execution after
424
+ /// a specified time.
425
+ ///
426
+ /// - parameter: deadline the time after which the work item should be executed,
427
+ /// given as a `DispatchTime`.
428
+ /// - parameter execute: The work item to be invoked on the queue.
429
+ /// - SeeAlso: `asyncAfter(deadline:qos:flags:execute:)`
430
+ /// - SeeAlso: `DispatchTime`
431
+ ///
302
432
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
303
433
public func asyncAfter( deadline: DispatchTime , execute: DispatchWorkItem ) {
304
434
CDispatch . dispatch_after ( deadline. rawValue, self . __wrapped, execute. _block)
305
435
}
306
436
437
+ ///
438
+ /// Submits a work item to a dispatch queue for asynchronous execution after
439
+ /// a specified time.
440
+ ///
441
+ /// - parameter: deadline the time after which the work item should be executed,
442
+ /// given as a `DispatchWallTime`.
443
+ /// - parameter execute: The work item to be invoked on the queue.
444
+ /// - SeeAlso: `asyncAfter(wallDeadline:qos:flags:execute:)`
445
+ /// - SeeAlso: `DispatchTime`
446
+ ///
307
447
@available ( macOS 10 . 10 , iOS 8 . 0 , * )
308
448
public func asyncAfter( wallDeadline: DispatchWallTime , execute: DispatchWorkItem ) {
309
449
CDispatch . dispatch_after ( wallDeadline. rawValue, self . __wrapped, execute. _block)
0 commit comments