2 * Copyright (c) 2008-2011 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@
21 #ifndef __DISPATCH_SOURCE__
22 #define __DISPATCH_SOURCE__
24 #ifndef __DISPATCH_INDIRECT__
25 #error "Please #include <dispatch/dispatch.h> instead of this file directly."
26 #include <dispatch/base.h> // for HeaderDoc
30 #include <mach/port.h>
31 #include <mach/message.h>
33 #include <sys/signal.h>
37 * The dispatch framework provides a suite of interfaces for monitoring low-
38 * level system objects (file descriptors, Mach ports, signals, VFS nodes, etc.)
39 * for activity and automatically submitting event handler blocks to dispatch
40 * queues when such activity occurs.
42 * This suite of interfaces is known as the Dispatch Source API.
46 * @typedef dispatch_source_t
49 * Dispatch sources are used to automatically submit event handler blocks to
50 * dispatch queues in response to external events.
52 DISPATCH_DECL(dispatch_source
);
55 * @typedef dispatch_source_type_t
58 * Constants of this type represent the class of low-level system object that
59 * is being monitored by the dispatch source. Constants of this type are
60 * passed as a parameter to dispatch_source_create() and determine how the
61 * handle argument is interpreted (i.e. as a file descriptor, mach port,
62 * signal number, process identifer, etc.), and how the mask arugment is
65 typedef const struct dispatch_source_type_s
*dispatch_source_type_t
;
68 * @const DISPATCH_SOURCE_TYPE_DATA_ADD
69 * @discussion A dispatch source that coalesces data obtained via calls to
70 * dispatch_source_merge_data(). An ADD is used to coalesce the data.
71 * The handle is unused (pass zero for now).
72 * The mask is unused (pass zero for now).
74 #define DISPATCH_SOURCE_TYPE_DATA_ADD (&_dispatch_source_type_data_add)
75 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
77 const struct dispatch_source_type_s _dispatch_source_type_data_add
;
80 * @const DISPATCH_SOURCE_TYPE_DATA_OR
81 * @discussion A dispatch source that coalesces data obtained via calls to
82 * dispatch_source_merge_data(). A logical OR is used to coalesce the data.
83 * The handle is unused (pass zero for now).
84 * The mask is used to perform a logical AND with the value passed to
85 * dispatch_source_merge_data().
87 #define DISPATCH_SOURCE_TYPE_DATA_OR (&_dispatch_source_type_data_or)
88 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
90 const struct dispatch_source_type_s _dispatch_source_type_data_or
;
93 * @const DISPATCH_SOURCE_TYPE_MACH_SEND
94 * @discussion A dispatch source that monitors a Mach port for dead name
95 * notifications (send right no longer has any corresponding receive right).
96 * The handle is a Mach port with a send or send-once right (mach_port_t).
97 * The mask is a mask of desired events from dispatch_source_mach_send_flags_t.
99 #define DISPATCH_SOURCE_TYPE_MACH_SEND (&_dispatch_source_type_mach_send)
100 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
102 const struct dispatch_source_type_s _dispatch_source_type_mach_send
;
105 * @const DISPATCH_SOURCE_TYPE_MACH_RECV
106 * @discussion A dispatch source that monitors a Mach port for pending messages.
107 * The handle is a Mach port with a receive right (mach_port_t).
108 * The mask is unused (pass zero for now).
110 #define DISPATCH_SOURCE_TYPE_MACH_RECV (&_dispatch_source_type_mach_recv)
111 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
113 const struct dispatch_source_type_s _dispatch_source_type_mach_recv
;
116 * @const DISPATCH_SOURCE_TYPE_PROC
117 * @discussion A dispatch source that monitors an external process for events
118 * defined by dispatch_source_proc_flags_t.
119 * The handle is a process identifier (pid_t).
120 * The mask is a mask of desired events from dispatch_source_proc_flags_t.
122 #define DISPATCH_SOURCE_TYPE_PROC (&_dispatch_source_type_proc)
123 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
125 const struct dispatch_source_type_s _dispatch_source_type_proc
;
128 * @const DISPATCH_SOURCE_TYPE_READ
129 * @discussion A dispatch source that monitors a file descriptor for pending
130 * bytes available to be read.
131 * The handle is a file descriptor (int).
132 * The mask is unused (pass zero for now).
134 #define DISPATCH_SOURCE_TYPE_READ (&_dispatch_source_type_read)
135 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
137 const struct dispatch_source_type_s _dispatch_source_type_read
;
140 * @const DISPATCH_SOURCE_TYPE_SIGNAL
141 * @discussion A dispatch source that monitors the current process for signals.
142 * The handle is a signal number (int).
143 * The mask is unused (pass zero for now).
145 #define DISPATCH_SOURCE_TYPE_SIGNAL (&_dispatch_source_type_signal)
146 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
148 const struct dispatch_source_type_s _dispatch_source_type_signal
;
151 * @const DISPATCH_SOURCE_TYPE_TIMER
152 * @discussion A dispatch source that submits the event handler block based
154 * The handle is unused (pass zero for now).
155 * The mask is unused (pass zero for now).
157 #define DISPATCH_SOURCE_TYPE_TIMER (&_dispatch_source_type_timer)
158 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
160 const struct dispatch_source_type_s _dispatch_source_type_timer
;
163 * @const DISPATCH_SOURCE_TYPE_VNODE
164 * @discussion A dispatch source that monitors a file descriptor for events
165 * defined by dispatch_source_vnode_flags_t.
166 * The handle is a file descriptor (int).
167 * The mask is a mask of desired events from dispatch_source_vnode_flags_t.
169 #define DISPATCH_SOURCE_TYPE_VNODE (&_dispatch_source_type_vnode)
170 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
172 const struct dispatch_source_type_s _dispatch_source_type_vnode
;
175 * @const DISPATCH_SOURCE_TYPE_WRITE
176 * @discussion A dispatch source that monitors a file descriptor for available
177 * buffer space to write bytes.
178 * The handle is a file descriptor (int).
179 * The mask is unused (pass zero for now).
181 #define DISPATCH_SOURCE_TYPE_WRITE (&_dispatch_source_type_write)
182 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
184 const struct dispatch_source_type_s _dispatch_source_type_write
;
187 * @typedef dispatch_source_mach_send_flags_t
188 * Type of dispatch_source_mach_send flags
190 * @constant DISPATCH_MACH_SEND_DEAD
191 * The receive right corresponding to the given send right was destroyed.
193 #define DISPATCH_MACH_SEND_DEAD 0x1
195 typedef unsigned long dispatch_source_mach_send_flags_t
;
198 * @typedef dispatch_source_proc_flags_t
199 * Type of dispatch_source_proc flags
201 * @constant DISPATCH_PROC_EXIT
202 * The process has exited (perhaps cleanly, perhaps not).
204 * @constant DISPATCH_PROC_FORK
205 * The process has created one or more child processes.
207 * @constant DISPATCH_PROC_EXEC
208 * The process has become another executable image via
209 * exec*() or posix_spawn*().
211 * @constant DISPATCH_PROC_SIGNAL
212 * A Unix signal was delivered to the process.
214 #define DISPATCH_PROC_EXIT 0x80000000
215 #define DISPATCH_PROC_FORK 0x40000000
216 #define DISPATCH_PROC_EXEC 0x20000000
217 #define DISPATCH_PROC_SIGNAL 0x08000000
219 typedef unsigned long dispatch_source_proc_flags_t
;
222 * @typedef dispatch_source_vnode_flags_t
223 * Type of dispatch_source_vnode flags
225 * @constant DISPATCH_VNODE_DELETE
226 * The filesystem object was deleted from the namespace.
228 * @constant DISPATCH_VNODE_WRITE
229 * The filesystem object data changed.
231 * @constant DISPATCH_VNODE_EXTEND
232 * The filesystem object changed in size.
234 * @constant DISPATCH_VNODE_ATTRIB
235 * The filesystem object metadata changed.
237 * @constant DISPATCH_VNODE_LINK
238 * The filesystem object link count changed.
240 * @constant DISPATCH_VNODE_RENAME
241 * The filesystem object was renamed in the namespace.
243 * @constant DISPATCH_VNODE_REVOKE
244 * The filesystem object was revoked.
247 #define DISPATCH_VNODE_DELETE 0x1
248 #define DISPATCH_VNODE_WRITE 0x2
249 #define DISPATCH_VNODE_EXTEND 0x4
250 #define DISPATCH_VNODE_ATTRIB 0x8
251 #define DISPATCH_VNODE_LINK 0x10
252 #define DISPATCH_VNODE_RENAME 0x20
253 #define DISPATCH_VNODE_REVOKE 0x40
255 typedef unsigned long dispatch_source_vnode_flags_t
;
260 * @function dispatch_source_create
263 * Creates a new dispatch source to monitor low-level system objects and auto-
264 * matically submit a handler block to a dispatch queue in response to events.
267 * Dispatch sources are not reentrant. Any events received while the dispatch
268 * source is suspended or while the event handler block is currently executing
269 * will be coalesced and delivered after the dispatch source is resumed or the
270 * event handler block has returned.
272 * Dispatch sources are created in a suspended state. After creating the
273 * source and setting any desired attributes (i.e. the handler, context, etc.),
274 * a call must be made to dispatch_resume() in order to begin event delivery.
277 * Declares the type of the dispatch source. Must be one of the defined
278 * dispatch_source_type_t constants.
280 * The underlying system handle to monitor. The interpretation of this argument
281 * is determined by the constant provided in the type parameter.
283 * A mask of flags specifying which events are desired. The interpretation of
284 * this argument is determined by the constant provided in the type parameter.
286 * The dispatch queue to which the event handler block will be submitted.
287 * If queue is DISPATCH_TARGET_QUEUE_DEFAULT, the source will submit the event
288 * handler block to the default priority global queue.
290 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
291 DISPATCH_EXPORT DISPATCH_MALLOC DISPATCH_WARN_RESULT DISPATCH_NOTHROW
293 dispatch_source_create(dispatch_source_type_t type
,
296 dispatch_queue_t queue
);
299 * @function dispatch_source_set_event_handler
302 * Sets the event handler block for the given dispatch source.
305 * The dispatch source to modify.
306 * The result of passing NULL in this parameter is undefined.
309 * The event handler block to submit to the source's target queue.
312 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
313 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
315 dispatch_source_set_event_handler(dispatch_source_t source
,
316 dispatch_block_t handler
);
317 #endif /* __BLOCKS__ */
320 * @function dispatch_source_set_event_handler_f
323 * Sets the event handler function for the given dispatch source.
326 * The dispatch source to modify.
327 * The result of passing NULL in this parameter is undefined.
330 * The event handler function to submit to the source's target queue.
331 * The context parameter passed to the event handler function is the current
332 * context of the dispatch source at the time the handler call is made.
333 * The result of passing NULL in this parameter is undefined.
335 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
336 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
338 dispatch_source_set_event_handler_f(dispatch_source_t source
,
339 dispatch_function_t handler
);
342 * @function dispatch_source_set_cancel_handler
345 * Sets the cancellation handler block for the given dispatch source.
348 * The cancellation handler (if specified) will be submitted to the source's
349 * target queue in response to a call to dispatch_source_cancel() once the
350 * system has released all references to the source's underlying handle and
351 * the source's event handler block has returned.
354 * A cancellation handler is required for file descriptor and mach port based
355 * sources in order to safely close the descriptor or destroy the port. Closing
356 * the descriptor or port before the cancellation handler may result in a race
357 * condition. If a new descriptor is allocated with the same value as the
358 * recently closed descriptor while the source's event handler is still running,
359 * the event handler may read/write data to the wrong descriptor.
362 * The dispatch source to modify.
363 * The result of passing NULL in this parameter is undefined.
366 * The cancellation handler block to submit to the source's target queue.
369 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
370 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
372 dispatch_source_set_cancel_handler(dispatch_source_t source
,
373 dispatch_block_t cancel_handler
);
374 #endif /* __BLOCKS__ */
377 * @function dispatch_source_set_cancel_handler_f
380 * Sets the cancellation handler function for the given dispatch source.
383 * See dispatch_source_set_cancel_handler() for more details.
386 * The dispatch source to modify.
387 * The result of passing NULL in this parameter is undefined.
390 * The cancellation handler function to submit to the source's target queue.
391 * The context parameter passed to the event handler function is the current
392 * context of the dispatch source at the time the handler call is made.
394 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
395 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
397 dispatch_source_set_cancel_handler_f(dispatch_source_t source
,
398 dispatch_function_t cancel_handler
);
401 * @function dispatch_source_cancel
404 * Asynchronously cancel the dispatch source, preventing any further invocation
405 * of its event handler block.
408 * Cancellation prevents any further invocation of the event handler block for
409 * the specified dispatch source, but does not interrupt an event handler
410 * block that is already in progress.
412 * The cancellation handler is submitted to the source's target queue once the
413 * the source's event handler has finished, indicating it is now safe to close
414 * the source's handle (i.e. file descriptor or mach port).
416 * See dispatch_source_set_cancel_handler() for more information.
419 * The dispatch source to be canceled.
420 * The result of passing NULL in this parameter is undefined.
422 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
423 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
425 dispatch_source_cancel(dispatch_source_t source
);
428 * @function dispatch_source_testcancel
431 * Tests whether the given dispatch source has been canceled.
434 * The dispatch source to be tested.
435 * The result of passing NULL in this parameter is undefined.
438 * Non-zero if canceled and zero if not canceled.
440 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
441 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
444 dispatch_source_testcancel(dispatch_source_t source
);
447 * @function dispatch_source_get_handle
450 * Returns the underlying system handle associated with this dispatch source.
453 * The result of passing NULL in this parameter is undefined.
456 * The return value should be interpreted according to the type of the dispatch
457 * source, and may be one of the following handles:
459 * DISPATCH_SOURCE_TYPE_DATA_ADD: n/a
460 * DISPATCH_SOURCE_TYPE_DATA_OR: n/a
461 * DISPATCH_SOURCE_TYPE_MACH_SEND: mach port (mach_port_t)
462 * DISPATCH_SOURCE_TYPE_MACH_RECV: mach port (mach_port_t)
463 * DISPATCH_SOURCE_TYPE_PROC: process identifier (pid_t)
464 * DISPATCH_SOURCE_TYPE_READ: file descriptor (int)
465 * DISPATCH_SOURCE_TYPE_SIGNAL: signal number (int)
466 * DISPATCH_SOURCE_TYPE_TIMER: n/a
467 * DISPATCH_SOURCE_TYPE_VNODE: file descriptor (int)
468 * DISPATCH_SOURCE_TYPE_WRITE: file descriptor (int)
470 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
471 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
474 dispatch_source_get_handle(dispatch_source_t source
);
477 * @function dispatch_source_get_mask
480 * Returns the mask of events monitored by the dispatch source.
483 * The result of passing NULL in this parameter is undefined.
486 * The return value should be interpreted according to the type of the dispatch
487 * source, and may be one of the following flag sets:
489 * DISPATCH_SOURCE_TYPE_DATA_ADD: n/a
490 * DISPATCH_SOURCE_TYPE_DATA_OR: n/a
491 * DISPATCH_SOURCE_TYPE_MACH_SEND: dispatch_source_mach_send_flags_t
492 * DISPATCH_SOURCE_TYPE_MACH_RECV: n/a
493 * DISPATCH_SOURCE_TYPE_PROC: dispatch_source_proc_flags_t
494 * DISPATCH_SOURCE_TYPE_READ: n/a
495 * DISPATCH_SOURCE_TYPE_SIGNAL: n/a
496 * DISPATCH_SOURCE_TYPE_TIMER: n/a
497 * DISPATCH_SOURCE_TYPE_VNODE: dispatch_source_vnode_flags_t
498 * DISPATCH_SOURCE_TYPE_WRITE: n/a
500 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
501 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
504 dispatch_source_get_mask(dispatch_source_t source
);
507 * @function dispatch_source_get_data
510 * Returns pending data for the dispatch source.
513 * This function is intended to be called from within the event handler block.
514 * The result of calling this function outside of the event handler callback is
518 * The result of passing NULL in this parameter is undefined.
521 * The return value should be interpreted according to the type of the dispatch
522 * source, and may be one of the following:
524 * DISPATCH_SOURCE_TYPE_DATA_ADD: application defined data
525 * DISPATCH_SOURCE_TYPE_DATA_OR: application defined data
526 * DISPATCH_SOURCE_TYPE_MACH_SEND: dispatch_source_mach_send_flags_t
527 * DISPATCH_SOURCE_TYPE_MACH_RECV: n/a
528 * DISPATCH_SOURCE_TYPE_PROC: dispatch_source_proc_flags_t
529 * DISPATCH_SOURCE_TYPE_READ: estimated bytes available to read
530 * DISPATCH_SOURCE_TYPE_SIGNAL: number of signals delivered since
531 * the last handler invocation
532 * DISPATCH_SOURCE_TYPE_TIMER: number of times the timer has fired
533 * since the last handler invocation
534 * DISPATCH_SOURCE_TYPE_VNODE: dispatch_source_vnode_flags_t
535 * DISPATCH_SOURCE_TYPE_WRITE: estimated buffer space available
537 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
538 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE
541 dispatch_source_get_data(dispatch_source_t source
);
544 * @function dispatch_source_merge_data
547 * Merges data into a dispatch source of type DISPATCH_SOURCE_TYPE_DATA_ADD or
548 * DISPATCH_SOURCE_TYPE_DATA_OR and submits its event handler block to its
552 * The result of passing NULL in this parameter is undefined.
555 * The value to coalesce with the pending data using a logical OR or an ADD
556 * as specified by the dispatch source type. A value of zero has no effect
557 * and will not result in the submission of the event handler block.
559 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
560 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
562 dispatch_source_merge_data(dispatch_source_t source
, unsigned long value
);
565 * @function dispatch_source_set_timer
568 * Sets a start time, interval, and leeway value for a timer source.
571 * Calling this function has no effect if the timer source has already been
572 * canceled. Once this function returns, any pending timer data accumulated
573 * for the previous timer values has been cleared
575 * The start time argument also determines which clock will be used for the
576 * timer. If the start time is DISPATCH_TIME_NOW or created with
577 * dispatch_time() then the timer is based on mach_absolute_time(). Otherwise,
578 * if the start time of the timer is created with dispatch_walltime() then the
579 * timer is based on gettimeofday(3).
582 * The start time of the timer. See dispatch_time() and dispatch_walltime()
583 * for more information.
586 * The nanosecond interval for the timer.
589 * A hint given to the system by the application for the amount of leeway, in
590 * nanoseconds, that the system may defer the timer in order to align with other
591 * system activity for improved system performance or power consumption. (For
592 * example, an application might perform a periodic task every 5 minutes, with
593 * a leeway of up to 30 seconds.) Note that some latency is to be expected for
594 * all timers even when a leeway value of zero is specified.
596 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_4_0
)
597 DISPATCH_EXPORT DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
599 dispatch_source_set_timer(dispatch_source_t source
,
600 dispatch_time_t start
,
605 * @function dispatch_source_set_registration_handler
608 * Sets the registration handler block for the given dispatch source.
611 * The registration handler (if specified) will be submitted to the source's
612 * target queue once the corresponding kevent() has been registered with the
613 * system, following the initial dispatch_resume() of the source.
615 * If a source is already registered when the registration handler is set, the
616 * registration handler will be invoked immediately.
619 * The dispatch source to modify.
620 * The result of passing NULL in this parameter is undefined.
623 * The registration handler block to submit to the source's target queue.
626 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_4_3
)
627 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
629 dispatch_source_set_registration_handler(dispatch_source_t source
,
630 dispatch_block_t registration_handler
);
631 #endif /* __BLOCKS__ */
634 * @function dispatch_source_set_registration_handler_f
637 * Sets the registration handler function for the given dispatch source.
640 * See dispatch_source_set_registration_handler() for more details.
643 * The dispatch source to modify.
644 * The result of passing NULL in this parameter is undefined.
647 * The registration handler function to submit to the source's target queue.
648 * The context parameter passed to the registration handler function is the
649 * current context of the dispatch source at the time the handler call is made.
651 __OSX_AVAILABLE_STARTING(__MAC_10_7
,__IPHONE_4_3
)
652 DISPATCH_EXPORT DISPATCH_NONNULL1 DISPATCH_NOTHROW
654 dispatch_source_set_registration_handler_f(dispatch_source_t source
,
655 dispatch_function_t registration_handler
);