]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Block.swift
libdispatch-703.30.5.tar.gz
[apple/libdispatch.git] / src / swift / Block.swift
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 import CDispatch
14
15 public struct DispatchWorkItemFlags : OptionSet, RawRepresentable {
16 public let rawValue: UInt
17 public init(rawValue: UInt) { self.rawValue = rawValue }
18
19 public static let barrier = DispatchWorkItemFlags(rawValue: 0x1)
20
21 @available(OSX 10.10, iOS 8.0, *)
22 public static let detached = DispatchWorkItemFlags(rawValue: 0x2)
23
24 @available(OSX 10.10, iOS 8.0, *)
25 public static let assignCurrentContext = DispatchWorkItemFlags(rawValue: 0x4)
26
27 @available(OSX 10.10, iOS 8.0, *)
28 public static let noQoS = DispatchWorkItemFlags(rawValue: 0x8)
29
30 @available(OSX 10.10, iOS 8.0, *)
31 public static let inheritQoS = DispatchWorkItemFlags(rawValue: 0x10)
32
33 @available(OSX 10.10, iOS 8.0, *)
34 public static let enforceQoS = DispatchWorkItemFlags(rawValue: 0x20)
35 }
36
37 @available(OSX 10.10, iOS 8.0, *)
38 public class DispatchWorkItem {
39 internal var _block: _DispatchBlock
40 internal var _group: DispatchGroup?
41
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)
45 }
46
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)
51 }
52
53 public func perform() {
54 if let g = _group {
55 g.enter()
56 defer { g.leave() }
57 }
58 _block()
59 }
60
61 public func wait() {
62 _ = dispatch_block_wait(_block, DispatchTime.distantFuture.rawValue)
63 }
64
65 public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
66 return dispatch_block_wait(_block, timeout.rawValue) == 0 ? .Success : .TimedOut
67 }
68
69 public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
70 return dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .Success : .TimedOut
71 }
72
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)
77 } else {
78 dispatch_block_notify(_block, queue.__wrapped, execute)
79 }
80 }
81
82 public func notify(queue: DispatchQueue, execute: DispatchWorkItem) {
83 dispatch_block_notify(_block, queue.__wrapped, execute._block)
84 }
85
86 public func cancel() {
87 dispatch_block_cancel(_block)
88 }
89
90 public var isCancelled: Bool {
91 return dispatch_block_testcancel(_block) != 0
92 }
93 }
94
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
102 }
103 }
104 }
105
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
112
113 @_silgen_name("_swift_dispatch_block_create_noescape")
114 internal func _swift_dispatch_block_create_noescape(_ flags: dispatch_block_flags_t, _ block: @noescape () -> ()) -> _DispatchBlock