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@
25 * LEGACY: This header file describles LEGACY interfaces to libdispatch from an
26 * earlier revision of the API. These interfaces WILL be removed in the future.
29 DISPATCH_PUBLIC_API DISPATCH_NONNULL1 DISPATCH_NONNULL2
31 LEGACY_dispatch_call(dispatch_queue_t
, dispatch_legacy_block_t work
, dispatch_legacy_block_t completion
)
32 __asm__("_dispatch_call2");
34 DISPATCH_PUBLIC_API DISPATCH_PURE DISPATCH_WARN_RESULT
36 LEGACY_dispatch_queue_get_current(void)
37 __asm__("_dispatch_queue_get_current");
39 /////////////////////////////////////////////////////////////////////////////
42 LEGACY_dispatch_queue_get_current(void)
44 return _dispatch_queue_get_current();
48 LEGACY_dispatch_call(dispatch_queue_t dq
,
49 dispatch_legacy_block_t dispatch_block
,
50 dispatch_legacy_block_t callback_block
)
52 dispatch_queue_t lq
= _dispatch_queue_get_current() ?: dispatch_get_main_queue();
55 di
= dispatch_block
? calloc(1, ROUND_UP_TO_CACHELINE_SIZE(sizeof(*di
))) : NULL
;
83 dispatch_event_get_signals(dispatch_event_t de
)
87 sigaddset(&ret
, (int)dispatch_event_get_signal(de
));
91 void dispatch_cancel(dispatch_source_t ds
) { dispatch_source_cancel(ds
); }
92 long dispatch_testcancel(dispatch_source_t ds
) { return dispatch_source_testcancel(ds
); }
94 void dispatch_queue_resume(dispatch_queue_t dq
) { dispatch_resume(dq
); }
95 void dispatch_queue_retain(dispatch_queue_t dq
) { dispatch_retain(dq
); }
96 void dispatch_queue_release(dispatch_queue_t dq
) { dispatch_release(dq
); }
98 void dispatch_source_suspend(dispatch_source_t ds
) { dispatch_suspend(ds
); }
99 void dispatch_source_resume(dispatch_source_t ds
) { dispatch_resume(ds
); }
100 void dispatch_source_release(dispatch_source_t ds
) { dispatch_release(ds
); }
102 void dispatch_source_attr_release(dispatch_source_attr_t attr
) { dispatch_release(attr
); }
103 void dispatch_queue_attr_release(dispatch_queue_attr_t attr
) { dispatch_release(attr
); }
105 void *dispatch_queue_get_context(dispatch_queue_t dq
) { return dispatch_get_context(dq
); }
106 void dispatch_queue_set_context(dispatch_queue_t dq
, void *context
) { dispatch_set_context(dq
, context
); }
108 void *dispatch_source_get_context(dispatch_source_t ds
) { return dispatch_get_context(ds
); }
109 void dispatch_source_set_context(dispatch_source_t ds
, void *context
) { dispatch_set_context(ds
, context
); }
111 void dispatch_source_custom_trigger(dispatch_source_t ds
) { dispatch_source_merge_data(ds
, 1); }
114 dispatch_source_trigger(dispatch_source_t ds
, unsigned long val
)
116 dispatch_source_merge_data(ds
, val
);
119 int dispatch_source_get_descriptor(dispatch_source_t ds
) { return (int)dispatch_source_get_handle(ds
); }
121 pid_t
dispatch_source_get_pid(dispatch_source_t ds
) { return (pid_t
)dispatch_source_get_handle(ds
); }
123 mach_port_t
dispatch_source_get_machport(dispatch_source_t ds
) { return (mach_port_t
)dispatch_source_get_handle(ds
); }
125 uint64_t dispatch_source_get_flags(dispatch_source_t ds
) { return dispatch_source_get_mask(ds
); }
127 dispatch_source_t
dispatch_event_get_source(dispatch_event_t event
) { return event
; }
129 long dispatch_event_get_error(dispatch_event_t event
, long* error
) { return dispatch_source_get_error(event
, error
); }
131 uint64_t dispatch_event_get_flags(dispatch_event_t event
) { return dispatch_source_get_data(event
); }
133 size_t dispatch_event_get_bytes_available(dispatch_event_t event
) { return (size_t)dispatch_source_get_data(event
); }
135 unsigned long dispatch_event_get_count(dispatch_event_t event
) { return (unsigned long)dispatch_source_get_data(event
); }
137 long dispatch_event_get_signal(dispatch_event_t event
) { return (long)dispatch_source_get_handle(event
); }
140 dispatch_source_custom_create(
141 unsigned long behavior
,
142 dispatch_source_attr_t attr
,
143 dispatch_queue_t queue
,
144 dispatch_event_handler_t handler
) {
145 return dispatch_source_data_create(behavior
, attr
, queue
, handler
);
149 dispatch_source_custom_create_f(
150 unsigned long behavior
,
151 dispatch_source_attr_t attr
,
152 dispatch_queue_t queue
,
154 dispatch_event_handler_function_t handler
) {
155 return dispatch_source_data_create_f(behavior
, attr
, queue
, h_context
, handler
);
158 #define _dispatch_source_call_block ((void *)-1)
164 dispatch_source_timer_create(uint64_t flags
,
165 uint64_t nanoseconds
,
167 dispatch_source_attr_t attr
,
169 dispatch_source_handler_t callback
)
171 return dispatch_source_timer_create_f(flags
, nanoseconds
, leeway
,
172 attr
, q
, callback
, _dispatch_source_call_block
);
177 dispatch_source_timer_create_f(uint64_t timer_flags
,
178 uint64_t nanoseconds
,
180 dispatch_source_attr_t attr
,
183 dispatch_source_handler_function_t callback
)
185 dispatch_source_t ds
;
186 dispatch_time_t start
;
188 // 6866347 - make sure nanoseconds won't overflow
189 if ((int64_t)nanoseconds
< 0) {
190 nanoseconds
= INT64_MAX
;
193 if (timer_flags
& DISPATCH_TIMER_ONESHOT
) {
194 timer_flags
|= DISPATCH_TIMER_WALL_CLOCK
;
196 if (timer_flags
== (DISPATCH_TIMER_ABSOLUTE
|DISPATCH_TIMER_WALL_CLOCK
)) {
197 static const struct timespec t0
;
198 start
= dispatch_walltime(&t0
, nanoseconds
);
199 } else if (timer_flags
& DISPATCH_TIMER_WALL_CLOCK
) {
200 start
= dispatch_walltime(DISPATCH_TIME_NOW
, nanoseconds
);
202 start
= dispatch_time(DISPATCH_TIME_NOW
, nanoseconds
);
204 if (timer_flags
& DISPATCH_TIMER_ONESHOT
) {
205 // 6866347 - make sure nanoseconds won't overflow
206 nanoseconds
= INT64_MAX
; // non-repeating (~292 years)
209 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER
, 0, (unsigned long)timer_flags
, q
);
213 ds
= _dispatch_source_create2(ds
, attr
, context
, callback
);
217 dispatch_source_set_timer(ds
, start
, nanoseconds
, leeway
);
224 dispatch_source_read_create(int descriptor
,
225 dispatch_source_attr_t attr
,
227 dispatch_source_handler_t callback
)
229 return dispatch_source_read_create_f(descriptor
,
230 attr
, q
, callback
, _dispatch_source_call_block
);
235 dispatch_source_read_create_f(int fd
,
236 dispatch_source_attr_t attr
,
239 dispatch_source_handler_function_t callback
)
241 dispatch_source_t ds
;
242 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_READ
, fd
, 0, q
);
243 return _dispatch_source_create2(ds
, attr
, context
, callback
);
248 dispatch_source_write_create(int descriptor
,
249 dispatch_source_attr_t attr
,
251 dispatch_source_handler_t callback
)
253 return dispatch_source_write_create_f(descriptor
,
254 attr
, q
, callback
, _dispatch_source_call_block
);
259 dispatch_source_write_create_f(int fd
,
260 dispatch_source_attr_t attr
,
263 dispatch_source_handler_function_t callback
)
265 dispatch_source_t ds
;
266 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_WRITE
, fd
, 0, q
);
267 return _dispatch_source_create2(ds
, attr
, context
, callback
);
272 dispatch_source_vnode_create(int descriptor
,
274 dispatch_source_attr_t attr
,
276 dispatch_source_handler_t callback
)
278 return dispatch_source_vnode_create_f(descriptor
,
279 flags
, attr
, q
, callback
, _dispatch_source_call_block
);
284 dispatch_source_vnode_create_f(int fd
,
286 dispatch_source_attr_t attr
,
289 dispatch_source_handler_function_t callback
)
291 dispatch_source_t ds
;
292 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE
, fd
, (unsigned long)event_mask
, q
);
293 return _dispatch_source_create2(ds
, attr
, context
, callback
);
298 dispatch_source_signal_create(unsigned long sig
,
299 dispatch_source_attr_t attr
,
301 dispatch_source_handler_t callback
)
303 return dispatch_source_signal_create_f(sig
,
304 attr
, q
, callback
, _dispatch_source_call_block
);
309 dispatch_source_signal_create_f(unsigned long signo
,
310 dispatch_source_attr_t attr
,
313 dispatch_source_handler_function_t callback
)
315 dispatch_source_t ds
;
316 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL
, signo
, 0, q
);
317 return _dispatch_source_create2(ds
, attr
, context
, callback
);
322 dispatch_source_proc_create(pid_t pid
,
324 dispatch_source_attr_t attr
,
326 dispatch_source_handler_t callback
)
328 return dispatch_source_proc_create_f(pid
,
329 flags
, attr
, q
, callback
, _dispatch_source_call_block
);
334 dispatch_source_proc_create_f(pid_t pid
,
336 dispatch_source_attr_t attr
,
339 dispatch_source_handler_function_t callback
)
341 dispatch_source_t ds
;
342 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC
, pid
, (unsigned long)event_mask
, q
);
343 return _dispatch_source_create2(ds
, attr
, context
, callback
);
348 dispatch_source_vfs_create(uint64_t flags
,
349 dispatch_source_attr_t attr
,
351 dispatch_source_handler_t callback
)
353 return dispatch_source_vfs_create_f(flags
,
354 attr
, q
, callback
, _dispatch_source_call_block
);
359 dispatch_source_vfs_create_f(uint64_t event_mask
,
360 dispatch_source_attr_t attr
,
363 dispatch_source_handler_function_t callback
)
365 dispatch_source_t ds
;
366 ds
= dispatch_source_create(DISPATCH_SOURCE_TYPE_VFS
, 0, (unsigned long)event_mask
, q
);
367 return _dispatch_source_create2(ds
, attr
, context
, callback
);
372 dispatch_source_data_create(unsigned long behavior
,
373 dispatch_source_attr_t attr
,
375 dispatch_source_handler_t callback
)
377 return dispatch_source_data_create_f(behavior
,
378 attr
, q
, callback
, _dispatch_source_call_block
);
384 dispatch_source_machport_create(mach_port_t mport
,
386 dispatch_source_attr_t attr
,
388 dispatch_source_handler_t callback
)
390 return dispatch_source_machport_create_f(mport
, flags
,
391 attr
, dq
, callback
, _dispatch_source_call_block
);
396 dispatch_source_data_create_f(unsigned long behavior
,
397 dispatch_source_attr_t attr
,
400 dispatch_source_handler_function_t callback
)
402 dispatch_source_t ds
;
403 dispatch_source_type_t type
;
405 case DISPATCH_SOURCE_CUSTOM_ADD
:
406 type
= DISPATCH_SOURCE_TYPE_DATA_ADD
;
408 case DISPATCH_SOURCE_CUSTOM_OR
:
409 type
= DISPATCH_SOURCE_TYPE_DATA_OR
;
414 ds
= dispatch_source_create(type
, 0, 0, q
);
415 return _dispatch_source_create2(ds
, attr
, context
, callback
);
419 dispatch_source_machport_create_f(mach_port_t mport
,
421 dispatch_source_attr_t attr
,
424 dispatch_source_handler_function_t func
)
426 dispatch_source_t ds
;
427 dispatch_source_type_t type
;
428 unsigned long newflags
= 0;
430 if (flags
& ~(DISPATCH_MACHPORT_DEAD
|DISPATCH_MACHPORT_RECV
)) {
434 if (flags
& DISPATCH_MACHPORT_DEAD
) {
435 type
= DISPATCH_SOURCE_TYPE_MACH_SEND
;
436 newflags
|= DISPATCH_MACH_SEND_DEAD
;
438 type
= DISPATCH_SOURCE_TYPE_MACH_RECV
;
441 ds
= dispatch_source_create(type
, mport
, newflags
, dq
);
442 return _dispatch_source_create2(ds
, attr
, ctxt
, func
);