1 //===----------------------------------------------------------------------===//
3 // This source file is part of the Swift.org open source project
5 // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11 //===----------------------------------------------------------------------===//
15 public struct DispatchWorkItemFlags : OptionSet, RawRepresentable {
16 public let rawValue: UInt
17 public init(rawValue: UInt) { self.rawValue = rawValue }
19 public static let barrier = DispatchWorkItemFlags(rawValue: 0x1)
21 @available(OSX 10.10, iOS 8.0, *)
22 public static let detached = DispatchWorkItemFlags(rawValue: 0x2)
24 @available(OSX 10.10, iOS 8.0, *)
25 public static let assignCurrentContext = DispatchWorkItemFlags(rawValue: 0x4)
27 @available(OSX 10.10, iOS 8.0, *)
28 public static let noQoS = DispatchWorkItemFlags(rawValue: 0x8)
30 @available(OSX 10.10, iOS 8.0, *)
31 public static let inheritQoS = DispatchWorkItemFlags(rawValue: 0x10)
33 @available(OSX 10.10, iOS 8.0, *)
34 public static let enforceQoS = DispatchWorkItemFlags(rawValue: 0x20)
37 @available(OSX 10.10, iOS 8.0, *)
38 public class DispatchWorkItem {
39 internal var _block: _DispatchBlock
41 public init(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @escaping @convention(block) () -> ()) {
42 _block = dispatch_block_create_with_qos_class(dispatch_block_flags_t(flags.rawValue),
43 qos.qosClass.rawValue.rawValue, Int32(qos.relativePriority), block)
46 // Used by DispatchQueue.synchronously<T> to provide a path through
47 // dispatch_block_t, as we know the lifetime of the block in question.
48 internal init(flags: DispatchWorkItemFlags = [], noescapeBlock: () -> ()) {
49 _block = _swift_dispatch_block_create_noescape(dispatch_block_flags_t(flags.rawValue), noescapeBlock)
52 public func perform() {
57 _ = dispatch_block_wait(_block, DispatchTime.distantFuture.rawValue)
60 public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
61 return dispatch_block_wait(_block, timeout.rawValue) == 0 ? .success : .timedOut
64 public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
65 return dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .success : .timedOut
69 qos: DispatchQoS = .unspecified,
70 flags: DispatchWorkItemFlags = [],
72 execute: @escaping @convention(block) () -> ())
74 if qos != .unspecified || !flags.isEmpty {
75 let item = DispatchWorkItem(qos: qos, flags: flags, block: execute)
76 dispatch_block_notify(_block, queue.__wrapped, item._block)
78 dispatch_block_notify(_block, queue.__wrapped, execute)
82 public func notify(queue: DispatchQueue, execute: DispatchWorkItem) {
83 dispatch_block_notify(_block, queue.__wrapped, execute._block)
86 public func cancel() {
87 dispatch_block_cancel(_block)
90 public var isCancelled: Bool {
91 return dispatch_block_testcancel(_block) != 0
95 /// The dispatch_block_t typealias is different from usual closures in that it
96 /// uses @convention(block). This is to avoid unnecessary bridging between
97 /// C blocks and Swift closures, which interferes with dispatch APIs that depend
98 /// on the referential identity of a block. Particularly, dispatch_block_create.
99 internal typealias _DispatchBlock = @convention(block) () -> Void
100 internal typealias dispatch_block_t = @convention(block) () -> Void
102 @_silgen_name("_swift_dispatch_block_create_noescape")
103 internal func _swift_dispatch_block_create_noescape(_ flags: dispatch_block_flags_t, _ block: () -> ()) -> _DispatchBlock