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 DispatchSourceType {
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)
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 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)
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 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)
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]
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 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
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
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)
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
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
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
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
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
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
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
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
219 public extension DispatchSourceMachSend {
220 public var handle: mach_port_t {
221 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
224 public var data: DispatchSource.MachSendEvent {
225 let data = dispatch_source_get_data(self as! DispatchSource)
226 return DispatchSource.MachSendEvent(rawValue: data)
229 public var mask: DispatchSource.MachSendEvent {
230 let mask = dispatch_source_get_mask(self as! DispatchSource)
231 return DispatchSource.MachSendEvent(rawValue: mask)
237 public extension DispatchSourceMachReceive {
238 public var handle: mach_port_t {
239 return mach_port_t(dispatch_source_get_handle(self as! DispatchSource))
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)
251 public var mask: DispatchSource.MemoryPressureEvent {
252 let mask = dispatch_source_get_mask(self as! DispatchSource)
253 return DispatchSource.MemoryPressureEvent(rawValue: mask)
259 public extension DispatchSourceProcess {
260 public var handle: pid_t {
261 return pid_t(dispatch_source_get_handle(self as! DispatchSource))
264 public var data: DispatchSource.ProcessEvent {
265 let data = dispatch_source_get_data(self as! DispatchSource)
266 return DispatchSource.ProcessEvent(rawValue: data)
269 public var mask: DispatchSource.ProcessEvent {
270 let mask = dispatch_source_get_mask(self as! DispatchSource)
271 return DispatchSource.ProcessEvent(rawValue: mask)
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))
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))
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))
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))
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))
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))
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)
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)
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)
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)
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)
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)
335 public extension DispatchSourceFileSystemObject {
336 public var handle: Int32 {
337 return Int32(dispatch_source_get_handle((self as! DispatchSource).__wrapped))
340 public var data: DispatchSource.FileSystemEvent {
341 let data = dispatch_source_get_data((self as! DispatchSource).__wrapped)
342 return DispatchSource.FileSystemEvent(rawValue: data)
345 public var mask: DispatchSource.FileSystemEvent {
346 let data = dispatch_source_get_mask((self as! DispatchSource).__wrapped)
347 return DispatchSource.FileSystemEvent(rawValue: data)
352 public extension DispatchSourceUserDataAdd {
353 /// @function mergeData
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
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)
369 public extension DispatchSourceUserDataOr {
370 #if false /*FIXME: clashes with UserDataAdd?? */
371 /// @function mergeData
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
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)
388 @_silgen_name("_swift_dispatch_source_type_DATA_ADD")
389 internal func _swift_dispatch_source_type_data_add() -> dispatch_source_type_t
391 @_silgen_name("_swift_dispatch_source_type_DATA_OR")
392 internal func _swift_dispatch_source_type_data_or() -> dispatch_source_type_t
395 @_silgen_name("_swift_dispatch_source_type_MACH_SEND")
396 internal func _swift_dispatch_source_type_mach_send() -> dispatch_source_type_t
398 @_silgen_name("_swift_dispatch_source_type_MACH_RECV")
399 internal func _swift_dispatch_source_type_mach_recv() -> dispatch_source_type_t
401 @_silgen_name("_swift_dispatch_source_type_MEMORYPRESSURE")
402 internal func _swift_dispatch_source_type_memorypressure() -> dispatch_source_type_t
406 @_silgen_name("_swift_dispatch_source_type_PROC")
407 internal func _swift_dispatch_source_type_proc() -> dispatch_source_type_t
410 @_silgen_name("_swift_dispatch_source_type_READ")
411 internal func _swift_dispatch_source_type_read() -> dispatch_source_type_t
413 @_silgen_name("_swift_dispatch_source_type_SIGNAL")
414 internal func _swift_dispatch_source_type_signal() -> dispatch_source_type_t
416 @_silgen_name("_swift_dispatch_source_type_TIMER")
417 internal func _swift_dispatch_source_type_timer() -> dispatch_source_type_t
420 @_silgen_name("_swift_dispatch_source_type_VNODE")
421 internal func _swift_dispatch_source_type_vnode() -> dispatch_source_type_t
424 @_silgen_name("_swift_dispatch_source_type_WRITE")
425 internal func _swift_dispatch_source_type_write() -> dispatch_source_type_t