]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/DispatchStubs.cc
libdispatch-913.30.4.tar.gz
[apple/libdispatch.git] / src / swift / DispatchStubs.cc
1 //===----------------------------------------------------------------------===//
2 //
3 // This source file is part of the Swift.org open source project
4 //
5 // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
7 //
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include <dispatch/dispatch.h>
14 #include <stdio.h>
15
16 #define DISPATCH_RUNTIME_STDLIB_INTERFACE __attribute__((__visibility__("default")))
17
18 #if USE_OBJC
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;
32
33 // #include <dispatch/private.h>
34 __attribute__((constructor))
35 static void _dispatch_overlay_constructor() {
36 Class source = objc_lookUpClass("OS_dispatch_source");
37 if (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));
51 }
52 }
53
54 #endif /* USE_OBJC */
55
56
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))
64 #else
65 #define SWIFT_CC_swift
66 #endif
67
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);
86 #if !USE_OBJC
87 extern "C" void * objc_retainAutoreleasedReturnValue(void *obj);
88 #endif
89
90
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;
95 }
96
97 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
98 extern "C" void
99 _swift_dispatch_apply_current(size_t iterations, __attribute__((__noescape__)) void (^block)(size_t)) {
100 dispatch_apply(iterations, (dispatch_queue_t _Nonnull)0, block);
101 }
102
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();
107 }
108
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;
113 }
114
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;
119 }
120
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;
125 }
126
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;
131 }
132
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);
137 }
138
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);
143 }
144
145 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
146 extern "C" void
147 _swift_dispatch_block_cancel(dispatch_block_t block) {
148 dispatch_block_cancel(block);
149 }
150
151 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
152 extern "C" long
153 _swift_dispatch_block_wait(dispatch_block_t block, dispatch_time_t timeout) {
154 return dispatch_block_wait(block, timeout);
155 }
156
157 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
158 extern "C" void
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);
161 }
162
163 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
164 extern "C" long
165 _swift_dispatch_block_testcancel(dispatch_block_t block) {
166 return dispatch_block_testcancel(block);
167 }
168
169 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
170 extern "C" void
171 _swift_dispatch_async(dispatch_queue_t queue, dispatch_block_t block) {
172 dispatch_async(queue, block);
173 }
174
175 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
176 extern "C" void
177 _swift_dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block) {
178 dispatch_group_async(group, queue, block);
179 }
180
181 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
182 extern "C" void
183 _swift_dispatch_sync(dispatch_queue_t queue, dispatch_block_t block) {
184 dispatch_sync(queue, block);
185 }
186
187 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
188 extern "C" void
189 _swift_dispatch_release(dispatch_object_t obj) {
190 dispatch_release(obj);
191 }
192
193 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
194 extern "C" void
195 _swift_dispatch_retain(dispatch_object_t obj) {
196 dispatch_retain(obj);
197 }
198
199 #define SOURCE(t) \
200 extern "C" dispatch_source_type_t _swift_dispatch_source_type_##t(void); \
201 SWIFT_CC(swift) \
202 DISPATCH_RUNTIME_STDLIB_INTERFACE extern "C" dispatch_source_type_t \
203 _swift_dispatch_source_type_##t(void) { \
204 return DISPATCH_SOURCE_TYPE_##t; \
205 }
206
207 SOURCE(DATA_ADD)
208 SOURCE(DATA_OR)
209 SOURCE(DATA_REPLACE)
210 #if HAVE_MACH
211 SOURCE(MACH_SEND)
212 SOURCE(MACH_RECV)
213 SOURCE(MEMORYPRESSURE)
214 #endif
215 #ifndef __linux__
216 SOURCE(PROC)
217 #endif
218 SOURCE(READ)
219 SOURCE(SIGNAL)
220 SOURCE(TIMER)
221 #ifndef __linux__
222 SOURCE(VNODE)
223 #endif
224 SOURCE(WRITE)
225
226 #if !USE_OBJC
227
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
235 // platforms.
236 extern "C" void swift_retain(void *);
237
238 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
239 extern "C" void * objc_retainAutoreleasedReturnValue(void *obj) {
240 if (obj) {
241 swift_retain(obj);
242 return obj;
243 }
244 else return NULL;
245 }
246
247 #endif // !USE_OBJC