1 .\" Copyright (c) 2008-2009 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_cancel_handler
29 .Fa "dispatch_source_t source"
30 .Fa "void (^block)(void)"
33 .Fo dispatch_source_set_cancel_handler_f
34 .Fa "dispatch_source_t source"
35 .Fa "void (*function)(void *)"
38 .Fo dispatch_source_cancel
39 .Fa "dispatch_source_t source"
42 .Fo dispatch_source_testcancel
43 .Fa "dispatch_source_t source"
46 .Fo dispatch_source_get_handle
47 .Fa "dispatch_source_t source"
50 .Fo dispatch_source_get_mask
51 .Fa "dispatch_source_t source"
54 .Fo dispatch_source_get_data
55 .Fa "dispatch_source_t source"
58 .Fo dispatch_source_merge_data
59 .Fa "dispatch_source_t source"
60 .Fa "unsigned long data"
63 .Fo dispatch_source_set_timer
64 .Fa "dispatch_source_t source"
65 .Fa "dispatch_time_t start"
66 .Fa "uint64_t interval"
70 Dispatch event sources may be used to monitor a variety of system objects and
71 events including file descriptors, mach ports, processes, virtual filesystem
72 nodes, signal delivery and timers.
74 When a state change occurs, the dispatch source will submit its event handler
75 block to its target queue.
78 .Fn dispatch_source_create
79 function creates a new dispatch source object that may be retained and released
84 respectively. Newly created sources are created in a suspended state. After the
85 source has been configured by setting an event handler, cancellation handler,
86 context, etc., the source must be activated by a call to
88 before any events will be delivered.
90 Dispatch sources may be one of the following types:
91 .Bl -bullet -compact -offset indent
93 DISPATCH_SOURCE_TYPE_DATA_ADD
95 DISPATCH_SOURCE_TYPE_DATA_OR
97 DISPATCH_SOURCE_TYPE_MACH_SEND
99 DISPATCH_SOURCE_TYPE_MACH_RECV
101 DISPATCH_SOURCE_TYPE_PROC
103 DISPATCH_SOURCE_TYPE_READ
105 DISPATCH_SOURCE_TYPE_SIGNAL
107 DISPATCH_SOURCE_TYPE_TIMER
109 DISPATCH_SOURCE_TYPE_VNODE
111 DISPATCH_SOURCE_TYPE_WRITE
119 .Fn dispatch_source_create
120 and the return values of the
121 .Fn dispatch_source_get_handle ,
122 .Fn dispatch_source_get_mask ,
124 .Fn dispatch_source_get_data
125 functions should be interpreted according to the type of the dispatch source.
128 .Fn dispatch_source_get_handle
130 returns the underlying handle to the dispatch source (i.e. file descriptor,
131 mach port, process identifer, etc.). The result of this function may be cast
132 directly to the underlying type.
135 .Fn dispatch_source_get_mask
137 returns the set of flags that were specified at source creation time via the
142 .Fn dispatch_source_get_data
143 function returns the currently pending data for the dispatch source.
144 This function should only be called from within the source's event handler.
145 The result of calling this function from any other context is undefined.
148 .Fn dispatch_source_merge_data
149 function is intended for use with the
150 .Vt DISPATCH_SOURCE_TYPE_DATA_ADD
152 .Vt DISPATCH_SOURCE_TYPE_DATA_OR
153 source types. The result of using this function with any other source type is
154 undefined. Calling this function will atomically add or logical OR the data
155 into the source's data, and trigger the delivery of the source's event handler.
157 .Sh SOURCE EVENT HANDLERS
158 In order to receive events from the dispatch source, an event handler should be
160 .Fn dispatch_source_set_event_handler .
161 The event handler block is submitted to the source's target queue when the state
162 of the underlying system handle changes, or when an event occurs.
164 Dispatch sources may be suspended or resumed independently of their target
169 on the dispatch source directly. The data describing events which occur while a
170 source is suspended are coalesced and delivered once the source is resumed.
175 need not be reentrant safe, as it is not resubmitted to the target
177 until any prior invocation for that dispatch source has completed.
178 When the handler is set, the dispatch source will perform a
186 .Fn dispatch_source_cancel
187 function asynchronously cancels the dispatch source, preventing any further
188 invocation of its event handler block. Cancellation does not interrupt a
189 currently executing handler block (non-preemptive).
192 .Fn dispatch_source_testcancel
193 function may be used to determine whether the specified source has been
194 canceled. A non-zero value will be returned if the source is canceled.
196 When a dispatch source is canceled its optional cancellation handler will be
197 submitted to its target queue. The cancellation handler may be specified via
198 .Fn dispatch_source_set_cancel_handler .
199 This cancellation handler is invoked only once, and only as a direct consequence
201 .Fn dispatch_source_cancel .
204 a cancellation handler is required for file descriptor and mach port based
205 sources in order to safely close the descriptor or destroy the port. Closing the
206 descriptor or port before the cancellation handler has run may result in a race
207 condition: if a new descriptor is allocated with the same value as the recently
208 closed descriptor while the source's event handler is still running, the event
209 handler may read/write data to the wrong descriptor.
211 .Sh DISPATCH SOURCE TYPES
212 The following section contains a summary of supported dispatch event types and
213 the interpretation of their parameters and returned data.
215 .Vt DISPATCH_SOURCE_TYPE_DATA_ADD ,
216 .Vt DISPATCH_SOURCE_TYPE_DATA_OR
218 Sources of this type allow applications to manually trigger the source's event
219 handler via a call to
220 .Fn dispatch_source_merge_data .
221 The data will be merged with the source's pending data via an atomic add or
222 logic OR (based on the source's type), and the event handler block will be
223 submitted to the source's target queue. The
225 is application defined. These sources have no
229 and zero should be used.
231 .Vt DISPATCH_SOURCE_TYPE_MACH_SEND
233 Sources of this type monitor a mach port with a send right for state changes.
236 is the mach port (mach_port_t) to monitor and the
239 .Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
240 .It \(bu DISPATCH_MACH_SEND_DEAD
241 The port's corresponding receive right has been destroyed
245 .Fn dispatch_source_get_data
246 indicates which of the events in the
250 .Vt DISPATCH_SOURCE_TYPE_MACH_RECV
252 Sources of this type monitor a mach port with a receive right for state changes.
255 is the mach port (mach_port_t) to monitor and the
257 is unused and should be zero.
258 The event handler block will be submitted to the target queue when a message
259 on the mach port is waiting to be received.
261 .Vt DISPATCH_SOURCE_TYPE_PROC
263 Sources of this type monitor processes for state changes.
266 is the process identifier (pid_t) of the process to monitor and the
268 may be one or more of the following:
269 .Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
270 .It \(bu DISPATCH_PROC_EXIT
271 The process has exited and is available to
273 .It \(bu DISPATCH_PROC_FORK
274 The process has created one or more child processes.
275 .It \(bu DISPATCH_PROC_EXEC
276 The process has become another executable image via a call to
280 .It \(bu DISPATCH_PROC_REAP
281 The process status has been collected by its parent process via
283 .It \(bu DISPATCH_PROC_SIGNAL
284 A signal was delivered to the process.
288 .Fn dispatch_source_get_data
289 indicates which of the events in the
293 .Vt DISPATCH_SOURCE_TYPE_READ
295 Sources of this type monitor file descriptors for pending data.
298 is the file descriptor (int) to monitor and the
300 is unused and should be zero.
303 .Fn dispatch_source_get_data
304 is an estimated number of bytes available to be read from the descriptor. This
305 estimate should be treated as a suggested
307 read buffer size. There are no guarantees that a complete read of this size
310 Users of this source type are strongly encouraged to perform non-blocking I/O
311 and handle any truncated reads or error conditions that may occur. See
313 for additional information about setting the
315 flag on a file descriptor.
317 .Vt DISPATCH_SOURCE_TYPE_SIGNAL
319 Sources of this type monitor signals delivered to the current process. The
321 is the signal number to monitor (int) and the
323 is unused and should be zero.
326 .Fn dispatch_source_get_data
327 is the number of signals received since the last invocation of the event handler
330 Unlike signal handlers specified via
332 the execution of the event handler block does not interrupt the current thread
333 of execution; therefore the handler block is not limited to the use of signal
334 safe interfaces defined in
336 Furthermore, multiple observers of a given signal are supported; thus allowing
337 applications and libraries to cooperate safely. However, a dispatch source
339 install a signal handler or otherwise alter the behavior of signal delivery.
340 Therefore, applications must ignore or at least catch any signal that terminates
341 a process by default. For example, near the top of
343 .Bd -literal -offset ident
344 signal(SIGTERM, SIG_IGN);
347 .Vt DISPATCH_SOURCE_TYPE_TIMER
349 Sources of this type periodically submit the event handler block to the target
350 queue on an interval specified by
351 .Fn dispatch_source_set_timer .
356 arguments are unused and should be zero.
358 A best effort attempt is made to submit the event handler block to the target
359 queue at the specified time; however, actual invocation may occur at a later
363 .Fn dispatch_source_get_data
364 is the number of times the timer has fired since the last invocation of the
368 .Fn dispatch_source_set_timer
369 takes as an argument the
371 time of the timer (initial fire time) represented as a
372 .Vt dispatch_time_t .
373 The timer dispatch source will use the same clock as the function used to
374 create this value. (See
376 for more information.) The
378 in nanoseconds, specifies the period at which the timer should repeat. All
379 timers will repeat indefinitely until
380 .Fn dispatch_source_cancel
383 in nanoseconds, is a hint to the system that it may defer the timer in order to
384 align with other system activity for improved system performance or reduced
385 power consumption. (For example, an application might perform a periodic task
386 every 5 minutes with a leeway of up to 30 seconds.) Note that some latency is
387 to be expected for all timers even when a value of zero is used.
390 Under the C language, untyped numbers default to the
392 type. This can lead to truncation bugs when arithmetic operations with other
393 numbers are expected to generate a
395 sized result. When in doubt, use
397 as a suffix. For example:
398 .Bd -literal -offset indent
402 .Vt DISPATCH_SOURCE_TYPE_VNODE
404 Sources of this type monitor the virtual filesystem nodes for state changes.
407 is a file descriptor (int) referencing the node to monitor, and
410 may be one or more of the following:
411 .Bl -tag -width "XXDISPATCH_VNODE_ATTRIB" -compact -offset indent
412 .It \(bu DISPATCH_VNODE_DELETE
413 The referenced node was removed from the filesystem namespace via
415 .It \(bu DISPATCH_VNODE_WRITE
416 A write to the referenced file occurred
417 .It \(bu DISPATCH_VNODE_EXTEND
418 The referenced file was extended
419 .It \(bu DISPATCH_VNODE_ATTRIB
420 The metadata attributes of the referenced node have changed
421 .It \(bu DISPATCH_VNODE_LINK
422 The link count on the referenced node has changed
423 .It \(bu DISPATCH_VNODE_RENAME
424 The referenced node was renamed
425 .It \(bu DISPATCH_VNODE_REVOKE
426 Access to the referenced node was revoked via
428 or the underlying fileystem was unmounted.
432 .Fn dispatch_source_get_data
433 indicates which of the events in the
437 .Vt DISPATCH_SOURCE_TYPE_WRITE
439 Sources of this type monitor file descriptors for available write buffer space.
442 is the file descriptor (int) to monitor and the
444 is unused and should be zero.
446 Users of this source type are strongly encouraged to perform non-blocking I/O
447 and handle any truncated reads or error conditions that may occur. See
449 for additional information about setting the
451 flag on a file descriptor.
455 .Xr dispatch_object 3 ,
456 .Xr dispatch_queue_create 3