]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Wrapper.swift
libdispatch-703.1.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 class DispatchObject {
19
20 internal func wrapped() -> dispatch_object_t {
21 fatalError("should be overriden in subclass")
22 }
23
24 public func setTarget(queue:DispatchQueue) {
25 dispatch_set_target_queue(wrapped(), queue.__wrapped)
26 }
27
28 public func activate() {
29 dispatch_activate(wrapped())
30 }
31
32 public func suspend() {
33 dispatch_suspend(wrapped())
34 }
35
36 public func resume() {
37 dispatch_resume(wrapped())
38 }
39 }
40
41
42 public class DispatchGroup : DispatchObject {
43 internal let __wrapped:dispatch_group_t;
44
45 final internal override func wrapped() -> dispatch_object_t {
46 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
47 }
48
49 public override init() {
50 __wrapped = dispatch_group_create()
51 }
52
53 deinit {
54 _swift_dispatch_release(wrapped())
55 }
56
57 public func enter() {
58 dispatch_group_enter(__wrapped)
59 }
60
61 public func leave() {
62 dispatch_group_enter(__wrapped)
63 }
64 }
65
66 public class DispatchSemaphore : DispatchObject {
67 internal let __wrapped: dispatch_semaphore_t;
68
69 final internal override func wrapped() -> dispatch_object_t {
70 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
71 }
72
73 public init(value: Int) {
74 __wrapped = dispatch_semaphore_create(value)
75 }
76
77 deinit {
78 _swift_dispatch_release(wrapped())
79 }
80 }
81
82 public class DispatchIO : DispatchObject {
83 internal let __wrapped:dispatch_io_t
84
85 final internal override func wrapped() -> dispatch_object_t {
86 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
87 }
88
89 internal init(__type: UInt, fd: Int32, queue: DispatchQueue,
90 handler: (error: Int32) -> Void) {
91 __wrapped = dispatch_io_create(__type, fd, queue.__wrapped, handler)
92 }
93
94 internal init(__type: UInt, path: UnsafePointer<Int8>, oflag: Int32,
95 mode: mode_t, queue: DispatchQueue, handler: (error: Int32) -> Void) {
96 __wrapped = dispatch_io_create_with_path(__type, path, oflag, mode, queue.__wrapped, handler)
97 }
98
99 internal init(__type: UInt, io: DispatchIO,
100 queue: DispatchQueue, handler: (error: Int32) -> Void) {
101 __wrapped = dispatch_io_create_with_io(__type, io.__wrapped, queue.__wrapped, handler)
102 }
103
104 internal init(queue:dispatch_queue_t) {
105 __wrapped = queue
106 }
107
108 deinit {
109 _swift_dispatch_release(wrapped())
110 }
111
112 public func barrier(execute: () -> ()) {
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: @noescape ()->()) {
153 dispatch_sync(self.__wrapped, workItem)
154 }
155 }
156
157 public class DispatchSource : DispatchObject,
158 DispatchSourceType, 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 public typealias DispatchSourceHandler = @convention(block) () -> Void
190
191 public protocol DispatchSourceType {
192 func setEventHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
193
194 func setEventHandler(handler: DispatchWorkItem)
195
196 func setCancelHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
197
198 func setCancelHandler(handler: DispatchWorkItem)
199
200 func setRegistrationHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
201
202 func setRegistrationHandler(handler: DispatchWorkItem)
203
204 func cancel()
205
206 func resume()
207
208 func suspend()
209
210 var handle: UInt { get }
211
212 var mask: UInt { get }
213
214 var data: UInt { get }
215
216 var isCancelled: Bool { get }
217 }
218
219 public protocol DispatchSourceUserDataAdd : DispatchSourceType {
220 func mergeData(value: UInt)
221 }
222
223 public protocol DispatchSourceUserDataOr {
224 #if false /*FIXME: clashes with UserDataAdd?? */
225 func mergeData(value: UInt)
226 #endif
227 }
228
229 #if HAVE_MACH
230 public protocol DispatchSourceMachSend : DispatchSourceType {
231 public var handle: mach_port_t { get }
232
233 public var data: DispatchSource.MachSendEvent { get }
234
235 public var mask: DispatchSource.MachSendEvent { get }
236 }
237 #endif
238
239 #if HAVE_MACH
240 public protocol DispatchSourceMachReceive : DispatchSourceType {
241 var handle: mach_port_t { get }
242 }
243 #endif
244
245 #if HAVE_MACH
246 public protocol DispatchSourceMemoryPressure : DispatchSourceType {
247 public var data: DispatchSource.MemoryPressureEvent { get }
248
249 public var mask: DispatchSource.MemoryPressureEvent { get }
250 }
251 #endif
252
253 #if !os(Linux)
254 public protocol DispatchSourceProcess : DispatchSourceType {
255 var handle: pid_t { get }
256
257 var data: DispatchSource.ProcessEvent { get }
258
259 var mask: DispatchSource.ProcessEvent { get }
260 }
261 #endif
262
263 public protocol DispatchSourceRead : DispatchSourceType {
264 }
265
266 public protocol DispatchSourceSignal : DispatchSourceType {
267 }
268
269 public protocol DispatchSourceTimer : DispatchSourceType {
270 func setTimer(start: DispatchTime, leeway: DispatchTimeInterval)
271
272 func setTimer(walltime start: DispatchWallTime, leeway: DispatchTimeInterval)
273
274 func setTimer(start: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
275
276 func setTimer(start: DispatchTime, interval: Double, leeway: DispatchTimeInterval)
277
278 func setTimer(walltime start: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
279
280 func setTimer(walltime start: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
281 }
282
283 #if !os(Linux)
284 public protocol DispatchSourceFileSystemObject : DispatchSourceType {
285 var handle: Int32 { get }
286
287 var data: DispatchSource.FileSystemEvent { get }
288
289 var mask: DispatchSource.FileSystemEvent { get }
290 }
291 #endif
292
293 public protocol DispatchSourceWrite : DispatchSourceType {
294 }
295
296
297 internal enum _OSQoSClass : UInt32 {
298 case QOS_CLASS_USER_INTERACTIVE = 0x21
299 case QOS_CLASS_USER_INITIATED = 0x19
300 case QOS_CLASS_DEFAULT = 0x15
301 case QOS_CLASS_UTILITY = 0x11
302 case QOS_CLASS_BACKGROUND = 0x09
303 case QOS_CLASS_UNSPECIFIED = 0x00
304
305 internal init?(qosClass:dispatch_qos_class_t) {
306 switch qosClass {
307 case 0x21: self = .QOS_CLASS_USER_INTERACTIVE
308 case 0x19: self = .QOS_CLASS_USER_INITIATED
309 case 0x15: self = .QOS_CLASS_DEFAULT
310 case 0x11: self = QOS_CLASS_UTILITY
311 case 0x09: self = QOS_CLASS_BACKGROUND
312 case 0x00: self = QOS_CLASS_UNSPECIFIED
313 default: return nil
314 }
315 }
316 }
317
318 @_silgen_name("_swift_dispatch_release")
319 internal func _swift_dispatch_release(_ obj: dispatch_object_t) -> Void