1 //===----------------------------------------------------------------------===//
3 // This source file is part of the Swift.org open source project
5 // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11 //===----------------------------------------------------------------------===//
15 public extension DispatchSourceProtocol {
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)
22 CDispatch.dispatch_source_set_event_handler((self as! DispatchSource).__wrapped, handler)
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)
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)
36 CDispatch.dispatch_source_set_cancel_handler((self as! DispatchSource).__wrapped, handler)
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)
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)
50 CDispatch.dispatch_source_set_registration_handler((self as! DispatchSource).__wrapped, handler)
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)
59 @available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
60 public func activate() {
61 (self as! DispatchSource).activate()
64 public func cancel() {
65 CDispatch.dispatch_source_cancel((self as! DispatchSource).__wrapped)
68 public func resume() {
69 (self as! DispatchSource).resume()
72 public func suspend() {
73 (self as! DispatchSource).suspend()
76 public var handle: UInt {
77 return CDispatch.dispatch_source_get_handle((self as! DispatchSource).__wrapped)
80 public var mask: UInt {
81 return CDispatch.dispatch_source_get_mask((self as! DispatchSource).__wrapped)
84 public var data: UInt {
85 return CDispatch.dispatch_source_get_data((self as! DispatchSource).__wrapped)
88 public var isCancelled: Bool {
89 return CDispatch.dispatch_source_testcancel((self as! DispatchSource).__wrapped) != 0
93 public extension DispatchSource {
95 public struct MachSendEvent : OptionSet, RawRepresentable {
96 public let rawValue: UInt
97 public init(rawValue: UInt) { self.rawValue = rawValue }
99 public static let dead = MachSendEvent(rawValue: 0x1)
104 public struct MemoryPressureEvent : OptionSet, RawRepresentable {
105 public let rawValue: UInt
106 public init(rawValue: UInt) { self.rawValue = rawValue }
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]
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 }
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]
128 public struct TimerFlags : OptionSet, RawRepresentable {
129 public let rawValue: UInt
130 public init(rawValue: UInt) { self.rawValue = rawValue }
132 public static let strict = TimerFlags(rawValue: 1)
135 public struct FileSystemEvent : OptionSet, RawRepresentable {
136 public let rawValue: UInt
137 public init(rawValue: UInt) { self.rawValue = rawValue }
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)
148 public static let all: FileSystemEvent = [
149 .delete, .write, .extend, .attrib, .link, .rename, .revoke]
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
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
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)
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
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
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
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
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
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
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
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
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
224 public extension DispatchSourceMachSend {
225 public var handle: mach_port_t {
226 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
229 public var data: DispatchSource.MachSendEvent {
230 let data = dispatch_source_get_data(self as! DispatchSource)
231 return DispatchSource.MachSendEvent(rawValue: data)
234 public var mask: DispatchSource.MachSendEvent {
235 let mask = dispatch_source_get_mask(self as! DispatchSource)
236 return DispatchSource.MachSendEvent(rawValue: mask)
242 public extension DispatchSourceMachReceive {
243 public var handle: mach_port_t {
244 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
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)
256 public var mask: DispatchSource.MemoryPressureEvent {
257 let mask = dispatch_source_get_mask(self as! DispatchSource)
258 return DispatchSource.MemoryPressureEvent(rawValue: mask)
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))
269 public var data: DispatchSource.ProcessEvent {
270 let data = dispatch_source_get_data(self as! DispatchSource)
271 return DispatchSource.ProcessEvent(rawValue: data)
274 public var mask: DispatchSource.ProcessEvent {
275 let mask = dispatch_source_get_mask(self as! DispatchSource)
276 return DispatchSource.ProcessEvent(rawValue: mask)
281 public extension DispatchSourceTimer {
283 /// Sets the deadline and leeway for a timer event that fires once.
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`.
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.
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.
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.
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
304 /// - parameter leeway: the leeway for the timer.
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))
312 /// Sets the deadline and leeway for a timer event that fires once.
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`.
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.
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.
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.
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.
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))
341 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
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.
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.
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`.
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.
361 /// Calling this method has no effect if the timer source has already been canceled.
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
366 /// - parameter interval: the interval for the timer.
367 /// - parameter leeway: the leeway for the timer.
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))
375 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
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.
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.
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`.
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.
395 /// Calling this method has no effect if the timer source has already been canceled.
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
400 /// - parameter interval: the interval for the timer in seconds.
401 /// - parameter leeway: the leeway for the timer.
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))
409 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
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.
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.
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`.
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.
429 /// Calling this method has no effect if the timer source has already been canceled.
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.
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))
443 /// Sets the deadline, interval and leeway for a timer event that fires at least once.
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.
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.
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`.
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.
463 /// Calling this method has no effect if the timer source has already been canceled.
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.
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))
477 /// Sets the deadline, repeat interval and leeway for a timer event.
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.
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.
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`.
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.
498 /// Calling this method has no effect if the timer source has already been canceled.
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
503 /// - parameter repeating: the repeat interval for the timer, or `.never` if the timer should fire
505 /// - parameter leeway: the leeway for the timer.
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))
513 /// Sets the deadline, repeat interval and leeway for a timer event.
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.
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.
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`.
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.
534 /// Calling this method has no effect if the timer source has already been canceled.
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
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.
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))
549 /// Sets the deadline, repeat interval and leeway for a timer event.
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.
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.
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`.
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.
570 /// Calling this method has no effect if the timer source has already been canceled.
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
577 /// - parameter leeway: the leeway for the timer.
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))
585 /// Sets the deadline, repeat interval and leeway for a timer event that fires at least once.
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.
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.
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`.
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.
606 /// Calling this method has no effect if the timer source has already been canceled.
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.
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))
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))
627 public var data: DispatchSource.FileSystemEvent {
628 let data = dispatch_source_get_data((self as! DispatchSource).__wrapped)
629 return DispatchSource.FileSystemEvent(rawValue: data)
632 public var mask: DispatchSource.FileSystemEvent {
633 let data = dispatch_source_get_mask((self as! DispatchSource).__wrapped)
634 return DispatchSource.FileSystemEvent(rawValue: data)
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.
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)
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.
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)
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.
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
668 public func replace(data: UInt) {
669 dispatch_source_merge_data((self as! DispatchSource).__wrapped, data)
673 @_silgen_name("_swift_dispatch_source_type_DATA_ADD")
674 internal func _swift_dispatch_source_type_data_add() -> dispatch_source_type_t
676 @_silgen_name("_swift_dispatch_source_type_DATA_OR")
677 internal func _swift_dispatch_source_type_data_or() -> dispatch_source_type_t
679 @_silgen_name("_swift_dispatch_source_type_DATA_REPLACE")
680 internal func _swift_dispatch_source_type_data_replace() -> dispatch_source_type_t
683 @_silgen_name("_swift_dispatch_source_type_MACH_SEND")
684 internal func _swift_dispatch_source_type_mach_send() -> dispatch_source_type_t
686 @_silgen_name("_swift_dispatch_source_type_MACH_RECV")
687 internal func _swift_dispatch_source_type_mach_recv() -> dispatch_source_type_t
689 @_silgen_name("_swift_dispatch_source_type_MEMORYPRESSURE")
690 internal func _swift_dispatch_source_type_memorypressure() -> dispatch_source_type_t
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
698 @_silgen_name("_swift_dispatch_source_type_READ")
699 internal func _swift_dispatch_source_type_read() -> dispatch_source_type_t
701 @_silgen_name("_swift_dispatch_source_type_SIGNAL")
702 internal func _swift_dispatch_source_type_signal() -> dispatch_source_type_t
704 @_silgen_name("_swift_dispatch_source_type_TIMER")
705 internal func _swift_dispatch_source_type_timer() -> dispatch_source_type_t
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
712 @_silgen_name("_swift_dispatch_source_type_WRITE")
713 internal func _swift_dispatch_source_type_write() -> dispatch_source_type_t