1 .\" Copyright (c) 2008-2013 Apple Inc. All rights reserved.
3 .Dt dispatch_source_create 3
6 .Nm dispatch_source_create
7 .Nd dispatch event sources
9 .Fd #include <dispatch/dispatch.h>
11 .Fo dispatch_source_create
12 .Fa "dispatch_source_type_t type"
13 .Fa "uintptr_t handle"
14 .Fa "unsigned long mask"
15 .Fa "dispatch_queue_t queue"
18 .Fo dispatch_source_set_event_handler
19 .Fa "dispatch_source_t source"
20 .Fa "void (^block)(void)"
23 .Fo dispatch_source_set_event_handler_f
24 .Fa "dispatch_source_t source"
25 .Fa "void (*function)(void *)"
28 .Fo dispatch_source_set_registration_handler
29 .Fa "dispatch_source_t source"
30 .Fa "void (^block)(void)"
33 .Fo dispatch_source_set_registration_handler_f
34 .Fa "dispatch_source_t source"
35 .Fa "void (*function)(void *)"
38 .Fo dispatch_source_set_cancel_handler
39 .Fa "dispatch_source_t source"
40 .Fa "void (^block)(void)"
43 .Fo dispatch_source_set_cancel_handler_f
44 .Fa "dispatch_source_t source"
45 .Fa "void (*function)(void *)"
48 .Fo dispatch_source_cancel
49 .Fa "dispatch_source_t source"
52 .Fo dispatch_source_testcancel
53 .Fa "dispatch_source_t source"
56 .Fo dispatch_source_get_handle
57 .Fa "dispatch_source_t source"
60 .Fo dispatch_source_get_mask
61 .Fa "dispatch_source_t source"
64 .Fo dispatch_source_get_data
65 .Fa "dispatch_source_t source"
68 .Fo dispatch_source_merge_data
69 .Fa "dispatch_source_t source"
70 .Fa "unsigned long data"
73 .Fo dispatch_source_set_timer
74 .Fa "dispatch_source_t source"
75 .Fa "dispatch_time_t start"
76 .Fa "uint64_t interval"
80 Dispatch event sources may be used to monitor a variety of system objects and
81 events including file descriptors, mach ports, processes, virtual filesystem
82 nodes, signal delivery and timers.
84 When a state change occurs, the dispatch source will submit its event handler
85 block to its target queue.
88 .Fn dispatch_source_create
89 function creates a new dispatch source object that may be retained and released
96 parameter specifies the target queue of the new source object, it will
97 be retained by the source object. Pass the
98 .Dv DISPATCH_TARGET_QUEUE_DEFAULT
99 constant to use the default target queue (the default priority global
102 Newly created sources are created in a suspended state. After the source has
103 been configured by setting an event handler, cancellation handler, registration
105 etc., the source must be activated by a call to
107 before any events will be delivered.
109 Dispatch sources may be one of the following types:
110 .Bl -bullet -compact -offset indent
112 DISPATCH_SOURCE_TYPE_DATA_ADD
114 DISPATCH_SOURCE_TYPE_DATA_OR
116 DISPATCH_SOURCE_TYPE_MACH_SEND
118 DISPATCH_SOURCE_TYPE_MACH_RECV
120 DISPATCH_SOURCE_TYPE_MEMORYPRESSURE
122 DISPATCH_SOURCE_TYPE_PROC
124 DISPATCH_SOURCE_TYPE_READ
126 DISPATCH_SOURCE_TYPE_SIGNAL
128 DISPATCH_SOURCE_TYPE_TIMER
130 DISPATCH_SOURCE_TYPE_VNODE
132 DISPATCH_SOURCE_TYPE_WRITE
140 .Fn dispatch_source_create
141 and the return values of the
142 .Fn dispatch_source_get_handle ,
143 .Fn dispatch_source_get_mask ,
145 .Fn dispatch_source_get_data
146 functions should be interpreted according to the type of the dispatch source.
149 .Fn dispatch_source_get_handle
151 returns the underlying handle to the dispatch source (i.e. file descriptor,
152 mach port, process identifer, etc.). The result of this function may be cast
153 directly to the underlying type.
156 .Fn dispatch_source_get_mask
158 returns the set of flags that were specified at source creation time via the
163 .Fn dispatch_source_get_data
164 function returns the currently pending data for the dispatch source.
165 This function should only be called from within the source's event handler.
166 The result of calling this function from any other context is undefined.
169 .Fn dispatch_source_merge_data
170 function is intended for use with the
171 .Vt DISPATCH_SOURCE_TYPE_DATA_ADD
173 .Vt DISPATCH_SOURCE_TYPE_DATA_OR
174 source types. The result of using this function with any other source type is
175 undefined. Calling this function will atomically add or bitwise OR the data
176 into the source's data, and trigger the delivery of the source's event handler.
178 .Sh SOURCE EVENT HANDLERS
179 In order to receive events from the dispatch source, an event handler should be
181 .Fn dispatch_source_set_event_handler .
182 The event handler block is submitted to the source's target queue when the state
183 of the underlying system handle changes, or when an event occurs. If a source
184 is resumed with no event handler block set, events will be quietly ignored.
185 If the event handler block is changed while the source is suspended, or from a
186 block running on a serial queue that is the source's target queue, then the next
187 event handler invocation will use the new block.
189 Dispatch sources may be suspended or resumed independently of their target
194 on the dispatch source directly. The data describing events which occur while a
195 source is suspended are coalesced and delivered once the source is resumed.
200 need not be reentrant safe, as it is not resubmitted to the target
202 until any prior invocation for that dispatch source has completed.
203 When the handler is set, the dispatch source will perform a
209 To unset the event handler, call
210 .Fn dispatch_source_set_event_handler_f
213 This unsets the event handler regardless of whether the handler
214 was a function pointer or a block. Registration and cancellation handlers
215 (see below) may be unset in the same way, but as noted below, a cancellation
216 handler may be required.
220 is called on a suspended or newly created source, there may be a brief delay
221 before the source is ready to receive events from the underlying system handle.
222 During this delay, the event handler will not be invoked, and events will be
225 Once the dispatch source is registered with the underlying system and is ready
226 to process all events its optional registration handler will be submitted to
227 its target queue. This registration handler may be specified via
228 .Fn dispatch_source_set_registration_handler .
230 The event handler will not be called until the registration handler finishes.
231 If the source is canceled (see below) before it is registered,
232 its registration handler will not be called.
236 .Fn dispatch_source_cancel
237 function asynchronously cancels the dispatch source, preventing any further
238 invocation of its event handler block. Cancellation does not interrupt a
239 currently executing handler block (non-preemptive). If a source is canceled
240 before the first time it is resumed, its event handler will never be called.
241 (In this case, note that the source must be resumed before it can be released.)
244 .Fn dispatch_source_testcancel
245 function may be used to determine whether the specified source has been
246 canceled. A non-zero value will be returned if the source is canceled.
248 When a dispatch source is canceled its optional cancellation handler will be
249 submitted to its target queue. The cancellation handler may be specified via
250 .Fn dispatch_source_set_cancel_handler .
251 This cancellation handler is invoked only once, and only as a direct consequence
253 .Fn dispatch_source_cancel .
256 a cancellation handler is required for file descriptor and mach port based
257 sources in order to safely close the descriptor or destroy the port. Closing the
258 descriptor or port before the cancellation handler has run may result in a race
259 condition: if a new descriptor is allocated with the same value as the recently
260 closed descriptor while the source's event handler is still running, the event
261 handler may read/write data to the wrong descriptor.
263 .Sh DISPATCH SOURCE TYPES
264 The following section contains a summary of supported dispatch event types and
265 the interpretation of their parameters and returned data.
267 .Vt DISPATCH_SOURCE_TYPE_DATA_ADD ,
268 .Vt DISPATCH_SOURCE_TYPE_DATA_OR
270 Sources of this type allow applications to manually trigger the source's event
271 handler via a call to
272 .Fn dispatch_source_merge_data .
273 The data will be merged with the source's pending data via an atomic add or
274 logic OR (based on the source's type), and the event handler block will be
275 submitted to the source's target queue. The
277 is application defined. These sources have no
281 and zero should be used.
283 .Vt DISPATCH_SOURCE_TYPE_MACH_SEND
285 Sources of this type monitor a mach port with a send right for state changes.
288 is the mach port (mach_port_t) to monitor and the
291 .Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
292 .It \(bu DISPATCH_MACH_SEND_DEAD
293 The port's corresponding receive right has been destroyed
297 .Fn dispatch_source_get_data
298 indicates which of the events in the
302 .Vt DISPATCH_SOURCE_TYPE_MACH_RECV
304 Sources of this type monitor a mach port with a receive right for state changes.
307 is the mach port (mach_port_t) to monitor and the
309 is unused and should be zero.
310 The event handler block will be submitted to the target queue when a message
311 on the mach port is waiting to be received.
313 .Vt DISPATCH_SOURCE_TYPE_MEMORYPRESSURE
315 Sources of this type monitor the system memory pressure condition for state changes.
318 is unused and should be zero. The
320 may be one or more of the following:
321 .Bl -tag -width "XXDISPATCH_MEMORYPRESSURE_CRITICAL" -compact -offset indent
322 .It \(bu DISPATCH_MEMORYPRESSURE_NORMAL
323 The system memory pressure condition has returned to normal.
324 .It \(bu DISPATCH_MEMORYPRESSURE_WARN
325 The system memory pressure condition has changed to warning.
326 .It \(bu DISPATCH_MEMORYPRESSURE_CRITICAL
327 The system memory pressure condition has changed to critical.
331 .Fn dispatch_source_get_data
332 indicates which of the events in the
336 Elevated memory pressure is a system-wide condition that applications
337 registered for this source should react to by changing their future memory use
338 behavior, e.g. by reducing cache sizes of newly initiated operations until
339 memory pressure returns back to normal.
341 However, applications should
343 traverse and discard existing caches for past operations when the system memory
344 pressure enters an elevated state, as that is likely to trigger VM operations
345 that will further aggravate system memory pressure.
347 .Vt DISPATCH_SOURCE_TYPE_PROC
349 Sources of this type monitor processes for state changes.
352 is the process identifier (pid_t) of the process to monitor and the
354 may be one or more of the following:
355 .Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
356 .It \(bu DISPATCH_PROC_EXIT
357 The process has exited and is available to
359 .It \(bu DISPATCH_PROC_FORK
360 The process has created one or more child processes.
361 .It \(bu DISPATCH_PROC_EXEC
362 The process has become another executable image via a call to
366 .It \(bu DISPATCH_PROC_SIGNAL
367 A signal was delivered to the process.
371 .Fn dispatch_source_get_data
372 indicates which of the events in the
376 .Vt DISPATCH_SOURCE_TYPE_READ
378 Sources of this type monitor file descriptors for pending data.
381 is the file descriptor (int) to monitor and the
383 is unused and should be zero.
386 .Fn dispatch_source_get_data
387 is an estimated number of bytes available to be read from the descriptor. This
388 estimate should be treated as a suggested
390 read buffer size. There are no guarantees that a complete read of this size
393 Users of this source type are strongly encouraged to perform non-blocking I/O
394 and handle any truncated reads or error conditions that may occur. See
396 for additional information about setting the
398 flag on a file descriptor.
400 .Vt DISPATCH_SOURCE_TYPE_SIGNAL
402 Sources of this type monitor signals delivered to the current process. The
404 is the signal number to monitor (int) and the
406 is unused and should be zero.
409 .Fn dispatch_source_get_data
410 is the number of signals received since the last invocation of the event handler
413 Unlike signal handlers specified via
415 the execution of the event handler block does not interrupt the current thread
416 of execution; therefore the handler block is not limited to the use of signal
417 safe interfaces defined in
419 Furthermore, multiple observers of a given signal are supported; thus allowing
420 applications and libraries to cooperate safely. However, a dispatch source
422 install a signal handler or otherwise alter the behavior of signal delivery.
423 Therefore, applications must ignore or at least catch any signal that terminates
424 a process by default. For example, near the top of
426 .Bd -literal -offset ident
427 signal(SIGTERM, SIG_IGN);
430 .Vt DISPATCH_SOURCE_TYPE_TIMER
432 Sources of this type periodically submit the event handler block to the target
435 argument is unused and should be zero.
438 .Fn dispatch_source_get_data
439 is the number of times the timer has fired since the last invocation of the
442 The timer parameters are configured with the
443 .Fn dispatch_source_set_timer
444 function. Once this function returns, any pending source data accumulated for
445 the previous timer parameters has been cleared; the next fire of the timer will
450 nanoseconds thereafter until the timer source is canceled.
452 Any fire of the timer may be delayed by the system in order to improve power
453 consumption and system performance. The upper limit to the allowable delay may
454 be configured with the
456 argument, the lower limit is under the control of the system.
458 For the initial timer fire at
460 the upper limit to the allowable delay is set to
462 nanoseconds. For the subsequent timer fires at
472 The lower limit to the allowable delay may vary with process state such as
473 visibility of application UI. If the specified timer source was created with a
476 .Vt DISPATCH_TIMER_STRICT ,
477 the system will make a best effort to strictly observe the provided
479 value even if it is smaller than the current lower limit. Note that a minimal
480 amount of delay is to be expected even if this flag is specified.
484 argument also determines which clock will be used for the timer: If
487 .Vt DISPATCH_TIME_NOW
489 .Xr dispatch_time 3 ,
490 the timer is based on
491 .Fn mach_absolute_time .
495 .Xr dispatch_walltime 3 ,
496 the timer is based on
500 Under the C language, untyped numbers default to the
502 type. This can lead to truncation bugs when arithmetic operations with other
503 numbers are expected to generate a
505 sized result. When in doubt, use
507 as a suffix. For example:
508 .Bd -literal -offset indent
512 .Vt DISPATCH_SOURCE_TYPE_VNODE
514 Sources of this type monitor the virtual filesystem nodes for state changes.
517 is a file descriptor (int) referencing the node to monitor, and
520 may be one or more of the following:
521 .Bl -tag -width "XXDISPATCH_VNODE_ATTRIB" -compact -offset indent
522 .It \(bu DISPATCH_VNODE_DELETE
523 The referenced node was removed from the filesystem namespace via
525 .It \(bu DISPATCH_VNODE_WRITE
526 A write to the referenced file occurred
527 .It \(bu DISPATCH_VNODE_EXTEND
528 The referenced file was extended
529 .It \(bu DISPATCH_VNODE_ATTRIB
530 The metadata attributes of the referenced node have changed
531 .It \(bu DISPATCH_VNODE_LINK
532 The link count on the referenced node has changed
533 .It \(bu DISPATCH_VNODE_RENAME
534 The referenced node was renamed
535 .It \(bu DISPATCH_VNODE_REVOKE
536 Access to the referenced node was revoked via
538 or the underlying fileystem was unmounted.
542 .Fn dispatch_source_get_data
543 indicates which of the events in the
547 .Vt DISPATCH_SOURCE_TYPE_WRITE
549 Sources of this type monitor file descriptors for available write buffer space.
552 is the file descriptor (int) to monitor and the
554 is unused and should be zero.
556 Users of this source type are strongly encouraged to perform non-blocking I/O
557 and handle any truncated reads or error conditions that may occur. See
559 for additional information about setting the
561 flag on a file descriptor.
565 .Xr dispatch_object 3 ,
566 .Xr dispatch_queue_create 3