]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Source.swift
libdispatch-703.1.4.tar.gz
[apple/libdispatch.git] / src / swift / Source.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 extension DispatchSourceType {
16
17 public func setEventHandler(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], handler: DispatchSourceHandler?) {
18 if #available(OSX 10.10, iOS 8.0, *), let h = handler where qos != .unspecified || !flags.isEmpty {
19 let item = DispatchWorkItem(qos: qos, flags: flags, block: h)
20 CDispatch.dispatch_source_set_event_handler((self as! DispatchSource).__wrapped, item._block)
21 } else {
22 CDispatch.dispatch_source_set_event_handler((self as! DispatchSource).__wrapped, handler)
23 }
24 }
25
26 @available(OSX 10.10, iOS 8.0, *)
27 public func setEventHandler(handler: DispatchWorkItem) {
28 CDispatch.dispatch_source_set_event_handler((self as! DispatchSource).__wrapped, handler._block)
29 }
30
31 public func setCancelHandler(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], handler: DispatchSourceHandler?) {
32 if #available(OSX 10.10, iOS 8.0, *), let h = handler where qos != .unspecified || !flags.isEmpty {
33 let item = DispatchWorkItem(qos: qos, flags: flags, block: h)
34 CDispatch.dispatch_source_set_cancel_handler((self as! DispatchSource).__wrapped, item._block)
35 } else {
36 CDispatch.dispatch_source_set_cancel_handler((self as! DispatchSource).__wrapped, handler)
37 }
38 }
39
40 @available(OSX 10.10, iOS 8.0, *)
41 public func setCancelHandler(handler: DispatchWorkItem) {
42 CDispatch.dispatch_source_set_cancel_handler((self as! DispatchSource).__wrapped, handler._block)
43 }
44
45 public func setRegistrationHandler(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], handler: DispatchSourceHandler?) {
46 if #available(OSX 10.10, iOS 8.0, *), let h = handler where qos != .unspecified || !flags.isEmpty {
47 let item = DispatchWorkItem(qos: qos, flags: flags, block: h)
48 CDispatch.dispatch_source_set_registration_handler((self as! DispatchSource).__wrapped, item._block)
49 } else {
50 CDispatch.dispatch_source_set_registration_handler((self as! DispatchSource).__wrapped, handler)
51 }
52 }
53
54 @available(OSX 10.10, iOS 8.0, *)
55 public func setRegistrationHandler(handler: DispatchWorkItem) {
56 CDispatch.dispatch_source_set_registration_handler((self as! DispatchSource).__wrapped, handler._block)
57 }
58
59 @available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
60 public func activate() {
61 (self as! DispatchSource).activate()
62 }
63
64 public func cancel() {
65 CDispatch.dispatch_source_cancel((self as! DispatchSource).__wrapped)
66 }
67
68 public func resume() {
69 (self as! DispatchSource).resume()
70 }
71
72 public func suspend() {
73 (self as! DispatchSource).suspend()
74 }
75
76 public var handle: UInt {
77 return CDispatch.dispatch_source_get_handle((self as! DispatchSource).__wrapped)
78 }
79
80 public var mask: UInt {
81 return CDispatch.dispatch_source_get_mask((self as! DispatchSource).__wrapped)
82 }
83
84 public var data: UInt {
85 return CDispatch.dispatch_source_get_data((self as! DispatchSource).__wrapped)
86 }
87
88 public var isCancelled: Bool {
89 return CDispatch.dispatch_source_testcancel((self as! DispatchSource).__wrapped) != 0
90 }
91 }
92
93 public extension DispatchSource {
94 #if HAVE_MACH
95 public struct MachSendEvent : OptionSet, RawRepresentable {
96 public let rawValue: UInt
97 public init(rawValue: UInt) { self.rawValue = rawValue }
98
99 public static let dead = MachSendEvent(rawValue: 0x1)
100 }
101 #endif
102
103 #if HAVE_MACH
104 public struct MemoryPressureEvent : OptionSet, RawRepresentable {
105 public let rawValue: UInt
106 public init(rawValue: UInt) { self.rawValue = rawValue }
107
108 public static let normal = MemoryPressureEvent(rawValue: 0x1)
109 public static let warning = MemoryPressureEvent(rawValue: 0x2)
110 public static let critical = MemoryPressureEvent(rawValue: 0x4)
111 public static let all: MemoryPressureEvent = [.normal, .warning, .critical]
112 }
113 #endif
114
115 #if !os(Linux)
116 public struct ProcessEvent : OptionSet, RawRepresentable {
117 public let rawValue: UInt
118 public init(rawValue: UInt) { self.rawValue = rawValue }
119
120 public static let exit = ProcessEvent(rawValue: 0x80000000)
121 public static let fork = ProcessEvent(rawValue: 0x40000000)
122 public static let exec = ProcessEvent(rawValue: 0x20000000)
123 public static let signal = ProcessEvent(rawValue: 0x08000000)
124 public static let all: ProcessEvent = [.exit, .fork, .exec, .signal]
125 }
126 #endif
127
128 public struct TimerFlags : OptionSet, RawRepresentable {
129 public let rawValue: UInt
130 public init(rawValue: UInt) { self.rawValue = rawValue }
131
132 public static let strict = TimerFlags(rawValue: 1)
133 }
134
135 public struct FileSystemEvent : OptionSet, RawRepresentable {
136 public let rawValue: UInt
137 public init(rawValue: UInt) { self.rawValue = rawValue }
138
139 public static let delete = FileSystemEvent(rawValue: 0x1)
140 public static let write = FileSystemEvent(rawValue: 0x2)
141 public static let extend = FileSystemEvent(rawValue: 0x4)
142 public static let attrib = FileSystemEvent(rawValue: 0x8)
143 public static let link = FileSystemEvent(rawValue: 0x10)
144 public static let rename = FileSystemEvent(rawValue: 0x20)
145 public static let revoke = FileSystemEvent(rawValue: 0x40)
146 public static let funlock = FileSystemEvent(rawValue: 0x100)
147
148 public static let all: FileSystemEvent = [
149 .delete, .write, .extend, .attrib, .link, .rename, .revoke]
150 }
151
152 #if HAVE_MACH
153 public class func machSend(port: mach_port_t, eventMask: MachSendEvent, queue: DispatchQueue? = nil) -> DispatchSourceMachSend {
154 let source = dispatch_source_create(_swift_dispatch_source_type_mach_send(), UInt(port), eventMask.rawValue, queue?.__wrapped)
155 return DispatchSource(source: source) as DispatchSourceMachSend
156 }
157 #endif
158
159 #if HAVE_MACH
160 public class func machReceive(port: mach_port_t, queue: DispatchQueue? = nil) -> DispatchSourceMachReceive {
161 let source = dispatch_source_create(_swift_dispatch_source_type_mach_recv(), UInt(port), 0, queue?.__wrapped)
162 return DispatchSource(source) as DispatchSourceMachReceive
163 }
164 #endif
165
166 #if HAVE_MACH
167 public class func memoryPressure(eventMask: MemoryPressureEvent, queue: DispatchQueue? = nil) -> DispatchSourceMemoryPressure {
168 let source = dispatch_source_create(_swift_dispatch_source_type_memorypressure(), 0, eventMask.rawValue, queue.__wrapped)
169 return DispatchSourceMemoryPressure(source)
170 }
171 #endif
172
173 #if !os(Linux)
174 public class func process(identifier: pid_t, eventMask: ProcessEvent, queue: DispatchQueue? = nil) -> DispatchSourceProcess {
175 let source = dispatch_source_create(_swift_dispatch_source_type_proc(), UInt(identifier), eventMask.rawValue, queue?.__wrapped)
176 return DispatchSource(source: source) as DispatchSourceProcess
177 }
178 #endif
179
180 public class func read(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceRead {
181 let source = dispatch_source_create(_swift_dispatch_source_type_read(), UInt(fileDescriptor), 0, queue?.__wrapped)
182 return DispatchSource(source: source) as DispatchSourceRead
183 }
184
185 public class func signal(signal: Int32, queue: DispatchQueue? = nil) -> DispatchSourceSignal {
186 let source = dispatch_source_create(_swift_dispatch_source_type_signal(), UInt(signal), 0, queue?.__wrapped)
187 return DispatchSource(source: source) as DispatchSourceSignal
188 }
189
190 public class func timer(flags: TimerFlags = [], queue: DispatchQueue? = nil) -> DispatchSourceTimer {
191 let source = dispatch_source_create(_swift_dispatch_source_type_timer(), 0, flags.rawValue, queue?.__wrapped)
192 return DispatchSource(source: source) as DispatchSourceTimer
193 }
194
195 public class func userDataAdd(queue: DispatchQueue? = nil) -> DispatchSourceUserDataAdd {
196 let source = dispatch_source_create(_swift_dispatch_source_type_data_add(), 0, 0, queue?.__wrapped)
197 return DispatchSource(source: source) as DispatchSourceUserDataAdd
198 }
199
200 public class func userDataOr(queue: DispatchQueue? = nil) -> DispatchSourceUserDataOr {
201 let source = dispatch_source_create(_swift_dispatch_source_type_data_or(), 0, 0, queue?.__wrapped)
202 return DispatchSource(source: source) as DispatchSourceUserDataOr
203 }
204
205 #if !os(Linux)
206 public class func fileSystemObject(fileDescriptor: Int32, eventMask: FileSystemEvent, queue: DispatchQueue? = nil) -> DispatchSourceFileSystemObject {
207 let source = dispatch_source_create(_swift_dispatch_source_type_vnode(), UInt(fileDescriptor), eventMask.rawValue, queue?.__wrapped)
208 return DispatchSource(source: source) as DispatchSourceFileSystemObject
209 }
210 #endif
211
212 public class func write(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceWrite {
213 let source = dispatch_source_create(_swift_dispatch_source_type_write(), UInt(fileDescriptor), 0, queue?.__wrapped)
214 return DispatchSource(source: source) as DispatchSourceWrite
215 }
216 }
217
218 #if HAVE_MACH
219 public extension DispatchSourceMachSend {
220 public var handle: mach_port_t {
221 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
222 }
223
224 public var data: DispatchSource.MachSendEvent {
225 let data = dispatch_source_get_data(self as! DispatchSource)
226 return DispatchSource.MachSendEvent(rawValue: data)
227 }
228
229 public var mask: DispatchSource.MachSendEvent {
230 let mask = dispatch_source_get_mask(self as! DispatchSource)
231 return DispatchSource.MachSendEvent(rawValue: mask)
232 }
233 }
234 #endif
235
236 #if HAVE_MACH
237 public extension DispatchSourceMachReceive {
238 public var handle: mach_port_t {
239 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
240 }
241 }
242 #endif
243
244 #if HAVE_MACH
245 public extension DispatchSourceMemoryPressure {
246 public var data: DispatchSource.MemoryPressureEvent {
247 let data = dispatch_source_get_data(self as! DispatchSource)
248 return DispatchSource.MemoryPressureEvent(rawValue: data)
249 }
250
251 public var mask: DispatchSource.MemoryPressureEvent {
252 let mask = dispatch_source_get_mask(self as! DispatchSource)
253 return DispatchSource.MemoryPressureEvent(rawValue: mask)
254 }
255 }
256 #endif
257
258 #if !os(Linux)
259 public extension DispatchSourceProcess {
260 public var handle: pid_t {
261 return pid_t(dispatch_source_get_handle(self as! DispatchSource))
262 }
263
264 public var data: DispatchSource.ProcessEvent {
265 let data = dispatch_source_get_data(self as! DispatchSource)
266 return DispatchSource.ProcessEvent(rawValue: data)
267 }
268
269 public var mask: DispatchSource.ProcessEvent {
270 let mask = dispatch_source_get_mask(self as! DispatchSource)
271 return DispatchSource.ProcessEvent(rawValue: mask)
272 }
273 }
274 #endif
275
276 public extension DispatchSourceTimer {
277 public func scheduleOneshot(deadline: DispatchTime, leeway: DispatchTimeInterval = .nanoseconds(0)) {
278 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, ~0, UInt64(leeway.rawValue))
279 }
280
281 public func scheduleOneshot(wallDeadline: DispatchWallTime, leeway: DispatchTimeInterval = .nanoseconds(0)) {
282 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, ~0, UInt64(leeway.rawValue))
283 }
284
285 public func scheduleRepeating(deadline: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0)) {
286 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, interval.rawValue, UInt64(leeway.rawValue))
287 }
288
289 public func scheduleRepeating(deadline: DispatchTime, interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
290 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, UInt64(interval * Double(NSEC_PER_SEC)), UInt64(leeway.rawValue))
291 }
292
293 public func scheduleRepeating(wallDeadline: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0)) {
294 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, interval.rawValue, UInt64(leeway.rawValue))
295 }
296
297 public func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
298 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, UInt64(interval * Double(NSEC_PER_SEC)), UInt64(leeway.rawValue))
299 }
300 }
301
302 public extension DispatchSourceTimer {
303 @available(*, deprecated, renamed: "DispatchSourceTimer.scheduleOneshot(self:deadline:leeway:)")
304 public func setTimer(start: DispatchTime, leeway: DispatchTimeInterval = .nanoseconds(0)) {
305 scheduleOneshot(deadline: start, leeway: leeway)
306 }
307
308 @available(*, deprecated, renamed: "DispatchSourceTimer.scheduleOneshot(self:wallDeadline:leeway:)")
309 public func setTimer(walltime start: DispatchWallTime, leeway: DispatchTimeInterval = .nanoseconds(0)) {
310 scheduleOneshot(wallDeadline: start, leeway: leeway)
311 }
312
313 @available(*, deprecated, renamed: "DispatchSourceTimer.scheduleRepeating(self:deadline:interval:leeway:)")
314 public func setTimer(start: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0)) {
315 scheduleRepeating(deadline: start, interval: interval, leeway: leeway)
316 }
317
318 @available(*, deprecated, renamed: "DispatchSourceTimer.scheduleRepeating(self:deadline:interval:leeway:)")
319 public func setTimer(start: DispatchTime, interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
320 scheduleRepeating(deadline: start, interval: interval, leeway: leeway)
321 }
322
323 @available(*, deprecated, renamed: "DispatchSourceTimer.scheduleRepeating(self:wallDeadline:interval:leeway:)")
324 public func setTimer(walltime start: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0)) {
325 scheduleRepeating(wallDeadline: start, interval: interval, leeway: leeway)
326 }
327
328 @available(*, deprecated, renamed: "DispatchSourceTimer.scheduleRepeating(self:wallDeadline:interval:leeway:)")
329 public func setTimer(walltime start: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
330 scheduleRepeating(wallDeadline: start, interval: interval, leeway: leeway)
331 }
332 }
333
334 #if !os(Linux)
335 public extension DispatchSourceFileSystemObject {
336 public var handle: Int32 {
337 return Int32(dispatch_source_get_handle((self as! DispatchSource).__wrapped))
338 }
339
340 public var data: DispatchSource.FileSystemEvent {
341 let data = dispatch_source_get_data((self as! DispatchSource).__wrapped)
342 return DispatchSource.FileSystemEvent(rawValue: data)
343 }
344
345 public var mask: DispatchSource.FileSystemEvent {
346 let data = dispatch_source_get_mask((self as! DispatchSource).__wrapped)
347 return DispatchSource.FileSystemEvent(rawValue: data)
348 }
349 }
350 #endif
351
352 public extension DispatchSourceUserDataAdd {
353 /// @function mergeData
354 ///
355 /// @abstract
356 /// Merges data into a dispatch source of type DISPATCH_SOURCE_TYPE_DATA_ADD or
357 /// DISPATCH_SOURCE_TYPE_DATA_OR and submits its event handler block to its
358 /// target queue.
359 ///
360 /// @param value
361 /// The value to coalesce with the pending data using a logical OR or an ADD
362 /// as specified by the dispatch source type. A value of zero has no effect
363 /// and will not result in the submission of the event handler block.
364 public func mergeData(value: UInt) {
365 dispatch_source_merge_data((self as! DispatchSource).__wrapped, value)
366 }
367 }
368
369 public extension DispatchSourceUserDataOr {
370 #if false /*FIXME: clashes with UserDataAdd?? */
371 /// @function mergeData
372 ///
373 /// @abstract
374 /// Merges data into a dispatch source of type DISPATCH_SOURCE_TYPE_DATA_ADD or
375 /// DISPATCH_SOURCE_TYPE_DATA_OR and submits its event handler block to its
376 /// target queue.
377 ///
378 /// @param value
379 /// The value to coalesce with the pending data using a logical OR or an ADD
380 /// as specified by the dispatch source type. A value of zero has no effect
381 /// and will not result in the submission of the event handler block.
382 public func mergeData(value: UInt) {
383 dispatch_source_merge_data((self as! DispatchSource).__wrapped, value)
384 }
385 #endif
386 }
387
388 @_silgen_name("_swift_dispatch_source_type_DATA_ADD")
389 internal func _swift_dispatch_source_type_data_add() -> dispatch_source_type_t
390
391 @_silgen_name("_swift_dispatch_source_type_DATA_OR")
392 internal func _swift_dispatch_source_type_data_or() -> dispatch_source_type_t
393
394 #if HAVE_MACH
395 @_silgen_name("_swift_dispatch_source_type_MACH_SEND")
396 internal func _swift_dispatch_source_type_mach_send() -> dispatch_source_type_t
397
398 @_silgen_name("_swift_dispatch_source_type_MACH_RECV")
399 internal func _swift_dispatch_source_type_mach_recv() -> dispatch_source_type_t
400
401 @_silgen_name("_swift_dispatch_source_type_MEMORYPRESSURE")
402 internal func _swift_dispatch_source_type_memorypressure() -> dispatch_source_type_t
403 #endif
404
405 #if !os(Linux)
406 @_silgen_name("_swift_dispatch_source_type_PROC")
407 internal func _swift_dispatch_source_type_proc() -> dispatch_source_type_t
408 #endif
409
410 @_silgen_name("_swift_dispatch_source_type_READ")
411 internal func _swift_dispatch_source_type_read() -> dispatch_source_type_t
412
413 @_silgen_name("_swift_dispatch_source_type_SIGNAL")
414 internal func _swift_dispatch_source_type_signal() -> dispatch_source_type_t
415
416 @_silgen_name("_swift_dispatch_source_type_TIMER")
417 internal func _swift_dispatch_source_type_timer() -> dispatch_source_type_t
418
419 #if !os(Linux)
420 @_silgen_name("_swift_dispatch_source_type_VNODE")
421 internal func _swift_dispatch_source_type_vnode() -> dispatch_source_type_t
422 #endif
423
424 @_silgen_name("_swift_dispatch_source_type_WRITE")
425 internal func _swift_dispatch_source_type_write() -> dispatch_source_type_t