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
40 internal var _group: DispatchGroup?
42 public init(group: DispatchGroup? = nil, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) {
43 _block = dispatch_block_create_with_qos_class(dispatch_block_flags_t(flags.rawValue),
44 qos.qosClass.rawValue.rawValue, Int32(qos.relativePriority), block)
47 // Used by DispatchQueue.synchronously<T> to provide a @noescape path through
48 // dispatch_block_t, as we know the lifetime of the block in question.
49 internal init(flags: DispatchWorkItemFlags = [], noescapeBlock: @noescape () -> ()) {
50 _block = _swift_dispatch_block_create_noescape(dispatch_block_flags_t(flags.rawValue), noescapeBlock)
53 public func perform() {
62 _ = dispatch_block_wait(_block, DispatchTime.distantFuture.rawValue)
65 public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
66 return dispatch_block_wait(_block, timeout.rawValue) == 0 ? .Success : .TimedOut
69 public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
70 return dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .Success : .TimedOut
73 public func notify(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], queue: DispatchQueue, execute: @convention(block) () -> Void) {
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 @available(OSX 10.10, iOS 8.0, *)
96 public extension DispatchWorkItem {
97 @available(*, deprecated, renamed: "DispatchWorkItem.wait(self:wallTimeout:)")
98 public func wait(timeout: DispatchWallTime) -> Int {
99 switch wait(wallTimeout: timeout) {
100 case .Success: return 0
101 case .TimedOut: return DispatchTimeoutResult.KERN_OPERATION_TIMED_OUT
106 /// The dispatch_block_t typealias is different from usual closures in that it
107 /// uses @convention(block). This is to avoid unnecessary bridging between
108 /// C blocks and Swift closures, which interferes with dispatch APIs that depend
109 /// on the referential identity of a block. Particularly, dispatch_block_create.
110 internal typealias _DispatchBlock = @convention(block) () -> Void
111 internal typealias dispatch_block_t = @convention(block) () -> Void
113 @_silgen_name("_swift_dispatch_block_create_noescape")
114 internal func _swift_dispatch_block_create_noescape(_ flags: dispatch_block_flags_t, _ block: @noescape () -> ()) -> _DispatchBlock