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         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 internal class __DispatchData : DispatchObject {
 
 190         internal let __wrapped:dispatch_data_t
 
 191         internal let __owned:Bool
 
 193         final internal override func wrapped() -> dispatch_object_t {
 
 194                 return unsafeBitCast(__wrapped, to: dispatch_object_t.self)
 
 197         internal init(data:dispatch_data_t, owned:Bool) {
 
 204                         _swift_dispatch_release(wrapped())
 
 209 public typealias DispatchSourceHandler = @convention(block) () -> Void
 
 211 public protocol DispatchSourceProtocol {
 
 212         func setEventHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
 
 214         func setEventHandler(handler: DispatchWorkItem)
 
 216         func setCancelHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
 
 218         func setCancelHandler(handler: DispatchWorkItem)
 
 220         func setRegistrationHandler(qos: DispatchQoS, flags: DispatchWorkItemFlags, handler: DispatchSourceHandler?)
 
 222         func setRegistrationHandler(handler: DispatchWorkItem)
 
 230         var handle: UInt { get }
 
 232         var mask: UInt { get }
 
 234         var data: UInt { get }
 
 236         var isCancelled: Bool { get }
 
 239 public protocol DispatchSourceUserDataAdd : DispatchSourceProtocol {
 
 243 public protocol DispatchSourceUserDataOr : DispatchSourceProtocol {
 
 248 public protocol DispatchSourceMachSend : DispatchSourceProtocol {
 
 249         public var handle: mach_port_t { get }
 
 251         public var data: DispatchSource.MachSendEvent { get }
 
 253         public var mask: DispatchSource.MachSendEvent { get }
 
 258 public protocol DispatchSourceMachReceive : DispatchSourceProtocol {
 
 259         var handle: mach_port_t { get }
 
 264 public protocol DispatchSourceMemoryPressure : DispatchSourceProtocol {
 
 265         public var data: DispatchSource.MemoryPressureEvent { get }
 
 267         public var mask: DispatchSource.MemoryPressureEvent { get }
 
 272 public protocol DispatchSourceProcess : DispatchSourceProtocol {
 
 273         var handle: pid_t { get }
 
 275         var data: DispatchSource.ProcessEvent { get }
 
 277         var mask: DispatchSource.ProcessEvent { get }
 
 281 public protocol DispatchSourceRead : DispatchSourceProtocol {
 
 284 public protocol DispatchSourceSignal : DispatchSourceProtocol {
 
 287 public protocol DispatchSourceTimer : DispatchSourceProtocol {
 
 288         func scheduleOneshot(deadline: DispatchTime, leeway: DispatchTimeInterval)
 
 290         func scheduleOneshot(wallDeadline: DispatchWallTime, leeway: DispatchTimeInterval)
 
 292         func scheduleRepeating(deadline: DispatchTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
 
 294         func scheduleRepeating(deadline: DispatchTime, interval: Double, leeway: DispatchTimeInterval)
 
 296         func scheduleRepeating(wallDeadline: DispatchWallTime, interval: DispatchTimeInterval, leeway: DispatchTimeInterval)
 
 298         func scheduleRepeating(wallDeadline: DispatchWallTime, interval: Double, leeway: DispatchTimeInterval)
 
 302 public protocol DispatchSourceFileSystemObject : DispatchSourceProtocol {
 
 303         var handle: Int32 { get }
 
 305         var data: DispatchSource.FileSystemEvent { get }
 
 307         var mask: DispatchSource.FileSystemEvent { get }
 
 311 public protocol DispatchSourceWrite : DispatchSourceProtocol {
 
 315 internal enum _OSQoSClass : UInt32  {
 
 316         case QOS_CLASS_USER_INTERACTIVE = 0x21
 
 317         case QOS_CLASS_USER_INITIATED = 0x19
 
 318         case QOS_CLASS_DEFAULT = 0x15
 
 319         case QOS_CLASS_UTILITY = 0x11
 
 320         case QOS_CLASS_BACKGROUND = 0x09
 
 321         case QOS_CLASS_UNSPECIFIED = 0x00
 
 323         internal init?(qosClass:dispatch_qos_class_t) {
 
 325                 case 0x21: self = .QOS_CLASS_USER_INTERACTIVE
 
 326                 case 0x19: self = .QOS_CLASS_USER_INITIATED
 
 327                 case 0x15: self = .QOS_CLASS_DEFAULT
 
 328                 case 0x11: self = .QOS_CLASS_UTILITY
 
 329                 case 0x09: self = .QOS_CLASS_BACKGROUND
 
 330                 case 0x00: self = .QOS_CLASS_UNSPECIFIED
 
 336 @_silgen_name("_swift_dispatch_release")
 
 337 internal func _swift_dispatch_release(_ obj: dispatch_object_t) -> Void