Skip to content

Commit fb6d7c5

Browse files
authored
Merge pull request #388 from ktopley-apple/ktopley-dispatch-42-rdar40951342
Add documentation comments.
2 parents 25f3244 + a174c93 commit fb6d7c5

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed

src/swift/Queue.swift

+140
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,85 @@ public extension DispatchQueue {
161161
return String(validatingUTF8: dispatch_queue_get_label(self.__wrapped))!
162162
}
163163

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+
///
164186
@available(macOS 10.10, iOS 8.0, *)
165187
public func sync(execute workItem: DispatchWorkItem) {
166188
CDispatch.dispatch_sync(self.__wrapped, workItem._block)
167189
}
168190

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+
///
169207
@available(macOS 10.10, iOS 8.0, *)
170208
public func async(execute workItem: DispatchWorkItem) {
171209
CDispatch.dispatch_async(self.__wrapped, workItem._block)
172210
}
173211

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+
///
174221
@available(macOS 10.10, iOS 8.0, *)
175222
public func async(group: DispatchGroup, execute workItem: DispatchWorkItem) {
176223
CDispatch.dispatch_group_async(group.__wrapped, self.__wrapped, workItem._block)
177224
}
178225

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+
///
179243
public func async(
180244
group: DispatchGroup? = nil,
181245
qos: DispatchQoS = .unspecified,
@@ -257,10 +321,32 @@ public extension DispatchQueue {
257321
}
258322
}
259323

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

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+
///
264350
public func sync<T>(flags: DispatchWorkItemFlags, execute work: () throws -> T) rethrows -> T {
265351
if flags == .barrier {
266352
return try self._syncHelper(fn: _syncBarrier, execute: work, rescue: { throw $0 })
@@ -271,6 +357,23 @@ public extension DispatchQueue {
271357
}
272358
}
273359

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+
///
274377
public func asyncAfter(
275378
deadline: DispatchTime,
276379
qos: DispatchQoS = .unspecified,
@@ -285,6 +388,23 @@ public extension DispatchQueue {
285388
}
286389
}
287390

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+
///
288408
public func asyncAfter(
289409
wallDeadline: DispatchWallTime,
290410
qos: DispatchQoS = .unspecified,
@@ -299,11 +419,31 @@ public extension DispatchQueue {
299419
}
300420
}
301421

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+
///
302432
@available(macOS 10.10, iOS 8.0, *)
303433
public func asyncAfter(deadline: DispatchTime, execute: DispatchWorkItem) {
304434
CDispatch.dispatch_after(deadline.rawValue, self.__wrapped, execute._block)
305435
}
306436

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+
///
307447
@available(macOS 10.10, iOS 8.0, *)
308448
public func asyncAfter(wallDeadline: DispatchWallTime, execute: DispatchWorkItem) {
309449
CDispatch.dispatch_after(wallDeadline.rawValue, self.__wrapped, execute._block)

0 commit comments

Comments
 (0)