Skip to content

Commit f83c17c

Browse files
Merge pull request #18044 from ktopley-apple/ktopley-dispatch-rdar40951342
Add documentation comments.
2 parents 33c2efe + 354bc9e commit f83c17c

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

stdlib/public/SDK/Dispatch/Queue.swift

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,27 +170,92 @@ public extension DispatchQueue {
170170
return String(validatingUTF8: __dispatch_queue_get_label(self))!
171171
}
172172

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+
///
173195
@available(macOS 10.10, iOS 8.0, *)
174196
public func sync(execute workItem: DispatchWorkItem) {
175197
// _swift_dispatch_sync preserves the @convention(block) for
176198
// work item blocks.
177199
_swift_dispatch_sync(self, workItem._block)
178200
}
179201

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+
///
180219
@available(macOS 10.10, iOS 8.0, *)
181220
public func async(execute workItem: DispatchWorkItem) {
182221
// _swift_dispatch_async preserves the @convention(block)
183222
// for work item blocks.
184223
_swift_dispatch_async(self, workItem._block)
185224
}
186225

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+
///
187235
@available(macOS 10.10, iOS 8.0, *)
188236
public func async(group: DispatchGroup, execute workItem: DispatchWorkItem) {
189237
// _swift_dispatch_group_async preserves the @convention(block)
190238
// for work item blocks.
191239
_swift_dispatch_group_async(group, self, workItem._block)
192240
}
193241

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+
///
194259
public func async(
195260
group: DispatchGroup? = nil,
196261
qos: DispatchQoS = .unspecified,
@@ -272,10 +337,32 @@ public extension DispatchQueue {
272337
}
273338
}
274339

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+
///
275350
public func sync<T>(execute work: () throws -> T) rethrows -> T {
276351
return try self._syncHelper(fn: sync, execute: work, rescue: { throw $0 })
277352
}
278353

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+
///
279366
public func sync<T>(flags: DispatchWorkItemFlags, execute work: () throws -> T) rethrows -> T {
280367
if flags == .barrier {
281368
return try self._syncHelper(fn: _syncBarrier, execute: work, rescue: { throw $0 })
@@ -286,6 +373,23 @@ public extension DispatchQueue {
286373
}
287374
}
288375

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+
///
289393
public func asyncAfter(
290394
deadline: DispatchTime,
291395
qos: DispatchQoS = .unspecified,
@@ -300,6 +404,23 @@ public extension DispatchQueue {
300404
}
301405
}
302406

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+
///
303424
public func asyncAfter(
304425
wallDeadline: DispatchWallTime,
305426
qos: DispatchQoS = .unspecified,
@@ -314,11 +435,31 @@ public extension DispatchQueue {
314435
}
315436
}
316437

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+
///
317448
@available(macOS 10.10, iOS 8.0, *)
318449
public func asyncAfter(deadline: DispatchTime, execute: DispatchWorkItem) {
319450
_swift_dispatch_after(deadline.rawValue, self, execute._block)
320451
}
321452

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+
///
322463
@available(macOS 10.10, iOS 8.0, *)
323464
public func asyncAfter(wallDeadline: DispatchWallTime, execute: DispatchWorkItem) {
324465
_swift_dispatch_after(wallDeadline.rawValue, self, execute._block)

0 commit comments

Comments
 (0)