1 .\" Copyright (c) 2008-2010 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
86 parameter specifies the target queue of the new source object, it will
87 be retained by the source object. Pass the
88 .Dv DISPATCH_TARGET_QUEUE_DEFAULT
89 constant to use the default target queue (the default priority global
92 Newly created sources are created in a suspended state. After the source has
93 been configured by setting an event handler, cancellation handler, context,
94 etc., the source must be activated by a call to
96 before any events will be delivered.
98 Dispatch sources may be one of the following types:
99 .Bl -bullet -compact -offset indent
101 DISPATCH_SOURCE_TYPE_DATA_ADD
103 DISPATCH_SOURCE_TYPE_DATA_OR
105 DISPATCH_SOURCE_TYPE_MACH_SEND
107 DISPATCH_SOURCE_TYPE_MACH_RECV
109 DISPATCH_SOURCE_TYPE_PROC
111 DISPATCH_SOURCE_TYPE_READ
113 DISPATCH_SOURCE_TYPE_SIGNAL
115 DISPATCH_SOURCE_TYPE_TIMER
117 DISPATCH_SOURCE_TYPE_VNODE
119 DISPATCH_SOURCE_TYPE_WRITE
127 .Fn dispatch_source_create
128 and the return values of the
129 .Fn dispatch_source_get_handle ,
130 .Fn dispatch_source_get_mask ,
132 .Fn dispatch_source_get_data
133 functions should be interpreted according to the type of the dispatch source.
136 .Fn dispatch_source_get_handle
138 returns the underlying handle to the dispatch source (i.e. file descriptor,
139 mach port, process identifer, etc.). The result of this function may be cast
140 directly to the underlying type.
143 .Fn dispatch_source_get_mask
145 returns the set of flags that were specified at source creation time via the
150 .Fn dispatch_source_get_data
151 function returns the currently pending data for the dispatch source.
152 This function should only be called from within the source's event handler.
153 The result of calling this function from any other context is undefined.
156 .Fn dispatch_source_merge_data
157 function is intended for use with the
158 .Vt DISPATCH_SOURCE_TYPE_DATA_ADD
160 .Vt DISPATCH_SOURCE_TYPE_DATA_OR
161 source types. The result of using this function with any other source type is
162 undefined. Calling this function will atomically add or logical OR the data
163 into the source's data, and trigger the delivery of the source's event handler.
165 .Sh SOURCE EVENT HANDLERS
166 In order to receive events from the dispatch source, an event handler should be
168 .Fn dispatch_source_set_event_handler .
169 The event handler block is submitted to the source's target queue when the state
170 of the underlying system handle changes, or when an event occurs.
172 Dispatch sources may be suspended or resumed independently of their target
177 on the dispatch source directly. The data describing events which occur while a
178 source is suspended are coalesced and delivered once the source is resumed.
183 need not be reentrant safe, as it is not resubmitted to the target
185 until any prior invocation for that dispatch source has completed.
186 When the handler is set, the dispatch source will perform a
194 .Fn dispatch_source_cancel
195 function asynchronously cancels the dispatch source, preventing any further
196 invocation of its event handler block. Cancellation does not interrupt a
197 currently executing handler block (non-preemptive).
200 .Fn dispatch_source_testcancel
201 function may be used to determine whether the specified source has been
202 canceled. A non-zero value will be returned if the source is canceled.
204 When a dispatch source is canceled its optional cancellation handler will be
205 submitted to its target queue. The cancellation handler may be specified via
206 .Fn dispatch_source_set_cancel_handler .
207 This cancellation handler is invoked only once, and only as a direct consequence
209 .Fn dispatch_source_cancel .
212 a cancellation handler is required for file descriptor and mach port based
213 sources in order to safely close the descriptor or destroy the port. Closing the
214 descriptor or port before the cancellation handler has run may result in a race
215 condition: if a new descriptor is allocated with the same value as the recently
216 closed descriptor while the source's event handler is still running, the event
217 handler may read/write data to the wrong descriptor.
219 .Sh DISPATCH SOURCE TYPES
220 The following section contains a summary of supported dispatch event types and
221 the interpretation of their parameters and returned data.
223 .Vt DISPATCH_SOURCE_TYPE_DATA_ADD ,
224 .Vt DISPATCH_SOURCE_TYPE_DATA_OR
226 Sources of this type allow applications to manually trigger the source's event
227 handler via a call to
228 .Fn dispatch_source_merge_data .
229 The data will be merged with the source's pending data via an atomic add or
230 logic OR (based on the source's type), and the event handler block will be
231 submitted to the source's target queue. The
233 is application defined. These sources have no
237 and zero should be used.
239 .Vt DISPATCH_SOURCE_TYPE_MACH_SEND
241 Sources of this type monitor a mach port with a send right for state changes.
244 is the mach port (mach_port_t) to monitor and the
247 .Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
248 .It \(bu DISPATCH_MACH_SEND_DEAD
249 The port's corresponding receive right has been destroyed
253 .Fn dispatch_source_get_data
254 indicates which of the events in the
258 .Vt DISPATCH_SOURCE_TYPE_MACH_RECV
260 Sources of this type monitor a mach port with a receive right for state changes.
263 is the mach port (mach_port_t) to monitor and the
265 is unused and should be zero.
266 The event handler block will be submitted to the target queue when a message
267 on the mach port is waiting to be received.
269 .Vt DISPATCH_SOURCE_TYPE_PROC
271 Sources of this type monitor processes for state changes.
274 is the process identifier (pid_t) of the process to monitor and the
276 may be one or more of the following:
277 .Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
278 .It \(bu DISPATCH_PROC_EXIT
279 The process has exited and is available to
281 .It \(bu DISPATCH_PROC_FORK
282 The process has created one or more child processes.
283 .It \(bu DISPATCH_PROC_EXEC
284 The process has become another executable image via a call to
288 .It \(bu DISPATCH_PROC_REAP
289 The process status has been collected by its parent process via
291 .It \(bu DISPATCH_PROC_SIGNAL
292 A signal was delivered to the process.
296 .Fn dispatch_source_get_data
297 indicates which of the events in the
301 .Vt DISPATCH_SOURCE_TYPE_READ
303 Sources of this type monitor file descriptors for pending data.
306 is the file descriptor (int) to monitor and the
308 is unused and should be zero.
311 .Fn dispatch_source_get_data
312 is an estimated number of bytes available to be read from the descriptor. This
313 estimate should be treated as a suggested
315 read buffer size. There are no guarantees that a complete read of this size
318 Users of this source type are strongly encouraged to perform non-blocking I/O
319 and handle any truncated reads or error conditions that may occur. See
321 for additional information about setting the
323 flag on a file descriptor.
325 .Vt DISPATCH_SOURCE_TYPE_SIGNAL
327 Sources of this type monitor signals delivered to the current process. The
329 is the signal number to monitor (int) and the
331 is unused and should be zero.
334 .Fn dispatch_source_get_data
335 is the number of signals received since the last invocation of the event handler
338 Unlike signal handlers specified via
340 the execution of the event handler block does not interrupt the current thread
341 of execution; therefore the handler block is not limited to the use of signal
342 safe interfaces defined in
344 Furthermore, multiple observers of a given signal are supported; thus allowing
345 applications and libraries to cooperate safely. However, a dispatch source
347 install a signal handler or otherwise alter the behavior of signal delivery.
348 Therefore, applications must ignore or at least catch any signal that terminates
349 a process by default. For example, near the top of
351 .Bd -literal -offset ident
352 signal(SIGTERM, SIG_IGN);
355 .Vt DISPATCH_SOURCE_TYPE_TIMER
357 Sources of this type periodically submit the event handler block to the target
358 queue on an interval specified by
359 .Fn dispatch_source_set_timer .
364 arguments are unused and should be zero.
366 A best effort attempt is made to submit the event handler block to the target
367 queue at the specified time; however, actual invocation may occur at a later
371 .Fn dispatch_source_get_data
372 is the number of times the timer has fired since the last invocation of the
376 .Fn dispatch_source_set_timer
377 takes as an argument the
379 time of the timer (initial fire time) represented as a
380 .Vt dispatch_time_t .
381 The timer dispatch source will use the same clock as the function used to
382 create this value. (See
384 for more information.) The
386 in nanoseconds, specifies the period at which the timer should repeat. All
387 timers will repeat indefinitely until
388 .Fn dispatch_source_cancel
391 in nanoseconds, is a hint to the system that it may defer the timer in order to
392 align with other system activity for improved system performance or reduced
393 power consumption. (For example, an application might perform a periodic task
394 every 5 minutes with a leeway of up to 30 seconds.) Note that some latency is
395 to be expected for all timers even when a value of zero is used.
398 Under the C language, untyped numbers default to the
400 type. This can lead to truncation bugs when arithmetic operations with other
401 numbers are expected to generate a
403 sized result. When in doubt, use
405 as a suffix. For example:
406 .Bd -literal -offset indent
410 .Vt DISPATCH_SOURCE_TYPE_VNODE
412 Sources of this type monitor the virtual filesystem nodes for state changes.
415 is a file descriptor (int) referencing the node to monitor, and
418 may be one or more of the following:
419 .Bl -tag -width "XXDISPATCH_VNODE_ATTRIB" -compact -offset indent
420 .It \(bu DISPATCH_VNODE_DELETE
421 The referenced node was removed from the filesystem namespace via
423 .It \(bu DISPATCH_VNODE_WRITE
424 A write to the referenced file occurred
425 .It \(bu DISPATCH_VNODE_EXTEND
426 The referenced file was extended
427 .It \(bu DISPATCH_VNODE_ATTRIB
428 The metadata attributes of the referenced node have changed
429 .It \(bu DISPATCH_VNODE_LINK
430 The link count on the referenced node has changed
431 .It \(bu DISPATCH_VNODE_RENAME
432 The referenced node was renamed
433 .It \(bu DISPATCH_VNODE_REVOKE
434 Access to the referenced node was revoked via
436 or the underlying fileystem was unmounted.
440 .Fn dispatch_source_get_data
441 indicates which of the events in the
445 .Vt DISPATCH_SOURCE_TYPE_WRITE
447 Sources of this type monitor file descriptors for available write buffer space.
450 is the file descriptor (int) to monitor and the
452 is unused and should be zero.
454 Users of this source type are strongly encouraged to perform non-blocking I/O
455 and handle any truncated reads or error conditions that may occur. See
457 for additional information about setting the
459 flag on a file descriptor.
463 .Xr dispatch_object 3 ,
464 .Xr dispatch_queue_create 3