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@
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
29 #include <mach/port.h>
30 #include <mach/message.h>
31 #include <sys/signal.h>
35 * The dispatch framework provides a suite of interfaces for monitoring low-
36 * level system objects (file descriptors, Mach ports, signals, VFS nodes, etc.)
37 * for activity and automatically submitting event handler blocks to dispatch
38 * queues when such activity occurs.
40 * This suite of interfaces is known as the Dispatch Source API.
44 * @typedef dispatch_source_t
47 * Dispatch sources are used to automatically submit event handler blocks to
48 * dispatch queues in response to external events.
50 DISPATCH_DECL(dispatch_source
);
53 * @typedef dispatch_source_type_t
56 * Constants of this type represent the class of low-level system object that
57 * is being monitored by the dispatch source. Constants of this type are
58 * passed as a parameter to dispatch_source_create() and determine how the
59 * handle argument is interpreted (i.e. as a file descriptor, mach port,
60 * signal number, process identifer, etc.), and how the mask arugment is
63 typedef const struct dispatch_source_type_s
*dispatch_source_type_t
;
66 * @const DISPATCH_SOURCE_TYPE_DATA_ADD
67 * @discussion A dispatch source that coalesces data obtained via calls to
68 * dispatch_source_merge_data(). An ADD is used to coalesce the data.
69 * The handle is unused (pass zero for now).
70 * The mask is unused (pass zero for now).
72 #define DISPATCH_SOURCE_TYPE_DATA_ADD (&_dispatch_source_type_data_add)
73 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
74 extern const struct dispatch_source_type_s _dispatch_source_type_data_add
;
77 * @const DISPATCH_SOURCE_TYPE_DATA_OR
78 * @discussion A dispatch source that coalesces data obtained via calls to
79 * dispatch_source_merge_data(). A logical OR is used to coalesce the data.
80 * The handle is unused (pass zero for now).
81 * The mask is used to perform a logical AND with the value passed to
82 * dispatch_source_merge_data().
84 #define DISPATCH_SOURCE_TYPE_DATA_OR (&_dispatch_source_type_data_or)
85 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
86 extern const struct dispatch_source_type_s _dispatch_source_type_data_or
;
89 * @const DISPATCH_SOURCE_TYPE_MACH_SEND
90 * @discussion A dispatch source that monitors a Mach port for dead name
91 * notifications (send right no longer has any corresponding receive right).
92 * The handle is a Mach port with a send or send-once right (mach_port_t).
93 * The mask is a mask of desired events from dispatch_source_mach_send_flags_t.
95 #define DISPATCH_SOURCE_TYPE_MACH_SEND (&_dispatch_source_type_mach_send)
96 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
97 extern const struct dispatch_source_type_s _dispatch_source_type_mach_send
;
100 * @const DISPATCH_SOURCE_TYPE_MACH_RECV
101 * @discussion A dispatch source that monitors a Mach port for pending messages.
102 * The handle is a Mach port with a receive right (mach_port_t).
103 * The mask is unused (pass zero for now).
105 #define DISPATCH_SOURCE_TYPE_MACH_RECV (&_dispatch_source_type_mach_recv)
106 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
107 extern const struct dispatch_source_type_s _dispatch_source_type_mach_recv
;
110 * @const DISPATCH_SOURCE_TYPE_PROC
111 * @discussion A dispatch source that monitors an external process for events
112 * defined by dispatch_source_proc_flags_t.
113 * The handle is a process identifier (pid_t).
114 * The mask is a mask of desired events from dispatch_source_proc_flags_t.
116 #define DISPATCH_SOURCE_TYPE_PROC (&_dispatch_source_type_proc)
117 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
118 extern const struct dispatch_source_type_s _dispatch_source_type_proc
;
121 * @const DISPATCH_SOURCE_TYPE_READ
122 * @discussion A dispatch source that monitors a file descriptor for pending
123 * bytes available to be read.
124 * The handle is a file descriptor (int).
125 * The mask is unused (pass zero for now).
127 #define DISPATCH_SOURCE_TYPE_READ (&_dispatch_source_type_read)
128 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
129 extern const struct dispatch_source_type_s _dispatch_source_type_read
;
132 * @const DISPATCH_SOURCE_TYPE_SIGNAL
133 * @discussion A dispatch source that monitors the current process for signals.
134 * The handle is a signal number (int).
135 * The mask is unused (pass zero for now).
137 #define DISPATCH_SOURCE_TYPE_SIGNAL (&_dispatch_source_type_signal)
138 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
139 extern const struct dispatch_source_type_s _dispatch_source_type_signal
;
142 * @const DISPATCH_SOURCE_TYPE_TIMER
143 * @discussion A dispatch source that submits the event handler block based
145 * The handle is unused (pass zero for now).
146 * The mask is unused (pass zero for now).
148 #define DISPATCH_SOURCE_TYPE_TIMER (&_dispatch_source_type_timer)
149 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
150 extern const struct dispatch_source_type_s _dispatch_source_type_timer
;
153 * @const DISPATCH_SOURCE_TYPE_VNODE
154 * @discussion A dispatch source that monitors a file descriptor for events
155 * defined by dispatch_source_vnode_flags_t.
156 * The handle is a file descriptor (int).
157 * The mask is a mask of desired events from dispatch_source_vnode_flags_t.
159 #define DISPATCH_SOURCE_TYPE_VNODE (&_dispatch_source_type_vnode)
160 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
161 extern const struct dispatch_source_type_s _dispatch_source_type_vnode
;
164 * @const DISPATCH_SOURCE_TYPE_WRITE
165 * @discussion A dispatch source that monitors a file descriptor for available
166 * buffer space to write bytes.
167 * The handle is a file descriptor (int).
168 * The mask is unused (pass zero for now).
170 #define DISPATCH_SOURCE_TYPE_WRITE (&_dispatch_source_type_write)
171 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
172 extern const struct dispatch_source_type_s _dispatch_source_type_write
;
175 * @enum dispatch_source_mach_send_flags_t
177 * @constant DISPATCH_MACH_SEND_DEAD
178 * The receive right corresponding to the given send right was destroyed.
181 DISPATCH_MACH_SEND_DEAD
= 0x1,
185 * @enum dispatch_source_proc_flags_t
187 * @constant DISPATCH_PROC_EXIT
188 * The process has exited (perhaps cleanly, perhaps not).
190 * @constant DISPATCH_PROC_FORK
191 * The process has created one or more child processes.
193 * @constant DISPATCH_PROC_EXEC
194 * The process has become another executable image via
195 * exec*() or posix_spawn*().
197 * @constant DISPATCH_PROC_SIGNAL
198 * A Unix signal was delivered to the process.
201 DISPATCH_PROC_EXIT
= 0x80000000,
202 DISPATCH_PROC_FORK
= 0x40000000,
203 DISPATCH_PROC_EXEC
= 0x20000000,
204 DISPATCH_PROC_SIGNAL
= 0x08000000,
208 * @enum dispatch_source_vnode_flags_t
210 * @constant DISPATCH_VNODE_DELETE
211 * The filesystem object was deleted from the namespace.
213 * @constant DISPATCH_VNODE_WRITE
214 * The filesystem object data changed.
216 * @constant DISPATCH_VNODE_EXTEND
217 * The filesystem object changed in size.
219 * @constant DISPATCH_VNODE_ATTRIB
220 * The filesystem object metadata changed.
222 * @constant DISPATCH_VNODE_LINK
223 * The filesystem object link count changed.
225 * @constant DISPATCH_VNODE_RENAME
226 * The filesystem object was renamed in the namespace.
228 * @constant DISPATCH_VNODE_REVOKE
229 * The filesystem object was revoked.
232 DISPATCH_VNODE_DELETE
= 0x1,
233 DISPATCH_VNODE_WRITE
= 0x2,
234 DISPATCH_VNODE_EXTEND
= 0x4,
235 DISPATCH_VNODE_ATTRIB
= 0x8,
236 DISPATCH_VNODE_LINK
= 0x10,
237 DISPATCH_VNODE_RENAME
= 0x20,
238 DISPATCH_VNODE_REVOKE
= 0x40,
244 * @function dispatch_source_create
247 * Creates a new dispatch source to monitor low-level system objects and auto-
248 * matically submit a handler block to a dispatch queue in response to events.
251 * Dispatch sources are not reentrant. Any events received while the dispatch
252 * source is suspended or while the event handler block is currently executing
253 * will be coalesced and delivered after the dispatch source is resumed or the
254 * event handler block has returned.
256 * Dispatch sources are created in a suspended state. After creating the
257 * source and setting any desired attributes (i.e. the handler, context, etc.),
258 * a call must be made to dispatch_resume() in order to begin event delivery.
261 * Declares the type of the dispatch source. Must be one of the defined
262 * dispatch_source_type_t constants.
264 * The underlying system handle to monitor. The interpretation of this argument
265 * is determined by the constant provided in the type parameter.
267 * A mask of flags specifying which events are desired. The interpretation of
268 * this argument is determined by the constant provided in the type parameter.
270 * The dispatch queue to which the event handler block will be submited.
272 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
273 DISPATCH_MALLOC DISPATCH_NOTHROW
275 dispatch_source_create(dispatch_source_type_t type
,
278 dispatch_queue_t queue
);
281 * @function dispatch_source_set_event_handler
284 * Sets the event handler block for the given dispatch source.
287 * The dispatch source to modify.
288 * The result of passing NULL in this parameter is undefined.
291 * The event handler block to submit to the source's target queue.
294 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
295 DISPATCH_NONNULL1 DISPATCH_NOTHROW
297 dispatch_source_set_event_handler(dispatch_source_t source
,
298 dispatch_block_t handler
);
299 #endif /* __BLOCKS__ */
302 * @function dispatch_source_set_event_handler_f
305 * Sets the event handler function for the given dispatch source.
308 * The dispatch source to modify.
309 * The result of passing NULL in this parameter is undefined.
312 * The event handler function to submit to the source's target queue.
313 * The context parameter passed to the event handler function is the current
314 * context of the dispatch source at the time the handler call is made.
315 * The result of passing NULL in this parameter is undefined.
317 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
318 DISPATCH_NONNULL1 DISPATCH_NOTHROW
320 dispatch_source_set_event_handler_f(dispatch_source_t source
,
321 dispatch_function_t handler
);
324 * @function dispatch_source_set_cancel_handler
327 * Sets the cancellation handler block for the given dispatch source.
330 * The cancellation handler (if specified) will be submitted to the source's
331 * target queue in response to a call to dispatch_source_cancel() once the
332 * system has released all references to the source's underlying handle and
333 * the source's event handler block has returned.
336 * A cancellation handler is required for file descriptor and mach port based
337 * sources in order to safely close the descriptor or destroy the port. Closing
338 * the descriptor or port before the cancellation handler may result in a race
339 * condition. If a new descriptor is allocated with the same value as the
340 * recently closed descriptor while the source's event handler is still running,
341 * the event handler may read/write data to the wrong descriptor.
344 * The dispatch source to modify.
345 * The result of passing NULL in this parameter is undefined.
348 * The cancellation handler block to submit to the source's target queue.
351 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
352 DISPATCH_NONNULL1 DISPATCH_NOTHROW
354 dispatch_source_set_cancel_handler(dispatch_source_t source
,
355 dispatch_block_t cancel_handler
);
356 #endif /* __BLOCKS__ */
359 * @function dispatch_source_set_cancel_handler_f
362 * Sets the cancellation handler function for the given dispatch source.
365 * See dispatch_source_set_cancel_handler() for more details.
368 * The dispatch source to modify.
369 * The result of passing NULL in this parameter is undefined.
372 * The cancellation handler function to submit to the source's target queue.
373 * The context parameter passed to the event handler function is the current
374 * context of the dispatch source at the time the handler call is made.
376 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
377 DISPATCH_NONNULL1 DISPATCH_NOTHROW
379 dispatch_source_set_cancel_handler_f(dispatch_source_t source
,
380 dispatch_function_t cancel_handler
);
383 * @function dispatch_source_cancel
386 * Asynchronously cancel the dispatch source, preventing any further invocation
387 * of its event handler block.
390 * Cancellation prevents any further invocation of the event handler block for
391 * the specified dispatch source, but does not interrupt an event handler
392 * block that is already in progress.
394 * The cancellation handler is submitted to the source's target queue once the
395 * the source's event handler has finished, indicating it is now safe to close
396 * the source's handle (i.e. file descriptor or mach port).
398 * See dispatch_source_set_cancel_handler() for more information.
401 * The dispatch source to be canceled.
402 * The result of passing NULL in this parameter is undefined.
404 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
405 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
407 dispatch_source_cancel(dispatch_source_t source
);
410 * @function dispatch_source_testcancel
413 * Tests whether the given dispatch source has been canceled.
416 * The dispatch source to be tested.
417 * The result of passing NULL in this parameter is undefined.
420 * Non-zero if canceled and zero if not canceled.
422 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
423 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
425 dispatch_source_testcancel(dispatch_source_t source
);
428 * @function dispatch_source_get_handle
431 * Returns the underlying system handle associated with this dispatch source.
434 * The result of passing NULL in this parameter is undefined.
437 * The return value should be interpreted according to the type of the dispatch
438 * source, and may be one of the following handles:
440 * DISPATCH_SOURCE_TYPE_DATA_ADD: n/a
441 * DISPATCH_SOURCE_TYPE_DATA_OR: n/a
442 * DISPATCH_SOURCE_TYPE_MACH_SEND: mach port (mach_port_t)
443 * DISPATCH_SOURCE_TYPE_MACH_RECV: mach port (mach_port_t)
444 * DISPATCH_SOURCE_TYPE_PROC: process identifier (pid_t)
445 * DISPATCH_SOURCE_TYPE_READ: file descriptor (int)
446 * DISPATCH_SOURCE_TYPE_SIGNAL: signal number (int)
447 * DISPATCH_SOURCE_TYPE_TIMER: n/a
448 * DISPATCH_SOURCE_TYPE_VNODE: file descriptor (int)
449 * DISPATCH_SOURCE_TYPE_WRITE: file descriptor (int)
451 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
452 DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
454 dispatch_source_get_handle(dispatch_source_t source
);
457 * @function dispatch_source_get_mask
460 * Returns the mask of events monitored by the dispatch source.
463 * The result of passing NULL in this parameter is undefined.
466 * The return value should be interpreted according to the type of the dispatch
467 * source, and may be one of the following flag sets:
469 * DISPATCH_SOURCE_TYPE_DATA_ADD: n/a
470 * DISPATCH_SOURCE_TYPE_DATA_OR: n/a
471 * DISPATCH_SOURCE_TYPE_MACH_SEND: dispatch_source_mach_send_flags_t
472 * DISPATCH_SOURCE_TYPE_MACH_RECV: n/a
473 * DISPATCH_SOURCE_TYPE_PROC: dispatch_source_proc_flags_t
474 * DISPATCH_SOURCE_TYPE_READ: n/a
475 * DISPATCH_SOURCE_TYPE_SIGNAL: n/a
476 * DISPATCH_SOURCE_TYPE_TIMER: n/a
477 * DISPATCH_SOURCE_TYPE_VNODE: dispatch_source_vnode_flags_t
478 * DISPATCH_SOURCE_TYPE_WRITE: n/a
480 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
481 DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
483 dispatch_source_get_mask(dispatch_source_t source
);
486 * @function dispatch_source_get_data
489 * Returns pending data for the dispatch source.
492 * This function is intended to be called from within the event handler block.
493 * The result of calling this function outside of the event handler callback is
497 * The result of passing NULL in this parameter is undefined.
500 * The return value should be interpreted according to the type of the dispatch
501 * source, and may be one of the following:
503 * DISPATCH_SOURCE_TYPE_DATA_ADD: application defined data
504 * DISPATCH_SOURCE_TYPE_DATA_OR: application defined data
505 * DISPATCH_SOURCE_TYPE_MACH_SEND: dispatch_source_mach_send_flags_t
506 * DISPATCH_SOURCE_TYPE_MACH_RECV: n/a
507 * DISPATCH_SOURCE_TYPE_PROC: dispatch_source_proc_flags_t
508 * DISPATCH_SOURCE_TYPE_READ: estimated bytes available to read
509 * DISPATCH_SOURCE_TYPE_SIGNAL: number of signals delivered since
510 * the last handler invocation
511 * DISPATCH_SOURCE_TYPE_TIMER: number of times the timer has fired
512 * since the last handler invocation
513 * DISPATCH_SOURCE_TYPE_VNODE: dispatch_source_vnode_flags_t
514 * DISPATCH_SOURCE_TYPE_WRITE: estimated buffer space available
516 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
517 DISPATCH_NONNULL_ALL DISPATCH_WARN_RESULT DISPATCH_PURE DISPATCH_NOTHROW
519 dispatch_source_get_data(dispatch_source_t source
);
522 * @function dispatch_source_merge_data
525 * Merges data into a dispatch source of type DISPATCH_SOURCE_TYPE_DATA_ADD or
526 * DISPATCH_SOURCE_TYPE_DATA_OR and submits its event handler block to its
530 * The result of passing NULL in this parameter is undefined.
533 * The value to coalesce with the pending data using a logical OR or an ADD
534 * as specified by the dispatch source type. A value of zero has no effect
535 * and will not result in the submission of the event handler block.
537 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
538 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
540 dispatch_source_merge_data(dispatch_source_t source
, unsigned long value
);
543 * @function dispatch_source_set_timer
546 * Sets a start time, interval, and leeway value for a timer source.
549 * Calling this function has no effect if the timer source has already been
552 * The start time argument also determines which clock will be used for the
553 * timer. If the start time is DISPATCH_TIME_NOW or created with
554 * dispatch_time() then the timer is based on mach_absolute_time(). Otherwise,
555 * if the start time of the timer is created with dispatch_walltime() then the
556 * timer is based on gettimeofday(3).
559 * The start time of the timer. See dispatch_time() and dispatch_walltime()
560 * for more information.
563 * The nanosecond interval for the timer.
566 * A hint given to the system by the application for the amount of leeway, in
567 * nanoseconds, that the system may defer the timer in order to align with other
568 * system activity for improved system performance or power consumption. (For
569 * example, an application might perform a periodic task every 5 minutes, with
570 * a leeway of up to 30 seconds.) Note that some latency is to be expected for
571 * all timers even when a leeway value of zero is specified.
573 __OSX_AVAILABLE_STARTING(__MAC_10_6
,__IPHONE_NA
)
574 DISPATCH_NONNULL_ALL DISPATCH_NOTHROW
576 dispatch_source_set_timer(dispatch_source_t source
,
577 dispatch_time_t start
,