]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/Source.swift
libdispatch-913.20.5.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 DispatchSourceProtocol {
16
17 public func setEventHandler(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], handler: DispatchSourceHandler?) {
18 if #available(OSX 10.10, iOS 8.0, *), let h = handler, 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, 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, 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) && !os(Android)
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 makeMachSendSource(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 makeMachReceiveSource(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 makeMemoryPressureSource(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) && !os(Android)
174 public class func makeProcessSource(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 makeReadSource(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 makeSignalSource(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 makeTimerSource(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 makeUserDataAddSource(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 makeUserDataOrSource(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 public class func makeUserDataReplaceSource(queue: DispatchQueue? = nil) -> DispatchSourceUserDataReplace {
206 let source = dispatch_source_create(_swift_dispatch_source_type_data_replace(), 0, 0, queue?.__wrapped)
207 return DispatchSource(source: source) as DispatchSourceUserDataReplace
208 }
209
210 #if !os(Linux) && !os(Android)
211 public class func makeFileSystemObjectSource(fileDescriptor: Int32, eventMask: FileSystemEvent, queue: DispatchQueue? = nil) -> DispatchSourceFileSystemObject {
212 let source = dispatch_source_create(_swift_dispatch_source_type_vnode(), UInt(fileDescriptor), eventMask.rawValue, queue?.__wrapped)
213 return DispatchSource(source: source) as DispatchSourceFileSystemObject
214 }
215 #endif
216
217 public class func makeWriteSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceWrite {
218 let source = dispatch_source_create(_swift_dispatch_source_type_write(), UInt(fileDescriptor), 0, queue?.__wrapped)
219 return DispatchSource(source: source) as DispatchSourceWrite
220 }
221 }
222
223 #if HAVE_MACH
224 public extension DispatchSourceMachSend {
225 public var handle: mach_port_t {
226 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
227 }
228
229 public var data: DispatchSource.MachSendEvent {
230 let data = dispatch_source_get_data(self as! DispatchSource)
231 return DispatchSource.MachSendEvent(rawValue: data)
232 }
233
234 public var mask: DispatchSource.MachSendEvent {
235 let mask = dispatch_source_get_mask(self as! DispatchSource)
236 return DispatchSource.MachSendEvent(rawValue: mask)
237 }
238 }
239 #endif
240
241 #if HAVE_MACH
242 public extension DispatchSourceMachReceive {
243 public var handle: mach_port_t {
244 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
245 }
246 }
247 #endif
248
249 #if HAVE_MACH
250 public extension DispatchSourceMemoryPressure {
251 public var data: DispatchSource.MemoryPressureEvent {
252 let data = dispatch_source_get_data(self as! DispatchSource)
253 return DispatchSource.MemoryPressureEvent(rawValue: data)
254 }
255
256 public var mask: DispatchSource.MemoryPressureEvent {
257 let mask = dispatch_source_get_mask(self as! DispatchSource)
258 return DispatchSource.MemoryPressureEvent(rawValue: mask)
259 }
260 }
261 #endif
262
263 #if !os(Linux) && !os(Android)
264 public extension DispatchSourceProcess {
265 public var handle: pid_t {
266 return pid_t(dispatch_source_get_handle(self as! DispatchSource))
267 }
268
269 public var data: DispatchSource.ProcessEvent {
270 let data = dispatch_source_get_data(self as! DispatchSource)
271 return DispatchSource.ProcessEvent(rawValue: data)
272 }
273
274 public var mask: DispatchSource.ProcessEvent {
275 let mask = dispatch_source_get_mask(self as! DispatchSource)
276 return DispatchSource.ProcessEvent(rawValue: mask)
277 }
278 }
279 #endif
280
281 public extension DispatchSourceTimer {
282 ///
283 /// Sets the deadline and leeway for a timer event that fires once.
284 ///
285 /// Once this function returns, any pending source data accumulated for the previous timer values
286 /// has been cleared and the next timer event will occur at `deadline`.
287 ///
288 /// Delivery of the timer event may be delayed by the system in order to improve power consumption
289 /// and system performance. The upper limit to the allowable delay may be configured with the `leeway`
290 /// argument; the lower limit is under the control of the system.
291 ///
292 /// The lower limit to the allowable delay may vary with process state such as visibility of the
293 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
294 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
295 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
296 /// this flag is specified.
297 ///
298 /// Calling this method has no effect if the timer source has already been canceled.
299 /// - note: Delivery of the timer event does not cancel the timer source.
300 ///
301 /// - parameter deadline: the time at which the timer event will be delivered, subject to the
302 /// leeway and other considerations described above. The deadline is based on Mach absolute
303 /// time.
304 /// - parameter leeway: the leeway for the timer.
305 ///
306 @available(swift, deprecated: 4, renamed: "schedule(deadline:repeating:leeway:)")
307 public func scheduleOneshot(deadline: DispatchTime, leeway: DispatchTimeInterval = .nanoseconds(0)) {
308 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, ~0, UInt64(leeway.rawValue))
309 }
310
311 ///
312 /// Sets the deadline and leeway for a timer event that fires once.
313 ///
314 /// Once this function returns, any pending source data accumulated for the previous timer values
315 /// has been cleared and the next timer event will occur at `wallDeadline`.
316 ///
317 /// Delivery of the timer event may be delayed by the system in order to improve power consumption
318 /// and system performance. The upper limit to the allowable delay may be configured with the `leeway`
319 /// argument; the lower limit is under the control of the system.
320 ///
321 /// The lower limit to the allowable delay may vary with process state such as visibility of the
322 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
323 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
324 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
325 /// this flag is specified.
326 ///
327 /// Calling this method has no effect if the timer source has already been canceled.
328 /// - note: Delivery of the timer event does not cancel the timer source.
329 ///
330 /// - parameter wallDeadline: the time at which the timer event will be delivered, subject to the
331 /// leeway and other considerations described above. The deadline is based on
332 /// `gettimeofday(3)`.
333 /// - parameter leeway: the leeway for the timer.
334 ///
335 @available(swift, deprecated: 4, renamed: "schedule(wallDeadline:repeating:leeway:)")
336 public func scheduleOneshot(wallDeadline: DispatchWallTime, leeway: DispatchTimeInterval = .nanoseconds(0)) {
337 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, ~0, UInt64(leeway.rawValue))
338 }
339
340 ///
341 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
342 ///
343 /// Once this function returns, any pending source data accumulated for the previous timer values
344 /// has been cleared. The next timer event will occur at `deadline` and every `interval` units of
345 /// time thereafter until the timer source is canceled.
346 ///
347 /// Delivery of a timer event may be delayed by the system in order to improve power consumption
348 /// and system performance. The upper limit to the allowable delay may be configured with the `leeway`
349 /// argument; the lower limit is under the control of the system.
350 ///
351 /// For the initial timer fire at `deadline`, the upper limit to the allowable delay is set to
352 /// `leeway`. For the subsequent timer fires at `deadline + N * interval`, the upper
353 /// limit is the smaller of `leeway` and `interval/2`.
354 ///
355 /// The lower limit to the allowable delay may vary with process state such as visibility of the
356 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
357 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
358 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
359 /// this flag is specified.
360 ///
361 /// Calling this method has no effect if the timer source has already been canceled.
362 ///
363 /// - parameter deadline: the time at which the timer event will be delivered, subject to the
364 /// leeway and other considerations described above. The deadline is based on Mach absolute
365 /// time.
366 /// - parameter interval: the interval for the timer.
367 /// - parameter leeway: the leeway for the timer.
368 ///
369 @available(swift, deprecated: 4, renamed: "schedule(deadline:repeating:leeway:)")
370 public func scheduleRepeating(deadline: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0)) {
371 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, interval == .never ? ~0 : UInt64(interval.rawValue), UInt64(leeway.rawValue))
372 }
373
374 ///
375 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
376 ///
377 /// Once this function returns, any pending source data accumulated for the previous timer values
378 /// has been cleared. The next timer event will occur at `deadline` and every `interval` seconds
379 /// thereafter until the timer source is canceled.
380 ///
381 /// Delivery of a timer event may be delayed by the system in order to improve power consumption and
382 /// system performance. The upper limit to the allowable delay may be configured with the `leeway`
383 /// argument; the lower limit is under the control of the system.
384 ///
385 /// For the initial timer fire at `deadline`, the upper limit to the allowable delay is set to
386 /// `leeway`. For the subsequent timer fires at `deadline + N * interval`, the upper
387 /// limit is the smaller of `leeway` and `interval/2`.
388 ///
389 /// The lower limit to the allowable delay may vary with process state such as visibility of the
390 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
391 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
392 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
393 /// this flag is specified.
394 ///
395 /// Calling this method has no effect if the timer source has already been canceled.
396 ///
397 /// - parameter deadline: the time at which the timer event will be delivered, subject to the
398 /// leeway and other considerations described above. The deadline is based on Mach absolute
399 /// time.
400 /// - parameter interval: the interval for the timer in seconds.
401 /// - parameter leeway: the leeway for the timer.
402 ///
403 @available(swift, deprecated: 4, renamed: "schedule(deadline:repeating:leeway:)")
404 public func scheduleRepeating(deadline: DispatchTime, interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
405 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, interval.isInfinite ? ~0 : UInt64(interval * Double(NSEC_PER_SEC)), UInt64(leeway.rawValue))
406 }
407
408 ///
409 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
410 ///
411 /// Once this function returns, any pending source data accumulated for the previous timer values
412 /// has been cleared. The next timer event will occur at `wallDeadline` and every `interval` units of
413 /// time thereafter until the timer source is canceled.
414 ///
415 /// Delivery of a timer event may be delayed by the system in order to improve power consumption and
416 /// system performance. The upper limit to the allowable delay may be configured with the `leeway`
417 /// argument; the lower limit is under the control of the system.
418 ///
419 /// For the initial timer fire at `wallDeadline`, the upper limit to the allowable delay is set to
420 /// `leeway`. For the subsequent timer fires at `wallDeadline + N * interval`, the upper
421 /// limit is the smaller of `leeway` and `interval/2`.
422 ///
423 /// The lower limit to the allowable delay may vary with process state such as visibility of the
424 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
425 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
426 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
427 /// this flag is specified.
428 ///
429 /// Calling this method has no effect if the timer source has already been canceled.
430 ///
431 /// - parameter wallDeadline: the time at which the timer event will be delivered, subject to the
432 /// leeway and other considerations described above. The deadline is based on
433 /// `gettimeofday(3)`.
434 /// - parameter interval: the interval for the timer.
435 /// - parameter leeway: the leeway for the timer.
436 ///
437 @available(swift, deprecated: 4, renamed: "schedule(wallDeadline:repeating:leeway:)")
438 public func scheduleRepeating(wallDeadline: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .nanoseconds(0)) {
439 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, interval == .never ? ~0 : UInt64(interval.rawValue), UInt64(leeway.rawValue))
440 }
441
442 ///
443 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
444 ///
445 /// Once this function returns, any pending source data accumulated for the previous timer values
446 /// has been cleared. The next timer event will occur at `wallDeadline` and every `interval` seconds
447 /// thereafter until the timer source is canceled.
448 ///
449 /// Delivery of a timer event may be delayed by the system in order to improve power consumption and
450 /// system performance. The upper limit to the allowable delay may be configured with the `leeway`
451 /// argument; the lower limit is under the control of the system.
452 ///
453 /// For the initial timer fire at `wallDeadline`, the upper limit to the allowable delay is set to
454 /// `leeway`. For the subsequent timer fires at `wallDeadline + N * interval`, the upper
455 /// limit is the smaller of `leeway` and `interval/2`.
456 ///
457 /// The lower limit to the allowable delay may vary with process state such as visibility of the
458 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
459 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
460 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
461 /// this flag is specified.
462 ///
463 /// Calling this method has no effect if the timer source has already been canceled.
464 ///
465 /// - parameter wallDeadline: the time at which the timer event will be delivered, subject to the
466 /// leeway and other considerations described above. The deadline is based on
467 /// `gettimeofday(3)`.
468 /// - parameter interval: the interval for the timer in seconds.
469 /// - parameter leeway: the leeway for the timer.
470 ///
471 @available(swift, deprecated: 4, renamed: "schedule(wallDeadline:repeating:leeway:)")
472 public func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
473 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, interval.isInfinite ? ~0 : UInt64(interval * Double(NSEC_PER_SEC)), UInt64(leeway.rawValue))
474 }
475
476 ///
477 /// Sets the deadline, repeat interval and leeway for a timer event.
478 ///
479 /// Once this function returns, any pending source data accumulated for the previous timer values
480 /// has been cleared. The next timer event will occur at `deadline` and every `repeating` units of
481 /// time thereafter until the timer source is canceled. If the value of `repeating` is `.never`,
482 /// or is defaulted, the timer fires only once.
483 ///
484 /// Delivery of a timer event may be delayed by the system in order to improve power consumption
485 /// and system performance. The upper limit to the allowable delay may be configured with the `leeway`
486 /// argument; the lower limit is under the control of the system.
487 ///
488 /// For the initial timer fire at `deadline`, the upper limit to the allowable delay is set to
489 /// `leeway`. For the subsequent timer fires at `deadline + N * repeating`, the upper
490 /// limit is the smaller of `leeway` and `repeating/2`.
491 ///
492 /// The lower limit to the allowable delay may vary with process state such as visibility of the
493 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
494 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
495 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
496 /// this flag is specified.
497 ///
498 /// Calling this method has no effect if the timer source has already been canceled.
499 ///
500 /// - parameter deadline: the time at which the first timer event will be delivered, subject to the
501 /// leeway and other considerations described above. The deadline is based on Mach absolute
502 /// time.
503 /// - parameter repeating: the repeat interval for the timer, or `.never` if the timer should fire
504 /// only once.
505 /// - parameter leeway: the leeway for the timer.
506 ///
507 @available(swift, introduced: 4)
508 public func schedule(deadline: DispatchTime, repeating interval: DispatchTimeInterval = .never, leeway: DispatchTimeInterval = .nanoseconds(0)) {
509 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, interval == .never ? ~0 : UInt64(interval.rawValue), UInt64(leeway.rawValue))
510 }
511
512 ///
513 /// Sets the deadline, repeat interval and leeway for a timer event.
514 ///
515 /// Once this function returns, any pending source data accumulated for the previous timer values
516 /// has been cleared. The next timer event will occur at `deadline` and every `repeating` seconds
517 /// thereafter until the timer source is canceled. If the value of `repeating` is `.infinity`,
518 /// the timer fires only once.
519 ///
520 /// Delivery of a timer event may be delayed by the system in order to improve power consumption
521 /// and system performance. The upper limit to the allowable delay may be configured with the `leeway`
522 /// argument; the lower limit is under the control of the system.
523 ///
524 /// For the initial timer fire at `deadline`, the upper limit to the allowable delay is set to
525 /// `leeway`. For the subsequent timer fires at `deadline + N * repeating`, the upper
526 /// limit is the smaller of `leeway` and `repeating/2`.
527 ///
528 /// The lower limit to the allowable delay may vary with process state such as visibility of the
529 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
530 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
531 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
532 /// this flag is specified.
533 ///
534 /// Calling this method has no effect if the timer source has already been canceled.
535 ///
536 /// - parameter deadline: the time at which the timer event will be delivered, subject to the
537 /// leeway and other considerations described above. The deadline is based on Mach absolute
538 /// time.
539 /// - parameter repeating: the repeat interval for the timer in seconds, or `.infinity` if the timer
540 /// should fire only once.
541 /// - parameter leeway: the leeway for the timer.
542 ///
543 @available(swift, introduced: 4)
544 public func schedule(deadline: DispatchTime, repeating interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
545 dispatch_source_set_timer((self as! DispatchSource).__wrapped, deadline.rawValue, interval.isInfinite ? ~0 : UInt64(interval * Double(NSEC_PER_SEC)), UInt64(leeway.rawValue))
546 }
547
548 ///
549 /// Sets the deadline, repeat interval and leeway for a timer event.
550 ///
551 /// Once this function returns, any pending source data accumulated for the previous timer values
552 /// has been cleared. The next timer event will occur at `wallDeadline` and every `repeating` units of
553 /// time thereafter until the timer source is canceled. If the value of `repeating` is `.never`,
554 /// or is defaulted, the timer fires only once.
555 ///
556 /// Delivery of a timer event may be delayed by the system in order to improve power consumption and
557 /// system performance. The upper limit to the allowable delay may be configured with the `leeway`
558 /// argument; the lower limit is under the control of the system.
559 ///
560 /// For the initial timer fire at `wallDeadline`, the upper limit to the allowable delay is set to
561 /// `leeway`. For the subsequent timer fires at `wallDeadline + N * repeating`, the upper
562 /// limit is the smaller of `leeway` and `repeating/2`.
563 ///
564 /// The lower limit to the allowable delay may vary with process state such as visibility of the
565 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
566 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
567 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
568 /// this flag is specified.
569 ///
570 /// Calling this method has no effect if the timer source has already been canceled.
571 ///
572 /// - parameter wallDeadline: the time at which the timer event will be delivered, subject to the
573 /// leeway and other considerations described above. The deadline is based on
574 /// `gettimeofday(3)`.
575 /// - parameter repeating: the repeat interval for the timer, or `.never` if the timer should fire
576 /// only once.
577 /// - parameter leeway: the leeway for the timer.
578 ///
579 @available(swift, introduced: 4)
580 public func schedule(wallDeadline: DispatchWallTime, repeating interval: DispatchTimeInterval = .never, leeway: DispatchTimeInterval = .nanoseconds(0)) {
581 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, interval == .never ? ~0 : UInt64(interval.rawValue), UInt64(leeway.rawValue))
582 }
583
584 ///
585 /// Sets the deadline, repeat interval and leeway for a timer event that fires at least once.
586 ///
587 /// Once this function returns, any pending source data accumulated for the previous timer values
588 /// has been cleared. The next timer event will occur at `wallDeadline` and every `repeating` seconds
589 /// thereafter until the timer source is canceled. If the value of `repeating` is `.infinity`,
590 /// the timer fires only once.
591 ///
592 /// Delivery of a timer event may be delayed by the system in order to improve power consumption
593 /// and system performance. The upper limit to the allowable delay may be configured with the `leeway`
594 /// argument; the lower limit is under the control of the system.
595 ///
596 /// For the initial timer fire at `wallDeadline`, the upper limit to the allowable delay is set to
597 /// `leeway`. For the subsequent timer fires at `wallDeadline + N * repeating`, the upper
598 /// limit is the smaller of `leeway` and `repeating/2`.
599 ///
600 /// The lower limit to the allowable delay may vary with process state such as visibility of the
601 /// application UI. If the timer source was created with flags `TimerFlags.strict`, the system
602 /// will make a best effort to strictly observe the provided `leeway` value, even if it is smaller
603 /// than the current lower limit. Note that a minimal amount of delay is to be expected even if
604 /// this flag is specified.
605 ///
606 /// Calling this method has no effect if the timer source has already been canceled.
607 ///
608 /// - parameter wallDeadline: the time at which the timer event will be delivered, subject to the
609 /// leeway and other considerations described above. The deadline is based on
610 /// `gettimeofday(3)`.
611 /// - parameter repeating: the repeat interval for the timer in seconds, or `.infinity` if the timer
612 /// should fire only once.
613 /// - parameter leeway: the leeway for the timer.
614 ///
615 @available(swift, introduced: 4)
616 public func schedule(wallDeadline: DispatchWallTime, repeating interval: Double, leeway: DispatchTimeInterval = .nanoseconds(0)) {
617 dispatch_source_set_timer((self as! DispatchSource).__wrapped, wallDeadline.rawValue, interval.isInfinite ? ~0 : UInt64(interval * Double(NSEC_PER_SEC)), UInt64(leeway.rawValue))
618 }
619 }
620
621 #if !os(Linux) && !os(Android)
622 public extension DispatchSourceFileSystemObject {
623 public var handle: Int32 {
624 return Int32(dispatch_source_get_handle((self as! DispatchSource).__wrapped))
625 }
626
627 public var data: DispatchSource.FileSystemEvent {
628 let data = dispatch_source_get_data((self as! DispatchSource).__wrapped)
629 return DispatchSource.FileSystemEvent(rawValue: data)
630 }
631
632 public var mask: DispatchSource.FileSystemEvent {
633 let data = dispatch_source_get_mask((self as! DispatchSource).__wrapped)
634 return DispatchSource.FileSystemEvent(rawValue: data)
635 }
636 }
637 #endif
638
639 public extension DispatchSourceUserDataAdd {
640 /// Merges data into a dispatch source of type `DISPATCH_SOURCE_TYPE_DATA_ADD`
641 /// and submits its event handler block to its target queue.
642 ///
643 /// - parameter data: the value to add to the current pending data. A value of zero
644 /// has no effect and will not result in the submission of the event handler block.
645 public func add(data: UInt) {
646 dispatch_source_merge_data((self as! DispatchSource).__wrapped, data)
647 }
648 }
649
650 public extension DispatchSourceUserDataOr {
651 /// Merges data into a dispatch source of type `DISPATCH_SOURCE_TYPE_DATA_OR` and
652 /// submits its event handler block to its target queue.
653 ///
654 /// - parameter data: The value to OR into the current pending data. A value of zero
655 /// has no effect and will not result in the submission of the event handler block.
656 public func or(data: UInt) {
657 dispatch_source_merge_data((self as! DispatchSource).__wrapped, data)
658 }
659 }
660
661 public extension DispatchSourceUserDataReplace {
662 /// Merges data into a dispatch source of type `DISPATCH_SOURCE_TYPE_DATA_REPLACE`
663 /// and submits its event handler block to its target queue.
664 ///
665 /// - parameter data: The value that will replace the current pending data.
666 /// A value of zero will be stored but will not result in the submission of the event
667 /// handler block.
668 public func replace(data: UInt) {
669 dispatch_source_merge_data((self as! DispatchSource).__wrapped, data)
670 }
671 }
672
673 @_silgen_name("_swift_dispatch_source_type_DATA_ADD")
674 internal func _swift_dispatch_source_type_data_add() -> dispatch_source_type_t
675
676 @_silgen_name("_swift_dispatch_source_type_DATA_OR")
677 internal func _swift_dispatch_source_type_data_or() -> dispatch_source_type_t
678
679 @_silgen_name("_swift_dispatch_source_type_DATA_REPLACE")
680 internal func _swift_dispatch_source_type_data_replace() -> dispatch_source_type_t
681
682 #if HAVE_MACH
683 @_silgen_name("_swift_dispatch_source_type_MACH_SEND")
684 internal func _swift_dispatch_source_type_mach_send() -> dispatch_source_type_t
685
686 @_silgen_name("_swift_dispatch_source_type_MACH_RECV")
687 internal func _swift_dispatch_source_type_mach_recv() -> dispatch_source_type_t
688
689 @_silgen_name("_swift_dispatch_source_type_MEMORYPRESSURE")
690 internal func _swift_dispatch_source_type_memorypressure() -> dispatch_source_type_t
691 #endif
692
693 #if !os(Linux) && !os(Android)
694 @_silgen_name("_swift_dispatch_source_type_PROC")
695 internal func _swift_dispatch_source_type_proc() -> dispatch_source_type_t
696 #endif
697
698 @_silgen_name("_swift_dispatch_source_type_READ")
699 internal func _swift_dispatch_source_type_read() -> dispatch_source_type_t
700
701 @_silgen_name("_swift_dispatch_source_type_SIGNAL")
702 internal func _swift_dispatch_source_type_signal() -> dispatch_source_type_t
703
704 @_silgen_name("_swift_dispatch_source_type_TIMER")
705 internal func _swift_dispatch_source_type_timer() -> dispatch_source_type_t
706
707 #if !os(Linux) && !os(Android)
708 @_silgen_name("_swift_dispatch_source_type_VNODE")
709 internal func _swift_dispatch_source_type_vnode() -> dispatch_source_type_t
710 #endif
711
712 @_silgen_name("_swift_dispatch_source_type_WRITE")
713 internal func _swift_dispatch_source_type_write() -> dispatch_source_type_t