]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Wrapper.swift
libdispatch-703.50.37.tar.gz
[apple/libdispatch.git] / src / swift / Wrapper.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 // This file contains declarations that are provided by the
16 // importer via Dispatch.apinote when the platform has Objective-C support
17
18 public func dispatchMain() -> Never {
19 CDispatch.dispatch_main()
20 }
21
22 public class DispatchObject {
23
24 internal func wrapped() -> dispatch_object_t {
25 fatalError("should be overriden in subclass")
26 }
27
28 public func setTarget(queue:DispatchQueue) {
29 dispatch_set_target_queue(wrapped(), queue.__wrapped)
30 }
31
32 public func activate() {
33 dispatch_activate(wrapped())
34 }
35
36 public func suspend() {
37 dispatch_suspend(wrapped())
38 }
39
40 public func resume() {
41 dispatch_resume(wrapped())
42 }
43 }
44
45
46 public class DispatchGroup : DispatchObject {
47 internal let __wrapped:dispatch_group_t;
48
49 final internal override func wrapped() -> dispatch_object_t {
50 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
51 }
52
53 public override init() {
54 __wrapped = dispatch_group_create()
55 }
56
57 deinit {
58 _swift_dispatch_release(wrapped())
59 }
60
61 public func enter() {
62 dispatch_group_enter(__wrapped)
63 }
64
65 public func leave() {
66 dispatch_group_leave(__wrapped)
67 }
68 }
69
70 public class DispatchSemaphore : DispatchObject {
71 internal let __wrapped: dispatch_semaphore_t;
72
73 final internal override func wrapped() -> dispatch_object_t {
74 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
75 }
76
77 public init(value: Int) {
78 __wrapped = dispatch_semaphore_create(value)
79 }
80
81 deinit {
82 _swift_dispatch_release(wrapped())
83 }
84 }
85
86 public class DispatchIO : DispatchObject {
87 internal let __wrapped:dispatch_io_t
88
89 final internal override func wrapped() -> dispatch_object_t {
90 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
91 }
92
93 internal init(__type: UInt, fd: Int32, queue: DispatchQueue,
94 handler: @escaping (_ error: Int32) -> Void) {
95 __wrapped = dispatch_io_create(__type, fd, queue.__wrapped, handler)
96 }
97
98 internal init(__type: UInt, path: UnsafePointer<Int8>, oflag: Int32,
99 mode: mode_t, queue: DispatchQueue, handler: @escaping (_ error: Int32) -> Void) {
100 __wrapped = dispatch_io_create_with_path(__type, path, oflag, mode, queue.__wrapped, handler)
101 }
102
103 internal init(__type: UInt, io: DispatchIO,
104 queue: DispatchQueue, handler: @escaping (_ error: Int32) -> Void) {
105 __wrapped = dispatch_io_create_with_io(__type, io.__wrapped, queue.__wrapped, handler)
106 }
107
108 deinit {
109 _swift_dispatch_release(wrapped())
110 }
111
112 public func barrier(execute: @escaping () -> ()) {
113 dispatch_io_barrier(self.__wrapped, execute)
114 }
115
116 public var fileDescriptor: Int32 {
117 return dispatch_io_get_descriptor(__wrapped)
118 }
119
120 public func setLimit(highWater: Int) {
121 dispatch_io_set_high_water(__wrapped, highWater)
122 }
123
124 public func setLimit(lowWater: Int) {
125 dispatch_io_set_low_water(__wrapped, lowWater)
126 }
127 }
128
129 public class DispatchQueue : DispatchObject {
130 internal let __wrapped:dispatch_queue_t;
131
132 final internal override func wrapped() -> dispatch_object_t {
133 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
134 }
135
136 internal init(__label: String, attr: dispatch_queue_attr_t?) {
137 __wrapped = dispatch_queue_create(__label, attr)
138 }
139
140 internal init(__label: String, attr: dispatch_queue_attr_t?, queue: DispatchQueue?) {
141 __wrapped = dispatch_queue_create_with_target(__label, attr, queue?.__wrapped)
142 }
143
144 internal init(queue:dispatch_queue_t) {
145 __wrapped = queue
146 }
147
148 deinit {
149 _swift_dispatch_release(wrapped())
150 }
151
152 public func sync(execute workItem: ()->()) {
153 dispatch_sync(self.__wrapped, workItem)
154 }
155 }
156
157 public class DispatchSource : DispatchObject,
158 DispatchSourceProtocol, DispatchSourceRead,
159 DispatchSourceSignal, DispatchSourceTimer,
160 DispatchSourceUserDataAdd, DispatchSourceUserDataOr,
161 DispatchSourceWrite {
162 internal let __wrapped:dispatch_source_t
163
164 final internal override func wrapped() -> dispatch_object_t {
165 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
166 }
167
168 internal init(source:dispatch_source_t) {
169 __wrapped = source
170 }
171
172 deinit {
173 _swift_dispatch_release(wrapped())
174 }
175 }
176
177 #if HAVE_MACH
178 extension DispatchSource : DispatchSourceMachSend,
179 DispatchSourceMachReceive, DispatchSourceMemoryPressure {
180 }
181 #endif
182
183 #if !os(Linux)
184 extension DispatchSource : DispatchSourceProcess,
185 DispatchSourceFileSystemObject {
186 }
187 #endif
188
189 internal class __DispatchData : DispatchObject {
190 internal let __wrapped:dispatch_data_t
191 internal let __owned:Bool
192
193 final internal override func wrapped() -> dispatch_object_t {
194 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
195 }
196
197 internal init(data:dispatch_data_t, owned:Bool) {
198 __wrapped = data
199 __owned = owned
200 }
201
202 deinit {
203 if __owned {
204 _swift_dispatch_release(wrapped())
205 }
206 }
207 }
208
209 public typealias DispatchSourceHandler = @convention(block) () -> Void
210
211 public protocol DispatchSourceProtocol {
212 func setEventHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
213
214 func setEventHandler(handler: DispatchWorkItem)
215
216 func setCancelHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
217
218 func setCancelHandler(handler: DispatchWorkItem)
219
220 func setRegistrationHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
221
222 func setRegistrationHandler(handler: DispatchWorkItem)
223
224 func cancel()
225
226 func resume()
227
228 func suspend()
229
230 var handle: UInt { get }
231
232 var mask: UInt { get }
233
234 var data: UInt { get }
235
236 var isCancelled: Bool { get }
237 }
238
239 public protocol DispatchSourceUserDataAdd : DispatchSourceProtocol {
240 func add(data: UInt)
241 }
242
243 public protocol DispatchSourceUserDataOr : DispatchSourceProtocol {
244 func or(data: UInt)
245 }
246
247 #if HAVE_MACH
248 public protocol DispatchSourceMachSend : DispatchSourceProtocol {
249 public var handle: mach_port_t { get }
250
251 public var data: DispatchSource.MachSendEvent { get }
252
253 public var mask: DispatchSource.MachSendEvent { get }
254 }
255 #endif
256
257 #if HAVE_MACH
258 public protocol DispatchSourceMachReceive : DispatchSourceProtocol {
259 var handle: mach_port_t { get }
260 }
261 #endif
262
263 #if HAVE_MACH
264 public protocol DispatchSourceMemoryPressure : DispatchSourceProtocol {
265 public var data: DispatchSource.MemoryPressureEvent { get }
266
267 public var mask: DispatchSource.MemoryPressureEvent { get }
268 }
269 #endif
270
271 #if !os(Linux)
272 public protocol DispatchSourceProcess : DispatchSourceProtocol {
273 var handle: pid_t { get }
274
275 var data: DispatchSource.ProcessEvent { get }
276
277 var mask: DispatchSource.ProcessEvent { get }
278 }
279 #endif
280
281 public protocol DispatchSourceRead : DispatchSourceProtocol {
282 }
283
284 public protocol DispatchSourceSignal : DispatchSourceProtocol {
285 }
286
287 public protocol DispatchSourceTimer : DispatchSourceProtocol {
288 func scheduleOneshot(deadline: DispatchTime, leeway: DispatchTimeInterval)
289
290 func scheduleOneshot(wallDeadline: DispatchWallTime, leeway: DispatchTimeInterval)
291
292 func scheduleRepeating(deadline: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
293
294 func scheduleRepeating(deadline: DispatchTime, interval: Double, leeway: DispatchTimeInterval)
295
296 func scheduleRepeating(wallDeadline: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
297
298 func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
299 }
300
301 #if !os(Linux)
302 public protocol DispatchSourceFileSystemObject : DispatchSourceProtocol {
303 var handle: Int32 { get }
304
305 var data: DispatchSource.FileSystemEvent { get }
306
307 var mask: DispatchSource.FileSystemEvent { get }
308 }
309 #endif
310
311 public protocol DispatchSourceWrite : DispatchSourceProtocol {
312 }
313
314
315 internal enum _OSQoSClass : UInt32 {
316 case QOS_CLASS_USER_INTERACTIVE = 0x21
317 case QOS_CLASS_USER_INITIATED = 0x19
318 case QOS_CLASS_DEFAULT = 0x15
319 case QOS_CLASS_UTILITY = 0x11
320 case QOS_CLASS_BACKGROUND = 0x09
321 case QOS_CLASS_UNSPECIFIED = 0x00
322
323 internal init?(qosClass:dispatch_qos_class_t) {
324 switch qosClass {
325 case 0x21: self = .QOS_CLASS_USER_INTERACTIVE
326 case 0x19: self = .QOS_CLASS_USER_INITIATED
327 case 0x15: self = .QOS_CLASS_DEFAULT
328 case 0x11: self = .QOS_CLASS_UTILITY
329 case 0x09: self = .QOS_CLASS_BACKGROUND
330 case 0x00: self = .QOS_CLASS_UNSPECIFIED
331 default: return nil
332 }
333 }
334 }
335
336 @_silgen_name("_swift_dispatch_release")
337 internal func _swift_dispatch_release(_ obj: dispatch_object_t) -> Void