]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/DispatchStubs.cc
libdispatch-703.50.37.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_vnode;
30 @protocol OS_dispatch_source_write;
31
32 // #include <dispatch/private.h>
33 __attribute__((constructor))
34 static void _dispatch_overlay_constructor() {
35 Class source = objc_lookUpClass("OS_dispatch_source");
36 if (source) {
37 class_addProtocol(source, @protocol(OS_dispatch_source));
38 class_addProtocol(source, @protocol(OS_dispatch_source_mach_send));
39 class_addProtocol(source, @protocol(OS_dispatch_source_mach_recv));
40 class_addProtocol(source, @protocol(OS_dispatch_source_memorypressure));
41 class_addProtocol(source, @protocol(OS_dispatch_source_proc));
42 class_addProtocol(source, @protocol(OS_dispatch_source_read));
43 class_addProtocol(source, @protocol(OS_dispatch_source_signal));
44 class_addProtocol(source, @protocol(OS_dispatch_source_timer));
45 class_addProtocol(source, @protocol(OS_dispatch_source_data_add));
46 class_addProtocol(source, @protocol(OS_dispatch_source_data_or));
47 class_addProtocol(source, @protocol(OS_dispatch_source_vnode));
48 class_addProtocol(source, @protocol(OS_dispatch_source_write));
49 }
50 }
51
52 #endif /* USE_OBJC */
53
54
55 // Replicate the SWIFT_CC(swift) calling convention macro from
56 // swift/include/swift/Runtime/Config.h because it is
57 // quite awkward to include Config.h and its recursive includes
58 // in dispatch. This define must be manually kept in synch
59 #define SWIFT_CC(CC) SWIFT_CC_##CC
60 #if SWIFT_USE_SWIFTCALL
61 #define SWIFT_CC_swift __attribute__((swiftcall))
62 #else
63 #define SWIFT_CC_swift
64 #endif
65
66 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
67 extern "C" dispatch_queue_attr_t
68 _swift_dispatch_queue_concurrent(void) {
69 return DISPATCH_QUEUE_CONCURRENT;
70 }
71
72 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
73 extern "C" void
74 _swift_dispatch_apply_current(size_t iterations, __attribute__((__noescape__)) void (^block)(size_t)) {
75 dispatch_apply(iterations, (dispatch_queue_t _Nonnull)0, block);
76 }
77
78 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
79 extern "C" dispatch_queue_t
80 _swift_dispatch_get_main_queue(void) {
81 return dispatch_get_main_queue();
82 }
83
84 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
85 extern "C" dispatch_data_t
86 _swift_dispatch_data_empty(void) {
87 return dispatch_data_empty;
88 }
89
90 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
91 extern "C" dispatch_block_t
92 _swift_dispatch_data_destructor_default(void) {
93 return DISPATCH_DATA_DESTRUCTOR_DEFAULT;
94 }
95
96 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
97 extern "C" dispatch_block_t
98 _swift_dispatch_data_destructor_free(void) {
99 return _dispatch_data_destructor_free;
100 }
101
102 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
103 extern "C" dispatch_block_t
104 _swift_dispatch_data_destructor_munmap(void) {
105 return _dispatch_data_destructor_munmap;
106 }
107
108 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
109 extern "C" dispatch_block_t
110 _swift_dispatch_block_create_with_qos_class(dispatch_block_flags_t flags, dispatch_qos_class_t qos, int relative_priority, dispatch_block_t block) {
111 return dispatch_block_create_with_qos_class(flags, qos, relative_priority, block);
112 }
113
114 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
115 extern "C" dispatch_block_t
116 _swift_dispatch_block_create_noescape(dispatch_block_flags_t flags, dispatch_block_t block) {
117 return dispatch_block_create(flags, block);
118 }
119
120 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
121 extern "C" void
122 _swift_dispatch_block_cancel(dispatch_block_t block) {
123 dispatch_block_cancel(block);
124 }
125
126 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
127 extern "C" long
128 _swift_dispatch_block_wait(dispatch_block_t block, dispatch_time_t timeout) {
129 return dispatch_block_wait(block, timeout);
130 }
131
132 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
133 extern "C" void
134 _swift_dispatch_block_notify(dispatch_block_t block, dispatch_queue_t queue, dispatch_block_t notification_block) {
135 dispatch_block_notify(block, queue, notification_block);
136 }
137
138 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
139 extern "C" long
140 _swift_dispatch_block_testcancel(dispatch_block_t block) {
141 return dispatch_block_testcancel(block);
142 }
143
144 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
145 extern "C" bool
146 _swift_dispatch_data_apply(dispatch_data_t data, bool (^applier)(dispatch_data_t, size_t, const void *, size_t)) {
147 return dispatch_data_apply(data, applier);
148 }
149
150 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
151 extern "C" void
152 _swift_dispatch_async(dispatch_queue_t queue, dispatch_block_t block) {
153 dispatch_async(queue, block);
154 }
155
156 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
157 extern "C" void
158 _swift_dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block) {
159 dispatch_group_async(group, queue, block);
160 }
161
162 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
163 extern "C" void
164 _swift_dispatch_sync(dispatch_queue_t queue, dispatch_block_t block) {
165 dispatch_sync(queue, block);
166 }
167
168 SWIFT_CC(swift) DISPATCH_RUNTIME_STDLIB_INTERFACE
169 extern "C" void
170 _swift_dispatch_release(dispatch_object_t obj) {
171 dispatch_release(obj);
172 }
173
174 // DISPATCH_RUNTIME_STDLIB_INTERFACE
175 // extern "C" dispatch_queue_t
176 // _swift_apply_current_root_queue() {
177 // return DISPATCH_APPLY_CURRENT_ROOT_QUEUE;
178 // }
179
180 #define SOURCE(t) \
181 SWIFT_CC(swift) \
182 DISPATCH_RUNTIME_STDLIB_INTERFACE extern "C" dispatch_source_type_t \
183 _swift_dispatch_source_type_##t(void) { \
184 return DISPATCH_SOURCE_TYPE_##t; \
185 }
186
187 SOURCE(DATA_ADD)
188 SOURCE(DATA_OR)
189 #if HAVE_MACH
190 SOURCE(MACH_SEND)
191 SOURCE(MACH_RECV)
192 SOURCE(MEMORYPRESSURE)
193 #endif
194 #ifndef __linux__
195 SOURCE(PROC)
196 #endif
197 SOURCE(READ)
198 SOURCE(SIGNAL)
199 SOURCE(TIMER)
200 #ifndef __linux__
201 SOURCE(VNODE)
202 #endif
203 SOURCE(WRITE)
204
205 // See comment in CFFuntime.c explaining why objc_retainAutoreleasedReturnValue is needed.
206 extern "C" void swift_release(void *);
207 extern "C" void * objc_retainAutoreleasedReturnValue(void *obj) {
208 if (obj) {
209 swift_release(obj);
210 return obj;
211 }
212 else return NULL;
213 }