]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Block.swift
libdispatch-703.50.37.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
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)
44 }
45
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)
50 }
51
52 public func perform() {
53 _block()
54 }
55
56 public func wait() {
57 _ = dispatch_block_wait(_block, DispatchTime.distantFuture.rawValue)
58 }
59
60 public func wait(timeout: DispatchTime) -> DispatchTimeoutResult {
61 return dispatch_block_wait(_block, timeout.rawValue) == 0 ? .success : .timedOut
62 }
63
64 public func wait(wallTimeout: DispatchWallTime) -> DispatchTimeoutResult {
65 return dispatch_block_wait(_block, wallTimeout.rawValue) == 0 ? .success : .timedOut
66 }
67
68 public func notify(
69 qos: DispatchQoS = .unspecified,
70 flags: DispatchWorkItemFlags = [],
71 queue: DispatchQueue,
72 execute: @escaping @convention(block) () -> ())
73 {
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 /// 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
101
102 @_silgen_name("_swift_dispatch_block_create_noescape")
103 internal func _swift_dispatch_block_create_noescape(_ flags: dispatch_block_flags_t, _ block: () -> ()) -> _DispatchBlock