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 // This file contains declarations that are provided by the
16 // importer via Dispatch.apinote when the platform has Objective-C support
18 public func dispatchMain() -> Never {
19 CDispatch.dispatch_main()
22 public class DispatchObject {
24 internal func wrapped() -> dispatch_object_t {
25 fatalError("should be overriden in subclass")
28 public func setTarget(queue:DispatchQueue) {
29 dispatch_set_target_queue(wrapped(), queue.__wrapped)
32 public func activate() {
33 dispatch_activate(wrapped())
36 public func suspend() {
37 dispatch_suspend(wrapped())
40 public func resume() {
41 dispatch_resume(wrapped())
46 public class DispatchGroup : DispatchObject {
47 internal let __wrapped:dispatch_group_t;
49 final internal override func wrapped() -> dispatch_object_t {
50 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
53 public override init() {
54 __wrapped = dispatch_group_create()
58 _swift_dispatch_release(wrapped())
62 dispatch_group_enter(__wrapped)
66 dispatch_group_leave(__wrapped)
70 public class DispatchSemaphore : DispatchObject {
71 internal let __wrapped: dispatch_semaphore_t;
73 final internal override func wrapped() -> dispatch_object_t {
74 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
77 public init(value: Int) {
78 __wrapped = dispatch_semaphore_create(value)
82 _swift_dispatch_release(wrapped())
86 public class DispatchIO : DispatchObject {
87 internal let __wrapped:dispatch_io_t
89 final internal override func wrapped() -> dispatch_object_t {
90 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
93 internal init(__type: UInt, fd: Int32, queue: DispatchQueue,
94 handler: @escaping (_ error: Int32) -> Void) {
95 __wrapped = dispatch_io_create(__type, fd, queue.__wrapped, handler)
98 internal init(__type: UInt, path: UnsafePointer<Int8>, oflag: Int32,
99 mode: mode_t, queue: DispatchQueue, handler: @escaping (_ error: Int32) -> Void) {
100 __wrapped = dispatch_io_create_with_path(__type, path, oflag, mode, queue.__wrapped, handler)
103 internal init(__type: UInt, io: DispatchIO,
104 queue: DispatchQueue, handler: @escaping (_ error: Int32) -> Void) {
105 __wrapped = dispatch_io_create_with_io(__type, io.__wrapped, queue.__wrapped, handler)
109 _swift_dispatch_release(wrapped())
112 public func barrier(execute: @escaping () -> ()) {
113 dispatch_io_barrier(self.__wrapped, execute)
116 public var fileDescriptor: Int32 {
117 return dispatch_io_get_descriptor(__wrapped)
120 public func setLimit(highWater: Int) {
121 dispatch_io_set_high_water(__wrapped, highWater)
124 public func setLimit(lowWater: Int) {
125 dispatch_io_set_low_water(__wrapped, lowWater)
129 public class DispatchQueue : DispatchObject {
130 internal let __wrapped:dispatch_queue_t;
132 final internal override func wrapped() -> dispatch_object_t {
133 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
136 internal init(__label: String, attr: dispatch_queue_attr_t?) {
137 __wrapped = dispatch_queue_create(__label, attr)
140 internal init(__label: String, attr: dispatch_queue_attr_t?, queue: DispatchQueue?) {
141 __wrapped = dispatch_queue_create_with_target(__label, attr, queue?.__wrapped)
144 internal init(queue:dispatch_queue_t) {
149 _swift_dispatch_release(wrapped())
152 public func sync(execute workItem: ()->()) {
153 dispatch_sync(self.__wrapped, workItem)
157 public class DispatchSource : DispatchObject,
158 DispatchSourceProtocol, DispatchSourceRead,
159 DispatchSourceSignal, DispatchSourceTimer,
160 DispatchSourceUserDataAdd, DispatchSourceUserDataOr,
161 DispatchSourceUserDataReplace, DispatchSourceWrite {
162 internal let __wrapped:dispatch_source_t
164 final internal override func wrapped() -> dispatch_object_t {
165 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
168 internal init(source:dispatch_source_t) {
173 _swift_dispatch_release(wrapped())
178 extension DispatchSource : DispatchSourceMachSend,
179 DispatchSourceMachReceive, DispatchSourceMemoryPressure {
183 #if !os(Linux) && !os(Android)
184 extension DispatchSource : DispatchSourceProcess,
185 DispatchSourceFileSystemObject {
189 internal class __DispatchData : DispatchObject {
190 internal let __wrapped:dispatch_data_t
192 final internal override func wrapped() -> dispatch_object_t {
193 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
196 internal init(data:dispatch_data_t, owned:Bool) {
199 _swift_dispatch_retain(unsafeBitCast(data, to: dispatch_object_t.self))
204 _swift_dispatch_release(wrapped())
208 public typealias DispatchSourceHandler = @convention(block) () -> Void
210 public protocol DispatchSourceProtocol {
211 func setEventHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
213 func setEventHandler(handler: DispatchWorkItem)
215 func setCancelHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
217 func setCancelHandler(handler: DispatchWorkItem)
219 func setRegistrationHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
221 func setRegistrationHandler(handler: DispatchWorkItem)
229 var handle: UInt { get }
231 var mask: UInt { get }
233 var data: UInt { get }
235 var isCancelled: Bool { get }
238 public protocol DispatchSourceUserDataAdd : DispatchSourceProtocol {
242 public protocol DispatchSourceUserDataOr : DispatchSourceProtocol {
246 public protocol DispatchSourceUserDataReplace : DispatchSourceProtocol {
247 func replace(data: UInt)
251 public protocol DispatchSourceMachSend : DispatchSourceProtocol {
252 public var handle: mach_port_t { get }
254 public var data: DispatchSource.MachSendEvent { get }
256 public var mask: DispatchSource.MachSendEvent { get }
261 public protocol DispatchSourceMachReceive : DispatchSourceProtocol {
262 var handle: mach_port_t { get }
267 public protocol DispatchSourceMemoryPressure : DispatchSourceProtocol {
268 public var data: DispatchSource.MemoryPressureEvent { get }
270 public var mask: DispatchSource.MemoryPressureEvent { get }
274 #if !os(Linux) && !os(Android)
275 public protocol DispatchSourceProcess : DispatchSourceProtocol {
276 var handle: pid_t { get }
278 var data: DispatchSource.ProcessEvent { get }
280 var mask: DispatchSource.ProcessEvent { get }
284 public protocol DispatchSourceRead : DispatchSourceProtocol {
287 public protocol DispatchSourceSignal : DispatchSourceProtocol {
290 public protocol DispatchSourceTimer : DispatchSourceProtocol {
291 func scheduleOneshot(deadline: DispatchTime, leeway: DispatchTimeInterval)
293 func scheduleOneshot(wallDeadline: DispatchWallTime, leeway: DispatchTimeInterval)
295 func scheduleRepeating(deadline: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
297 func scheduleRepeating(deadline: DispatchTime, interval: Double, leeway: DispatchTimeInterval)
299 func scheduleRepeating(wallDeadline: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
301 func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
304 #if !os(Linux) && !os(Android)
305 public protocol DispatchSourceFileSystemObject : DispatchSourceProtocol {
306 var handle: Int32 { get }
308 var data: DispatchSource.FileSystemEvent { get }
310 var mask: DispatchSource.FileSystemEvent { get }
314 public protocol DispatchSourceWrite : DispatchSourceProtocol {
318 internal enum _OSQoSClass : UInt32 {
319 case QOS_CLASS_USER_INTERACTIVE = 0x21
320 case QOS_CLASS_USER_INITIATED = 0x19
321 case QOS_CLASS_DEFAULT = 0x15
322 case QOS_CLASS_UTILITY = 0x11
323 case QOS_CLASS_BACKGROUND = 0x09
324 case QOS_CLASS_UNSPECIFIED = 0x00
326 internal init?(qosClass:dispatch_qos_class_t) {
328 case 0x21: self = .QOS_CLASS_USER_INTERACTIVE
329 case 0x19: self = .QOS_CLASS_USER_INITIATED
330 case 0x15: self = .QOS_CLASS_DEFAULT
331 case 0x11: self = .QOS_CLASS_UTILITY
332 case 0x09: self = .QOS_CLASS_BACKGROUND
333 case 0x00: self = .QOS_CLASS_UNSPECIFIED
339 @_silgen_name("_swift_dispatch_release")
340 internal func _swift_dispatch_release(_ obj: dispatch_object_t) -> Void
342 @_silgen_name("_swift_dispatch_retain")
343 internal func _swift_dispatch_retain(_ obj: dispatch_object_t) -> Void