|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +public struct DispatchWorkItemFlags : OptionSet, RawRepresentable { |
| 14 | + public let rawValue: UInt |
| 15 | + public init(rawValue: UInt) { self.rawValue = rawValue } |
| 16 | + |
| 17 | + public static let barrier = DispatchWorkItemFlags(rawValue: 0x1) |
| 18 | + |
| 19 | + @available(OSX 10.10, iOS 8.0, *) |
| 20 | + public static let detached = DispatchWorkItemFlags(rawValue: 0x2) |
| 21 | + |
| 22 | + @available(OSX 10.10, iOS 8.0, *) |
| 23 | + public static let assignCurrentContext = DispatchWorkItemFlags(rawValue: 0x4) |
| 24 | + |
| 25 | + @available(OSX 10.10, iOS 8.0, *) |
| 26 | + public static let noQoS = DispatchWorkItemFlags(rawValue: 0x8) |
| 27 | + |
| 28 | + @available(OSX 10.10, iOS 8.0, *) |
| 29 | + public static let inheritQoS = DispatchWorkItemFlags(rawValue: 0x10) |
| 30 | + |
| 31 | + @available(OSX 10.10, iOS 8.0, *) |
| 32 | + public static let enforceQoS = DispatchWorkItemFlags(rawValue: 0x20) |
| 33 | +} |
| 34 | + |
| 35 | +@available(OSX 10.10, iOS 8.0, *) |
| 36 | +public class DispatchWorkItem { |
| 37 | + internal var _block: _DispatchBlock |
| 38 | + internal var _group: DispatchGroup? |
| 39 | + |
| 40 | + public init(group: DispatchGroup? = nil, qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], block: @convention(block) () -> ()) { |
| 41 | + _block = _swift_dispatch_block_create_with_qos_class(__dispatch_block_flags_t(flags.rawValue), |
| 42 | + qos.qosClass.rawValue, Int32(qos.relativePriority), block) |
| 43 | + } |
| 44 | + |
| 45 | + // Used by DispatchQueue.synchronously<T> to provide a @noescape path through |
| 46 | + // dispatch_block_t, as we know the lifetime of the block in question. |
| 47 | + internal init(flags: DispatchWorkItemFlags = [], noescapeBlock: @noescape () -> ()) { |
| 48 | + _block = _swift_dispatch_block_create_noescape(__dispatch_block_flags_t(flags.rawValue), noescapeBlock) |
| 49 | + } |
| 50 | + |
| 51 | + public func perform() { |
| 52 | + if let g = _group { |
| 53 | + g.enter() |
| 54 | + defer { g.leave() } |
| 55 | + } |
| 56 | + _block() |
| 57 | + } |
| 58 | + |
| 59 | + public func wait() { |
| 60 | + _ = _swift_dispatch_block_wait(_block, DispatchTime.distantFuture.rawValue) |
| 61 | + } |
| 62 | + |
| 63 | + public func wait(timeout: DispatchTime) -> DispatchTimeoutResult { |
| 64 | + return _swift_dispatch_block_wait(_block, timeout.rawValue) == 0 ? .Success : .TimedOut |
| 65 | + } |
| 66 | + |
| 67 | + public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult { |
| 68 | + return _swift_dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .Success : .TimedOut |
| 69 | + } |
| 70 | + |
| 71 | + public func notify(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], queue: DispatchQueue, execute: @convention(block) () -> Void) { |
| 72 | + if qos != .unspecified || !flags.isEmpty { |
| 73 | + let item = DispatchWorkItem(qos: qos, flags: flags, block: execute) |
| 74 | + _swift_dispatch_block_notify(_block, queue, item._block) |
| 75 | + } else { |
| 76 | + _swift_dispatch_block_notify(_block, queue, execute) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public func notify(queue: DispatchQueue, execute: DispatchWorkItem) { |
| 81 | + _swift_dispatch_block_notify(_block, queue, execute._block) |
| 82 | + } |
| 83 | + |
| 84 | + public func cancel() { |
| 85 | + _swift_dispatch_block_cancel(_block) |
| 86 | + } |
| 87 | + |
| 88 | + public var isCancelled: Bool { |
| 89 | + return _swift_dispatch_block_testcancel(_block) != 0 |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +@available(OSX 10.10, iOS 8.0, *) |
| 94 | +public extension DispatchWorkItem { |
| 95 | + @available(*, deprecated, renamed: "DispatchWorkItem.wait(self:wallTimeout:)") |
| 96 | + public func wait(timeout: DispatchWallTime) -> Int { |
| 97 | + switch wait(wallTimeout: timeout) { |
| 98 | + case .Success: return 0 |
| 99 | + case .TimedOut: return Int(KERN_OPERATION_TIMED_OUT) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/// The dispatch_block_t typealias is different from usual closures in that it |
| 105 | +/// uses @convention(block). This is to avoid unnecessary bridging between |
| 106 | +/// C blocks and Swift closures, which interferes with dispatch APIs that depend |
| 107 | +/// on the referential identity of a block. Particularly, dispatch_block_create. |
| 108 | +internal typealias _DispatchBlock = @convention(block) () -> Void |
| 109 | + |
| 110 | +/// APINotes also removes the old dispatch_block_t typedef from the Dispatch module |
| 111 | +/// completely. In doing so it causes the dispatch_block_* API to lose their |
| 112 | +/// @convention(block) attributes. As such, all of the entry points are shimmed |
| 113 | +//// through Dispatch.mm with _DispatchBlock types. |
| 114 | +@_silgen_name("_swift_dispatch_block_create_with_qos_class") |
| 115 | +internal func _swift_dispatch_block_create_with_qos_class(_ flags: __dispatch_block_flags_t, _ qos: qos_class_t, _ relativePriority: Int32, _ block: _DispatchBlock) -> _DispatchBlock |
| 116 | + |
| 117 | +@_silgen_name("_swift_dispatch_block_create_noescape") |
| 118 | +internal func _swift_dispatch_block_create_noescape(_ flags: __dispatch_block_flags_t, _ block: @noescape () -> ()) -> _DispatchBlock |
| 119 | + |
| 120 | +@_silgen_name("_swift_dispatch_block_wait") |
| 121 | +internal func _swift_dispatch_block_wait(_ block: _DispatchBlock, _ timeout: UInt64) -> Int |
| 122 | + |
| 123 | +@_silgen_name("_swift_dispatch_block_notify") |
| 124 | +internal func _swift_dispatch_block_notify(_ block: _DispatchBlock, _ queue: DispatchQueue, _ notifier: _DispatchBlock) |
| 125 | + |
| 126 | +@_silgen_name("_swift_dispatch_block_cancel") |
| 127 | +internal func _swift_dispatch_block_cancel(_ block: _DispatchBlock) |
| 128 | +
|
| 129 | +@_silgen_name("_swift_dispatch_block_testcancel") |
| 130 | +internal func _swift_dispatch_block_testcancel(_ block: _DispatchBlock) -> Int |
| 131 | +
|
0 commit comments