]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Wrapper.swift
libdispatch-913.30.4.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 DispatchSourceUserDataReplace, 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) && !os(Android)
184 extension DispatchSource : DispatchSourceProcess,
185 DispatchSourceFileSystemObject {
186 }
187 #endif
188
189 internal class __DispatchData : DispatchObject {
190 internal let __wrapped:dispatch_data_t
191
192 final internal override func wrapped() -> dispatch_object_t {
193 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
194 }
195
196 internal init(data:dispatch_data_t, owned:Bool) {
197 __wrapped = data
198 if !owned {
199 _swift_dispatch_retain(unsafeBitCast(data, to: dispatch_object_t.self))
200 }
201 }
202
203 deinit {
204 _swift_dispatch_release(wrapped())
205 }
206 }
207
208 public typealias DispatchSourceHandler = @convention(block) () -> Void
209
210 public protocol DispatchSourceProtocol {
211 func setEventHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
212
213 func setEventHandler(handler: DispatchWorkItem)
214
215 func setCancelHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
216
217 func setCancelHandler(handler: DispatchWorkItem)
218
219 func setRegistrationHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
220
221 func setRegistrationHandler(handler: DispatchWorkItem)
222
223 func cancel()
224
225 func resume()
226
227 func suspend()
228
229 var handle: UInt { get }
230
231 var mask: UInt { get }
232
233 var data: UInt { get }
234
235 var isCancelled: Bool { get }
236 }
237
238 public protocol DispatchSourceUserDataAdd : DispatchSourceProtocol {
239 func add(data: UInt)
240 }
241
242 public protocol DispatchSourceUserDataOr : DispatchSourceProtocol {
243 func or(data: UInt)
244 }
245
246 public protocol DispatchSourceUserDataReplace : DispatchSourceProtocol {
247 func replace(data: UInt)
248 }
249
250 #if HAVE_MACH
251 public protocol DispatchSourceMachSend : DispatchSourceProtocol {
252 public var handle: mach_port_t { get }
253
254 public var data: DispatchSource.MachSendEvent { get }
255
256 public var mask: DispatchSource.MachSendEvent { get }
257 }
258 #endif
259
260 #if HAVE_MACH
261 public protocol DispatchSourceMachReceive : DispatchSourceProtocol {
262 var handle: mach_port_t { get }
263 }
264 #endif
265
266 #if HAVE_MACH
267 public protocol DispatchSourceMemoryPressure : DispatchSourceProtocol {
268 public var data: DispatchSource.MemoryPressureEvent { get }
269
270 public var mask: DispatchSource.MemoryPressureEvent { get }
271 }
272 #endif
273
274 #if !os(Linux) && !os(Android)
275 public protocol DispatchSourceProcess : DispatchSourceProtocol {
276 var handle: pid_t { get }
277
278 var data: DispatchSource.ProcessEvent { get }
279
280 var mask: DispatchSource.ProcessEvent { get }
281 }
282 #endif
283
284 public protocol DispatchSourceRead : DispatchSourceProtocol {
285 }
286
287 public protocol DispatchSourceSignal : DispatchSourceProtocol {
288 }
289
290 public protocol DispatchSourceTimer : DispatchSourceProtocol {
291 func scheduleOneshot(deadline: DispatchTime, leeway: DispatchTimeInterval)
292
293 func scheduleOneshot(wallDeadline: DispatchWallTime, leeway: DispatchTimeInterval)
294
295 func scheduleRepeating(deadline: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
296
297 func scheduleRepeating(deadline: DispatchTime, interval: Double, leeway: DispatchTimeInterval)
298
299 func scheduleRepeating(wallDeadline: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
300
301 func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
302 }
303
304 #if !os(Linux) && !os(Android)
305 public protocol DispatchSourceFileSystemObject : DispatchSourceProtocol {
306 var handle: Int32 { get }
307
308 var data: DispatchSource.FileSystemEvent { get }
309
310 var mask: DispatchSource.FileSystemEvent { get }
311 }
312 #endif
313
314 public protocol DispatchSourceWrite : DispatchSourceProtocol {
315 }
316
317
318 internal enum _OSQoSClass : UInt32 {
319 case QOS_CLASS_USER_INTERACTIVE = 0x21
320 case QOS_CLASS_USER_INITIATED = 0x19
321 case QOS_CLASS_DEFAULT = 0x15
322 case QOS_CLASS_UTILITY = 0x11
323 case QOS_CLASS_BACKGROUND = 0x09
324 case QOS_CLASS_UNSPECIFIED = 0x00
325
326 internal init?(qosClass:dispatch_qos_class_t) {
327 switch qosClass {
328 case 0x21: self = .QOS_CLASS_USER_INTERACTIVE
329 case 0x19: self = .QOS_CLASS_USER_INITIATED
330 case 0x15: self = .QOS_CLASS_DEFAULT
331 case 0x11: self = .QOS_CLASS_UTILITY
332 case 0x09: self = .QOS_CLASS_BACKGROUND
333 case 0x00: self = .QOS_CLASS_UNSPECIFIED
334 default: return nil
335 }
336 }
337 }
338
339 @_silgen_name("_swift_dispatch_release")
340 internal func _swift_dispatch_release(_ obj: dispatch_object_t) -> Void
341
342 @_silgen_name("_swift_dispatch_retain")
343 internal func _swift_dispatch_retain(_ obj: dispatch_object_t) -> Void