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 class DispatchObject {
20 internal func wrapped() -> dispatch_object_t {
21 fatalError("should be overriden in subclass")
24 public func setTarget(queue:DispatchQueue) {
25 dispatch_set_target_queue(wrapped(), queue.__wrapped)
28 public func activate() {
29 dispatch_activate(wrapped())
32 public func suspend() {
33 dispatch_suspend(wrapped())
36 public func resume() {
37 dispatch_resume(wrapped())
42 public class DispatchGroup : DispatchObject {
43 internal let __wrapped:dispatch_group_t;
45 final internal override func wrapped() -> dispatch_object_t {
46 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
49 public override init() {
50 __wrapped = dispatch_group_create()
54 _swift_dispatch_release(wrapped())
58 dispatch_group_enter(__wrapped)
62 dispatch_group_enter(__wrapped)
66 public class DispatchSemaphore : DispatchObject {
67 internal let __wrapped: dispatch_semaphore_t;
69 final internal override func wrapped() -> dispatch_object_t {
70 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
73 public init(value: Int) {
74 __wrapped = dispatch_semaphore_create(value)
78 _swift_dispatch_release(wrapped())
82 public class DispatchIO : DispatchObject {
83 internal let __wrapped:dispatch_io_t
85 final internal override func wrapped() -> dispatch_object_t {
86 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
89 internal init(__type: UInt, fd: Int32, queue: DispatchQueue,
90 handler: (error: Int32) -> Void) {
91 __wrapped = dispatch_io_create(__type, fd, queue.__wrapped, handler)
94 internal init(__type: UInt, path: UnsafePointer<Int8>, oflag: Int32,
95 mode: mode_t, queue: DispatchQueue, handler: (error: Int32) -> Void) {
96 __wrapped = dispatch_io_create_with_path(__type, path, oflag, mode, queue.__wrapped, handler)
99 internal init(__type: UInt, io: DispatchIO,
100 queue: DispatchQueue, handler: (error: Int32) -> Void) {
101 __wrapped = dispatch_io_create_with_io(__type, io.__wrapped, queue.__wrapped, handler)
104 internal init(queue:dispatch_queue_t) {
109 _swift_dispatch_release(wrapped())
112 public func barrier(execute: () -> ()) {
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: @noescape ()->()) {
153 dispatch_sync(self.__wrapped, workItem)
157 public class DispatchSource : DispatchObject,
158 DispatchSourceType, DispatchSourceRead,
159 DispatchSourceSignal, DispatchSourceTimer,
160 DispatchSourceUserDataAdd, DispatchSourceUserDataOr,
161 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 {
184 extension DispatchSource : DispatchSourceProcess,
185 DispatchSourceFileSystemObject {
189 public typealias DispatchSourceHandler = @convention(block) () -> Void
191 public protocol DispatchSourceType {
192 func setEventHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
194 func setEventHandler(handler: DispatchWorkItem)
196 func setCancelHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
198 func setCancelHandler(handler: DispatchWorkItem)
200 func setRegistrationHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
202 func setRegistrationHandler(handler: DispatchWorkItem)
210 var handle: UInt { get }
212 var mask: UInt { get }
214 var data: UInt { get }
216 var isCancelled: Bool { get }
219 public protocol DispatchSourceUserDataAdd : DispatchSourceType {
220 func mergeData(value: UInt)
223 public protocol DispatchSourceUserDataOr {
224 #if false /*FIXME: clashes with UserDataAdd?? */
225 func mergeData(value: UInt)
230 public protocol DispatchSourceMachSend : DispatchSourceType {
231 public var handle: mach_port_t { get }
233 public var data: DispatchSource.MachSendEvent { get }
235 public var mask: DispatchSource.MachSendEvent { get }
240 public protocol DispatchSourceMachReceive : DispatchSourceType {
241 var handle: mach_port_t { get }
246 public protocol DispatchSourceMemoryPressure : DispatchSourceType {
247 public var data: DispatchSource.MemoryPressureEvent { get }
249 public var mask: DispatchSource.MemoryPressureEvent { get }
254 public protocol DispatchSourceProcess : DispatchSourceType {
255 var handle: pid_t { get }
257 var data: DispatchSource.ProcessEvent { get }
259 var mask: DispatchSource.ProcessEvent { get }
263 public protocol DispatchSourceRead : DispatchSourceType {
266 public protocol DispatchSourceSignal : DispatchSourceType {
269 public protocol DispatchSourceTimer : DispatchSourceType {
270 func setTimer(start: DispatchTime, leeway: DispatchTimeInterval)
272 func setTimer(walltime start: DispatchWallTime, leeway: DispatchTimeInterval)
274 func setTimer(start: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
276 func setTimer(start: DispatchTime, interval: Double, leeway: DispatchTimeInterval)
278 func setTimer(walltime start: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
280 func setTimer(walltime start: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
284 public protocol DispatchSourceFileSystemObject : DispatchSourceType {
285 var handle: Int32 { get }
287 var data: DispatchSource.FileSystemEvent { get }
289 var mask: DispatchSource.FileSystemEvent { get }
293 public protocol DispatchSourceWrite : DispatchSourceType {
297 internal enum _OSQoSClass : UInt32 {
298 case QOS_CLASS_USER_INTERACTIVE = 0x21
299 case QOS_CLASS_USER_INITIATED = 0x19
300 case QOS_CLASS_DEFAULT = 0x15
301 case QOS_CLASS_UTILITY = 0x11
302 case QOS_CLASS_BACKGROUND = 0x09
303 case QOS_CLASS_UNSPECIFIED = 0x00
305 internal init?(qosClass:dispatch_qos_class_t) {
307 case 0x21: self = .QOS_CLASS_USER_INTERACTIVE
308 case 0x19: self = .QOS_CLASS_USER_INITIATED
309 case 0x15: self = .QOS_CLASS_DEFAULT
310 case 0x11: self = QOS_CLASS_UTILITY
311 case 0x09: self = QOS_CLASS_BACKGROUND
312 case 0x00: self = QOS_CLASS_UNSPECIFIED
318 @_silgen_name("_swift_dispatch_release")
319 internal func _swift_dispatch_release(_ obj: dispatch_object_t) -> Void