2 * Copyright (c) 2008-2009 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
23 #ifndef DISPATCH_NO_LEGACY
27 * LEGACY: This header file describles LEGACY interfaces to libdispatch from an
28 * earlier revision of the API. These interfaces WILL be removed in the future.
32 DISPATCH_PUBLIC_API DISPATCH_NONNULL1 DISPATCH_NONNULL2
34 LEGACY_dispatch_call(dispatch_queue_t, dispatch_legacy_block_t work, dispatch_legacy_block_t completion)
35 __asm__("_dispatch_call2");
38 DISPATCH_PUBLIC_API DISPATCH_PURE DISPATCH_WARN_RESULT
40 LEGACY_dispatch_queue_get_current(void)
41 __asm__("_dispatch_queue_get_current");
43 /////////////////////////////////////////////////////////////////////////////
46 LEGACY_dispatch_queue_get_current(void)
48 return _dispatch_queue_get_current();
53 LEGACY_dispatch_call(dispatch_queue_t dq,
54 dispatch_legacy_block_t dispatch_block,
55 dispatch_legacy_block_t callback_block)
57 dispatch_queue_t lq = _dispatch_queue_get_current() ?: dispatch_get_main_queue();
60 di = dispatch_block ? calloc(1, ROUND_UP_TO_CACHELINE_SIZE(sizeof(*di))) : NULL;
86 #endif /* __BLOCKS__ */
89 dispatch_event_get_signals(dispatch_event_t de)
93 sigaddset(&ret, (int)dispatch_event_get_signal(de));
97 void dispatch_cancel(dispatch_source_t ds) { dispatch_source_cancel(ds); }
98 long dispatch_testcancel(dispatch_source_t ds) { return dispatch_source_testcancel(ds); }
100 void dispatch_queue_resume(dispatch_queue_t dq) { dispatch_resume(dq); }
101 void dispatch_queue_retain(dispatch_queue_t dq) { dispatch_retain(dq); }
102 void dispatch_queue_release(dispatch_queue_t dq) { dispatch_release(dq); }
104 void dispatch_source_suspend(dispatch_source_t ds) { dispatch_suspend(ds); }
105 void dispatch_source_resume(dispatch_source_t ds) { dispatch_resume(ds); }
106 void dispatch_source_release(dispatch_source_t ds) { dispatch_release(ds); }
108 void dispatch_source_attr_release(dispatch_source_attr_t attr) { dispatch_release(attr); }
109 void dispatch_queue_attr_release(dispatch_queue_attr_t attr) { dispatch_release(attr); }
111 void *dispatch_queue_get_context(dispatch_queue_t dq) { return dispatch_get_context(dq); }
112 void dispatch_queue_set_context(dispatch_queue_t dq, void *context) { dispatch_set_context(dq, context); }
114 void *dispatch_source_get_context(dispatch_source_t ds) { return dispatch_get_context(ds); }
115 void dispatch_source_set_context(dispatch_source_t ds, void *context) { dispatch_set_context(ds, context); }
117 void dispatch_source_custom_trigger(dispatch_source_t ds) { dispatch_source_merge_data(ds, 1); }
120 dispatch_source_trigger(dispatch_source_t ds, unsigned long val)
122 dispatch_source_merge_data(ds, val);
125 int dispatch_source_get_descriptor(dispatch_source_t ds) { return (int)dispatch_source_get_handle(ds); }
127 pid_t dispatch_source_get_pid(dispatch_source_t ds) { return (pid_t)dispatch_source_get_handle(ds); }
129 mach_port_t dispatch_source_get_machport(dispatch_source_t ds) { return (mach_port_t)dispatch_source_get_handle(ds); }
131 uint64_t dispatch_source_get_flags(dispatch_source_t ds) { return dispatch_source_get_mask(ds); }
133 dispatch_source_t dispatch_event_get_source(dispatch_event_t event) { return event; }
135 long dispatch_event_get_error(dispatch_event_t event, long* error) { return dispatch_source_get_error(event, error); }
137 uint64_t dispatch_event_get_flags(dispatch_event_t event) { return dispatch_source_get_data(event); }
139 size_t dispatch_event_get_bytes_available(dispatch_event_t event) { return (size_t)dispatch_source_get_data(event); }
141 unsigned long dispatch_event_get_count(dispatch_event_t event) { return (unsigned long)dispatch_source_get_data(event); }
143 long dispatch_event_get_signal(dispatch_event_t event) { return (long)dispatch_source_get_handle(event); }
146 dispatch_source_custom_create(
147 unsigned long behavior,
148 dispatch_source_attr_t attr,
149 dispatch_queue_t queue,
150 dispatch_event_handler_t handler) {
151 return dispatch_source_data_create(behavior, attr, queue, handler);
155 dispatch_source_custom_create_f(
156 unsigned long behavior,
157 dispatch_source_attr_t attr,
158 dispatch_queue_t queue,
160 dispatch_event_handler_function_t handler) {
161 return dispatch_source_data_create_f(behavior, attr, queue, h_context, handler);
164 #define _dispatch_source_call_block ((void *)-1)
170 dispatch_source_timer_create(uint64_t flags,
171 uint64_t nanoseconds,
173 dispatch_source_attr_t attr,
175 dispatch_source_handler_t callback)
177 return dispatch_source_timer_create_f(flags, nanoseconds, leeway,
178 attr, q, callback, _dispatch_source_call_block);
183 dispatch_source_timer_create_f(uint64_t timer_flags,
184 uint64_t nanoseconds,
186 dispatch_source_attr_t attr,
189 dispatch_source_handler_function_t callback)
191 dispatch_source_t ds;
192 dispatch_time_t start;
194 // 6866347 - make sure nanoseconds won't overflow
195 if ((int64_t)nanoseconds < 0) {
196 nanoseconds = INT64_MAX;
199 if (timer_flags & DISPATCH_TIMER_ONESHOT) {
200 timer_flags |= DISPATCH_TIMER_WALL_CLOCK;
202 if (timer_flags == (DISPATCH_TIMER_ABSOLUTE|DISPATCH_TIMER_WALL_CLOCK)) {
203 static const struct timespec t0;
204 start = dispatch_walltime(&t0, nanoseconds);
205 } else if (timer_flags & DISPATCH_TIMER_WALL_CLOCK) {
206 start = dispatch_walltime(DISPATCH_TIME_NOW, nanoseconds);
208 start = dispatch_time(DISPATCH_TIME_NOW, nanoseconds);
210 if (timer_flags & DISPATCH_TIMER_ONESHOT) {
211 // 6866347 - make sure nanoseconds won't overflow
212 nanoseconds = INT64_MAX; // non-repeating (~292 years)
215 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, (unsigned long)timer_flags, q);
219 ds = _dispatch_source_create2(ds, attr, context, callback);
223 dispatch_source_set_timer(ds, start, nanoseconds, leeway);
230 dispatch_source_read_create(int descriptor,
231 dispatch_source_attr_t attr,
233 dispatch_source_handler_t callback)
235 return dispatch_source_read_create_f(descriptor,
236 attr, q, callback, _dispatch_source_call_block);
241 dispatch_source_read_create_f(int fd,
242 dispatch_source_attr_t attr,
245 dispatch_source_handler_function_t callback)
247 dispatch_source_t ds;
248 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fd, 0, q);
249 return _dispatch_source_create2(ds, attr, context, callback);
254 dispatch_source_write_create(int descriptor,
255 dispatch_source_attr_t attr,
257 dispatch_source_handler_t callback)
259 return dispatch_source_write_create_f(descriptor,
260 attr, q, callback, _dispatch_source_call_block);
265 dispatch_source_write_create_f(int fd,
266 dispatch_source_attr_t attr,
269 dispatch_source_handler_function_t callback)
271 dispatch_source_t ds;
272 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE, fd, 0, q);
273 return _dispatch_source_create2(ds, attr, context, callback);
278 dispatch_source_vnode_create(int descriptor,
280 dispatch_source_attr_t attr,
282 dispatch_source_handler_t callback)
284 return dispatch_source_vnode_create_f(descriptor,
285 flags, attr, q, callback, _dispatch_source_call_block);
290 dispatch_source_vnode_create_f(int fd,
292 dispatch_source_attr_t attr,
295 dispatch_source_handler_function_t callback)
297 dispatch_source_t ds;
298 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fd, (unsigned long)event_mask, q);
299 return _dispatch_source_create2(ds, attr, context, callback);
304 dispatch_source_signal_create(unsigned long sig,
305 dispatch_source_attr_t attr,
307 dispatch_source_handler_t callback)
309 return dispatch_source_signal_create_f(sig,
310 attr, q, callback, _dispatch_source_call_block);
315 dispatch_source_signal_create_f(unsigned long signo,
316 dispatch_source_attr_t attr,
319 dispatch_source_handler_function_t callback)
321 dispatch_source_t ds;
322 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, signo, 0, q);
323 return _dispatch_source_create2(ds, attr, context, callback);
328 dispatch_source_proc_create(pid_t pid,
330 dispatch_source_attr_t attr,
332 dispatch_source_handler_t callback)
334 return dispatch_source_proc_create_f(pid,
335 flags, attr, q, callback, _dispatch_source_call_block);
340 dispatch_source_proc_create_f(pid_t pid,
342 dispatch_source_attr_t attr,
345 dispatch_source_handler_function_t callback)
347 dispatch_source_t ds;
348 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid, (unsigned long)event_mask, q);
349 return _dispatch_source_create2(ds, attr, context, callback);
354 dispatch_source_vfs_create(uint64_t flags,
355 dispatch_source_attr_t attr,
357 dispatch_source_handler_t callback)
359 return dispatch_source_vfs_create_f(flags,
360 attr, q, callback, _dispatch_source_call_block);
365 dispatch_source_vfs_create_f(uint64_t event_mask,
366 dispatch_source_attr_t attr,
369 dispatch_source_handler_function_t callback)
371 dispatch_source_t ds;
372 ds = dispatch_source_create(DISPATCH_SOURCE_TYPE_VFS, 0, (unsigned long)event_mask, q);
373 return _dispatch_source_create2(ds, attr, context, callback);
378 dispatch_source_data_create(unsigned long behavior,
379 dispatch_source_attr_t attr,
381 dispatch_source_handler_t callback)
383 return dispatch_source_data_create_f(behavior,
384 attr, q, callback, _dispatch_source_call_block);
390 dispatch_source_machport_create(mach_port_t mport,
392 dispatch_source_attr_t attr,
394 dispatch_source_handler_t callback)
396 return dispatch_source_machport_create_f(mport, flags,
397 attr, dq, callback, _dispatch_source_call_block);
402 dispatch_source_data_create_f(unsigned long behavior,
403 dispatch_source_attr_t attr,
406 dispatch_source_handler_function_t callback)
408 dispatch_source_t ds;
409 dispatch_source_type_t type;
411 case DISPATCH_SOURCE_CUSTOM_ADD:
412 type = DISPATCH_SOURCE_TYPE_DATA_ADD;
414 case DISPATCH_SOURCE_CUSTOM_OR:
415 type = DISPATCH_SOURCE_TYPE_DATA_OR;
420 ds = dispatch_source_create(type, 0, 0, q);
421 return _dispatch_source_create2(ds, attr, context, callback);
425 dispatch_source_machport_create_f(mach_port_t mport,
427 dispatch_source_attr_t attr,
430 dispatch_source_handler_function_t func)
432 dispatch_source_t ds;
433 dispatch_source_type_t type;
434 unsigned long newflags = 0;
436 if (flags & ~(DISPATCH_MACHPORT_DEAD|DISPATCH_MACHPORT_RECV)) {
440 if (flags & DISPATCH_MACHPORT_DEAD) {
441 type = DISPATCH_SOURCE_TYPE_MACH_SEND;
442 newflags |= DISPATCH_MACH_SEND_DEAD;
444 type = DISPATCH_SOURCE_TYPE_MACH_RECV;
447 ds = dispatch_source_create(type, mport, newflags, dq);
448 return _dispatch_source_create2(ds, attr, ctxt, func);
451 #endif /* DISPATCH_NO_LEGACY */