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 //===----------------------------------------------------------------------===//
13 #include <dispatch/dispatch.h>
16 #define DISPATCH_RUNTIME_STDLIB_INTERFACE __attribute__((__visibility__("default")))
19 @protocol OS_dispatch_source
;
20 @protocol OS_dispatch_source_mach_send
;
21 @protocol OS_dispatch_source_mach_recv
;
22 @protocol OS_dispatch_source_memorypressure
;
23 @protocol OS_dispatch_source_proc
;
24 @protocol OS_dispatch_source_read
;
25 @protocol OS_dispatch_source_signal
;
26 @protocol OS_dispatch_source_timer
;
27 @protocol OS_dispatch_source_data_add
;
28 @protocol OS_dispatch_source_data_or
;
29 @protocol OS_dispatch_source_data_replace
;
30 @protocol OS_dispatch_source_vnode
;
31 @protocol OS_dispatch_source_write
;
33 // #include <dispatch/private.h>
34 __attribute__((constructor
))
35 static void _dispatch_overlay_constructor() {
36 Class source
= objc_lookUpClass("OS_dispatch_source");
38 class_addProtocol(source
, @
protocol(OS_dispatch_source
));
39 class_addProtocol(source
, @
protocol(OS_dispatch_source_mach_send
));
40 class_addProtocol(source
, @
protocol(OS_dispatch_source_mach_recv
));
41 class_addProtocol(source
, @
protocol(OS_dispatch_source_memorypressure
));
42 class_addProtocol(source
, @
protocol(OS_dispatch_source_proc
));
43 class_addProtocol(source
, @
protocol(OS_dispatch_source_read
));
44 class_addProtocol(source
, @
protocol(OS_dispatch_source_signal
));
45 class_addProtocol(source
, @
protocol(OS_dispatch_source_timer
));
46 class_addProtocol(source
, @
protocol(OS_dispatch_source_data_add
));
47 class_addProtocol(source
, @
protocol(OS_dispatch_source_data_or
));
48 class_addProtocol(source
, @
protocol(OS_dispatch_source_data_replace
));
49 class_addProtocol(source
, @
protocol(OS_dispatch_source_vnode
));
50 class_addProtocol(source
, @
protocol(OS_dispatch_source_write
));
57 // Replicate the SWIFT_CC(swift) calling convention macro from
58 // swift/include/swift/Runtime/Config.h because it is
59 // quite awkward to include Config.h and its recursive includes
60 // in dispatch. This define must be manually kept in synch
61 #define SWIFT_CC(CC) SWIFT_CC_##CC
62 #if SWIFT_USE_SWIFTCALL
63 #define SWIFT_CC_swift __attribute__((swiftcall))
65 #define SWIFT_CC_swift
68 extern "C" dispatch_queue_attr_t
_swift_dispatch_queue_concurrent(void);
69 extern "C" void _swift_dispatch_apply_current(size_t iterations
, __attribute__((__noescape__
)) void (^block
)(size_t));
70 extern "C" dispatch_queue_t
_swift_dispatch_get_main_queue(void);
71 extern "C" dispatch_data_t
_swift_dispatch_data_empty(void);
72 extern "C" dispatch_block_t
_swift_dispatch_data_destructor_default(void);
73 extern "C" dispatch_block_t
_swift_dispatch_data_destructor_free(void);
74 extern "C" dispatch_block_t
_swift_dispatch_data_destructor_munmap(void);
75 extern "C" dispatch_block_t
_swift_dispatch_block_create_with_qos_class(dispatch_block_flags_t flags
, dispatch_qos_class_t qos
, int relative_priority
, dispatch_block_t block
);
76 extern "C" dispatch_block_t
_swift_dispatch_block_create_noescape(dispatch_block_flags_t flags
, dispatch_block_t block
);
77 extern "C" void _swift_dispatch_block_cancel(dispatch_block_t block
);
78 extern "C" long _swift_dispatch_block_wait(dispatch_block_t block
, dispatch_time_t timeout
);
79 extern "C" void _swift_dispatch_block_notify(dispatch_block_t block
, dispatch_queue_t queue
, dispatch_block_t notification_block
);
80 extern "C" long _swift_dispatch_block_testcancel(dispatch_block_t block
);
81 extern "C" void _swift_dispatch_async(dispatch_queue_t queue
, dispatch_block_t block
);
82 extern "C" void _swift_dispatch_group_async(dispatch_group_t group
, dispatch_queue_t queue
, dispatch_block_t block
);
83 extern "C" void _swift_dispatch_sync(dispatch_queue_t queue
, dispatch_block_t block
);
84 extern "C" void _swift_dispatch_release(dispatch_object_t obj
);
85 extern "C" void _swift_dispatch_retain(dispatch_object_t obj
);
87 extern "C" void * objc_retainAutoreleasedReturnValue(void *obj
);
91 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
92 extern "C" dispatch_queue_attr_t
93 _swift_dispatch_queue_concurrent(void) {
94 return DISPATCH_QUEUE_CONCURRENT
;
97 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
99 _swift_dispatch_apply_current(size_t iterations
, __attribute__((__noescape__
)) void (^block
)(size_t)) {
100 dispatch_apply(iterations
, (dispatch_queue_t _Nonnull
)0, block
);
103 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
104 extern "C" dispatch_queue_t
105 _swift_dispatch_get_main_queue(void) {
106 return dispatch_get_main_queue();
109 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
110 extern "C" dispatch_data_t
111 _swift_dispatch_data_empty(void) {
112 return dispatch_data_empty
;
115 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
116 extern "C" dispatch_block_t
117 _swift_dispatch_data_destructor_default(void) {
118 return DISPATCH_DATA_DESTRUCTOR_DEFAULT
;
121 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
122 extern "C" dispatch_block_t
123 _swift_dispatch_data_destructor_free(void) {
124 return _dispatch_data_destructor_free
;
127 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
128 extern "C" dispatch_block_t
129 _swift_dispatch_data_destructor_munmap(void) {
130 return _dispatch_data_destructor_munmap
;
133 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
134 extern "C" dispatch_block_t
135 _swift_dispatch_block_create_with_qos_class(dispatch_block_flags_t flags
, dispatch_qos_class_t qos
, int relative_priority
, dispatch_block_t block
) {
136 return dispatch_block_create_with_qos_class(flags
, qos
, relative_priority
, block
);
139 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
140 extern "C" dispatch_block_t
141 _swift_dispatch_block_create_noescape(dispatch_block_flags_t flags
, dispatch_block_t block
) {
142 return dispatch_block_create(flags
, block
);
145 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
147 _swift_dispatch_block_cancel(dispatch_block_t block
) {
148 dispatch_block_cancel(block
);
151 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
153 _swift_dispatch_block_wait(dispatch_block_t block
, dispatch_time_t timeout
) {
154 return dispatch_block_wait(block
, timeout
);
157 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
159 _swift_dispatch_block_notify(dispatch_block_t block
, dispatch_queue_t queue
, dispatch_block_t notification_block
) {
160 dispatch_block_notify(block
, queue
, notification_block
);
163 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
165 _swift_dispatch_block_testcancel(dispatch_block_t block
) {
166 return dispatch_block_testcancel(block
);
169 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
171 _swift_dispatch_async(dispatch_queue_t queue
, dispatch_block_t block
) {
172 dispatch_async(queue
, block
);
175 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
177 _swift_dispatch_group_async(dispatch_group_t group
, dispatch_queue_t queue
, dispatch_block_t block
) {
178 dispatch_group_async(group
, queue
, block
);
181 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
183 _swift_dispatch_sync(dispatch_queue_t queue
, dispatch_block_t block
) {
184 dispatch_sync(queue
, block
);
187 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
189 _swift_dispatch_release(dispatch_object_t obj
) {
190 dispatch_release(obj
);
193 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
195 _swift_dispatch_retain(dispatch_object_t obj
) {
196 dispatch_retain(obj
);
200 extern "C" dispatch_source_type_t _swift_dispatch_source_type_##t(void); \
202 DISPATCH_RUNTIME_STDLIB_INTERFACE extern "C" dispatch_source_type_t \
203 _swift_dispatch_source_type_##t(void) { \
204 return DISPATCH_SOURCE_TYPE_##t; \
213 SOURCE(MEMORYPRESSURE
)
228 // For CF functions with 'Get' semantics, the compiler currently assumes that
229 // the result is autoreleased and must be retained. It does so on all platforms
230 // by emitting a call to objc_retainAutoreleasedReturnValue. On Darwin, this is
231 // implemented by the ObjC runtime. On non-ObjC platforms, there is no runtime,
232 // and therefore we have to stub it out here ourselves. The compiler will
233 // eventually call swift_release to balance the retain below. This is a
234 // workaround until the compiler no longer emits this callout on non-ObjC
236 extern "C" void swift_retain(void *);
238 SWIFT_CC(swift
) DISPATCH_RUNTIME_STDLIB_INTERFACE
239 extern "C" void * objc_retainAutoreleasedReturnValue(void *obj
) {