]> git.saurik.com Git - apple/libdispatch.git/blame - man/dispatch_source_create.3
libdispatch-913.30.4.tar.gz
[apple/libdispatch.git] / man / dispatch_source_create.3
CommitLineData
517da941 1.\" Copyright (c) 2008-2013 Apple Inc. All rights reserved.
0ab74447
A
2.Dd May 1, 2009
3.Dt dispatch_source_create 3
4.Os Darwin
5.Sh NAME
6.Nm dispatch_source_create
7.Nd dispatch event sources
8.Sh SYNOPSIS
9.Fd #include <dispatch/dispatch.h>
10.Ft dispatch_source_t
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"
16.Fc
17.Ft void
18.Fo dispatch_source_set_event_handler
19.Fa "dispatch_source_t source"
20.Fa "void (^block)(void)"
21.Fc
22.Ft void
23.Fo dispatch_source_set_event_handler_f
24.Fa "dispatch_source_t source"
25.Fa "void (*function)(void *)"
26.Fc
27.Ft void
c093abd6
A
28.Fo dispatch_source_set_registration_handler
29.Fa "dispatch_source_t source"
30.Fa "void (^block)(void)"
31.Fc
32.Ft void
33.Fo dispatch_source_set_registration_handler_f
34.Fa "dispatch_source_t source"
35.Fa "void (*function)(void *)"
36.Fc
37.Ft void
0ab74447
A
38.Fo dispatch_source_set_cancel_handler
39.Fa "dispatch_source_t source"
40.Fa "void (^block)(void)"
41.Fc
42.Ft void
43.Fo dispatch_source_set_cancel_handler_f
44.Fa "dispatch_source_t source"
45.Fa "void (*function)(void *)"
46.Fc
47.Ft void
48.Fo dispatch_source_cancel
49.Fa "dispatch_source_t source"
50.Fc
e85f4437 51.Ft long
0ab74447
A
52.Fo dispatch_source_testcancel
53.Fa "dispatch_source_t source"
54.Fc
55.Ft uintptr_t
56.Fo dispatch_source_get_handle
57.Fa "dispatch_source_t source"
58.Fc
59.Ft "unsigned long"
60.Fo dispatch_source_get_mask
61.Fa "dispatch_source_t source"
62.Fc
63.Ft "unsigned long"
64.Fo dispatch_source_get_data
65.Fa "dispatch_source_t source"
66.Fc
67.Ft void
68.Fo dispatch_source_merge_data
69.Fa "dispatch_source_t source"
70.Fa "unsigned long data"
71.Fc
72.Ft void
73.Fo dispatch_source_set_timer
74.Fa "dispatch_source_t source"
75.Fa "dispatch_time_t start"
76.Fa "uint64_t interval"
77.Fa "uint64_t leeway"
78.Fc
79.Sh DESCRIPTION
e85f4437 80Dispatch event sources may be used to monitor a variety of system objects and
0ab74447
A
81events including file descriptors, mach ports, processes, virtual filesystem
82nodes, signal delivery and timers.
83.Pp
84When a state change occurs, the dispatch source will submit its event handler
85block to its target queue.
86.Pp
87The
88.Fn dispatch_source_create
89function creates a new dispatch source object that may be retained and released
90with calls to
91.Fn dispatch_retain
92and
93.Fn dispatch_release
e85f4437
A
94respectively. The
95.Fa queue
96parameter specifies the target queue of the new source object, it will
97be retained by the source object. Pass the
98.Dv DISPATCH_TARGET_QUEUE_DEFAULT
99constant to use the default target queue (the default priority global
100concurrent queue).
101.Pp
102Newly created sources are created in a suspended state. After the source has
c093abd6
A
103been configured by setting an event handler, cancellation handler, registration
104handler, context,
e85f4437 105etc., the source must be activated by a call to
0ab74447
A
106.Fn dispatch_resume
107before any events will be delivered.
108.Pp
109Dispatch sources may be one of the following types:
110.Bl -bullet -compact -offset indent
111.It
112DISPATCH_SOURCE_TYPE_DATA_ADD
113.It
114DISPATCH_SOURCE_TYPE_DATA_OR
115.It
6b746eb4
A
116DISPATCH_SOURCE_TYPE_DATA_REPLACE
117.It
0ab74447
A
118DISPATCH_SOURCE_TYPE_MACH_SEND
119.It
120DISPATCH_SOURCE_TYPE_MACH_RECV
121.It
517da941
A
122DISPATCH_SOURCE_TYPE_MEMORYPRESSURE
123.It
0ab74447
A
124DISPATCH_SOURCE_TYPE_PROC
125.It
126DISPATCH_SOURCE_TYPE_READ
127.It
128DISPATCH_SOURCE_TYPE_SIGNAL
129.It
130DISPATCH_SOURCE_TYPE_TIMER
131.It
132DISPATCH_SOURCE_TYPE_VNODE
133.It
134DISPATCH_SOURCE_TYPE_WRITE
135.El
136.Pp
137The
138.Fa handle
139and
140.Fa mask
141arguments to
142.Fn dispatch_source_create
e85f4437 143and the return values of the
0ab74447
A
144.Fn dispatch_source_get_handle ,
145.Fn dispatch_source_get_mask ,
146and
e85f4437 147.Fn dispatch_source_get_data
0ab74447
A
148functions should be interpreted according to the type of the dispatch source.
149.Pp
e85f4437 150The
0ab74447
A
151.Fn dispatch_source_get_handle
152function
153returns the underlying handle to the dispatch source (i.e. file descriptor,
154mach port, process identifer, etc.). The result of this function may be cast
155directly to the underlying type.
156.Pp
e85f4437 157The
0ab74447
A
158.Fn dispatch_source_get_mask
159function
160returns the set of flags that were specified at source creation time via the
161.Fa mask
162argument.
163.Pp
164The
165.Fn dispatch_source_get_data
166function returns the currently pending data for the dispatch source.
167This function should only be called from within the source's event handler.
168The result of calling this function from any other context is undefined.
169.Pp
170The
171.Fn dispatch_source_merge_data
172function is intended for use with the
6b746eb4 173.Vt DISPATCH_SOURCE_TYPE_DATA_ADD ,
0ab74447 174.Vt DISPATCH_SOURCE_TYPE_DATA_OR
6b746eb4
A
175and
176.Vt DISPATCH_SOURCE_TYPE_DATA_REPLACE
0ab74447 177source types. The result of using this function with any other source type is
6b746eb4
A
178undefined. Data merging is performed according to the source type:
179.Bl -tag -width "XXDISPATCH_SOURCE_TYPE_DATA_REPLACE" -compact -offset indent
180.It \(bu DISPATCH_SOURCE_TYPE_DATA_ADD
181.Vt data
182is atomically added to the source's data
183.It \(bu DISPATCH_SOURCE_TYPE_DATA_OR
184.Vt data
185is atomically bitwise ORed into the source's data
186.It \(bu DISPATCH_SOURCE_TYPE_DATA_REPLACE
187.Vt data
188atomically replaces the source's data.
189.El
190.Pp
191If the source data value resulting from the merge operation is 0, the source
192handler will not be invoked. This can happen if:
193.Bl -bullet -compact -offset indent
194.It
195the atomic addition wraps for sources of type
196.Vt DISPATCH_SOURCE_TYPE_DATA_ADD ,
197.It
1980 is merged for sources of type
199.Vt DISPATCH_SOURCE_TYPE_DATA_REPLACE .
200.El
0ab74447
A
201.Pp
202.Sh SOURCE EVENT HANDLERS
203In order to receive events from the dispatch source, an event handler should be
204specified via
205.Fn dispatch_source_set_event_handler .
206The event handler block is submitted to the source's target queue when the state
c093abd6
A
207of the underlying system handle changes, or when an event occurs. If a source
208is resumed with no event handler block set, events will be quietly ignored.
209If the event handler block is changed while the source is suspended, or from a
210block running on a serial queue that is the source's target queue, then the next
211event handler invocation will use the new block.
0ab74447
A
212.Pp
213Dispatch sources may be suspended or resumed independently of their target
214queues using
215.Fn dispatch_suspend
216and
217.Fn dispatch_resume
218on the dispatch source directly. The data describing events which occur while a
219source is suspended are coalesced and delivered once the source is resumed.
220.Pp
221The
222.Fa handler
223block
224need not be reentrant safe, as it is not resubmitted to the target
225.Fa queue
226until any prior invocation for that dispatch source has completed.
e85f4437 227When the handler is set, the dispatch source will perform a
0ab74447
A
228.Fn Block_copy
229on the
230.Fa handler
231block.
232.Pp
c093abd6
A
233To unset the event handler, call
234.Fn dispatch_source_set_event_handler_f
235and pass NULL as
236.Fa function .
237This unsets the event handler regardless of whether the handler
238was a function pointer or a block. Registration and cancellation handlers
239(see below) may be unset in the same way, but as noted below, a cancellation
240handler may be required.
241.Sh REGISTRATION
242When
243.Fn dispatch_resume
244is called on a suspended or newly created source, there may be a brief delay
245before the source is ready to receive events from the underlying system handle.
246During this delay, the event handler will not be invoked, and events will be
247missed.
248.Pp
249Once the dispatch source is registered with the underlying system and is ready
250to process all events its optional registration handler will be submitted to
251its target queue. This registration handler may be specified via
252.Fn dispatch_source_set_registration_handler .
253.Pp
254The event handler will not be called until the registration handler finishes.
255If the source is canceled (see below) before it is registered,
256its registration handler will not be called.
257.Pp
0ab74447
A
258.Sh CANCELLATION
259The
260.Fn dispatch_source_cancel
261function asynchronously cancels the dispatch source, preventing any further
262invocation of its event handler block. Cancellation does not interrupt a
c093abd6
A
263currently executing handler block (non-preemptive). If a source is canceled
264before the first time it is resumed, its event handler will never be called.
265(In this case, note that the source must be resumed before it can be released.)
0ab74447
A
266.Pp
267The
268.Fn dispatch_source_testcancel
269function may be used to determine whether the specified source has been
270canceled. A non-zero value will be returned if the source is canceled.
271.Pp
272When a dispatch source is canceled its optional cancellation handler will be
273submitted to its target queue. The cancellation handler may be specified via
274.Fn dispatch_source_set_cancel_handler .
275This cancellation handler is invoked only once, and only as a direct consequence
276of calling
277.Fn dispatch_source_cancel .
278.Pp
279.Em Important:
280a cancellation handler is required for file descriptor and mach port based
281sources in order to safely close the descriptor or destroy the port. Closing the
282descriptor or port before the cancellation handler has run may result in a race
283condition: if a new descriptor is allocated with the same value as the recently
e85f4437 284closed descriptor while the source's event handler is still running, the event
0ab74447
A
285handler may read/write data to the wrong descriptor.
286.Pp
287.Sh DISPATCH SOURCE TYPES
288The following section contains a summary of supported dispatch event types and
289the interpretation of their parameters and returned data.
290.Pp
291.Vt DISPATCH_SOURCE_TYPE_DATA_ADD ,
6b746eb4
A
292.Vt DISPATCH_SOURCE_TYPE_DATA_OR ,
293.Vt DISPATCH_SOURCE_TYPE_DATA_REPLACE
0ab74447
A
294.Pp
295Sources of this type allow applications to manually trigger the source's event
e85f4437 296handler via a call to
0ab74447
A
297.Fn dispatch_source_merge_data .
298The data will be merged with the source's pending data via an atomic add or
6b746eb4
A
299atomic bitwise OR, or direct replacement (based on the source's type), and the
300event handler block will be submitted to the source's target queue. The
0ab74447 301.Fa data
e85f4437 302is application defined. These sources have no
0ab74447 303.Fa handle
e85f4437
A
304or
305.Fa mask
0ab74447
A
306and zero should be used.
307.Pp
308.Vt DISPATCH_SOURCE_TYPE_MACH_SEND
309.Pp
310Sources of this type monitor a mach port with a send right for state changes.
311The
312.Fa handle
313is the mach port (mach_port_t) to monitor and the
314.Fa mask
315may be:
316.Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
317.It \(bu DISPATCH_MACH_SEND_DEAD
318The port's corresponding receive right has been destroyed
319.El
320.Pp
321The data returned by
322.Fn dispatch_source_get_data
fa22f35b 323is a bitmask that indicates which of the events in the
0ab74447 324.Fa mask
beb15981
A
325were observed. Note that because this source type will request notifications on
326the provided port, it should not be mixed with the use of
45201a42
A
327.Fn mach_port_request_notification
328on the same port.
0ab74447
A
329.Pp
330.Vt DISPATCH_SOURCE_TYPE_MACH_RECV
331.Pp
332Sources of this type monitor a mach port with a receive right for state changes.
333The
334.Fa handle
335is the mach port (mach_port_t) to monitor and the
336.Fa mask
337is unused and should be zero.
338The event handler block will be submitted to the target queue when a message
339on the mach port is waiting to be received.
340.Pp
517da941
A
341.Vt DISPATCH_SOURCE_TYPE_MEMORYPRESSURE
342.Pp
beb15981
A
343Sources of this type monitor the system memory pressure condition for state
344changes. The
517da941
A
345.Fa handle
346is unused and should be zero. The
347.Fa mask
348may be one or more of the following:
349.Bl -tag -width "XXDISPATCH_MEMORYPRESSURE_CRITICAL" -compact -offset indent
350.It \(bu DISPATCH_MEMORYPRESSURE_NORMAL
351The system memory pressure condition has returned to normal.
352.It \(bu DISPATCH_MEMORYPRESSURE_WARN
353The system memory pressure condition has changed to warning.
354.It \(bu DISPATCH_MEMORYPRESSURE_CRITICAL
355The system memory pressure condition has changed to critical.
356.El
357.Pp
358The data returned by
359.Fn dispatch_source_get_data
360indicates which of the events in the
361.Fa mask
362were observed.
363.Pp
364Elevated memory pressure is a system-wide condition that applications
365registered for this source should react to by changing their future memory use
366behavior, e.g. by reducing cache sizes of newly initiated operations until
367memory pressure returns back to normal.
368.Pp
369However, applications should
370.Em NOT
371traverse and discard existing caches for past operations when the system memory
372pressure enters an elevated state, as that is likely to trigger VM operations
373that will further aggravate system memory pressure.
374.Pp
0ab74447
A
375.Vt DISPATCH_SOURCE_TYPE_PROC
376.Pp
377Sources of this type monitor processes for state changes.
378The
379.Fa handle
380is the process identifier (pid_t) of the process to monitor and the
381.Fa mask
382may be one or more of the following:
383.Bl -tag -width "XXDISPATCH_PROC_SIGNAL" -compact -offset indent
384.It \(bu DISPATCH_PROC_EXIT
e85f4437 385The process has exited and is available to
0ab74447
A
386.Xr wait 2 .
387.It \(bu DISPATCH_PROC_FORK
388The process has created one or more child processes.
389.It \(bu DISPATCH_PROC_EXEC
390The process has become another executable image via a call to
391.Xr execve 2
392or
393.Xr posix_spawn 2 .
0ab74447
A
394.It \(bu DISPATCH_PROC_SIGNAL
395A signal was delivered to the process.
396.El
397.Pp
398The data returned by
399.Fn dispatch_source_get_data
fa22f35b 400is a bitmask that indicates which of the events in the
0ab74447
A
401.Fa mask
402were observed.
403.Pp
404.Vt DISPATCH_SOURCE_TYPE_READ
405.Pp
406Sources of this type monitor file descriptors for pending data.
407The
408.Fa handle
409is the file descriptor (int) to monitor and the
410.Fa mask
411is unused and should be zero.
412.Pp
413The data returned by
414.Fn dispatch_source_get_data
415is an estimated number of bytes available to be read from the descriptor. This
416estimate should be treated as a suggested
417.Em minimum
418read buffer size. There are no guarantees that a complete read of this size
419will be performed.
420.Pp
421Users of this source type are strongly encouraged to perform non-blocking I/O
422and handle any truncated reads or error conditions that may occur. See
e85f4437 423.Xr fcntl 2
0ab74447
A
424for additional information about setting the
425.Vt O_NONBLOCK
426flag on a file descriptor.
427.Pp
428.Vt DISPATCH_SOURCE_TYPE_SIGNAL
429.Pp
430Sources of this type monitor signals delivered to the current process. The
431.Fa handle
432is the signal number to monitor (int) and the
433.Fa mask
434is unused and should be zero.
435.Pp
436The data returned by
437.Fn dispatch_source_get_data
438is the number of signals received since the last invocation of the event handler
439block.
440.Pp
441Unlike signal handlers specified via
442.Fn sigaction ,
443the execution of the event handler block does not interrupt the current thread
444of execution; therefore the handler block is not limited to the use of signal
445safe interfaces defined in
446.Xr sigaction 2 .
447Furthermore, multiple observers of a given signal are supported; thus allowing
448applications and libraries to cooperate safely. However, a dispatch source
449.Em does not
450install a signal handler or otherwise alter the behavior of signal delivery.
451Therefore, applications must ignore or at least catch any signal that terminates
452a process by default. For example, near the top of
453.Fn main :
454.Bd -literal -offset ident
455signal(SIGTERM, SIG_IGN);
456.Ed
457.Pp
458.Vt DISPATCH_SOURCE_TYPE_TIMER
459.Pp
460Sources of this type periodically submit the event handler block to the target
517da941 461queue. The
0ab74447 462.Fa handle
517da941 463argument is unused and should be zero.
0ab74447
A
464.Pp
465The data returned by
466.Fn dispatch_source_get_data
467is the number of times the timer has fired since the last invocation of the
468event handler block.
469.Pp
517da941 470The timer parameters are configured with the
0ab74447 471.Fn dispatch_source_set_timer
517da941
A
472function. Once this function returns, any pending source data accumulated for
473the previous timer parameters has been cleared; the next fire of the timer will
474occur at
475.Fa start ,
476and every
477.Fa interval
478nanoseconds thereafter until the timer source is canceled.
479.Pp
480Any fire of the timer may be delayed by the system in order to improve power
481consumption and system performance. The upper limit to the allowable delay may
482be configured with the
483.Fa leeway
484argument, the lower limit is under the control of the system.
485.Pp
486For the initial timer fire at
487.Fa start ,
488the upper limit to the allowable delay is set to
489.Fa leeway
490nanoseconds. For the subsequent timer fires at
0ab74447 491.Fa start
517da941 492.Li "+ N *"
0ab74447 493.Fa interval ,
517da941
A
494the upper limit is
495.Li MIN(
0ab74447 496.Fa leeway ,
517da941
A
497.Fa interval
498.Li "/ 2 )" .
499.Pp
500The lower limit to the allowable delay may vary with process state such as
501visibility of application UI. If the specified timer source was created with a
502.Fa mask
503of
504.Vt DISPATCH_TIMER_STRICT ,
505the system will make a best effort to strictly observe the provided
506.Fa leeway
507value even if it is smaller than the current lower limit. Note that a minimal
508amount of delay is to be expected even if this flag is specified.
509.Pp
510The
511.Fa start
512argument also determines which clock will be used for the timer: If
513.Fa start
514is
515.Vt DISPATCH_TIME_NOW
516or was created with
517.Xr dispatch_time 3 ,
518the timer is based on
519.Fn mach_absolute_time .
520If
521.Fa start
522was created with
523.Xr dispatch_walltime 3 ,
524the timer is based on
525.Xr gettimeofday 3 .
0ab74447 526.Pp
0ab74447
A
527.Vt DISPATCH_SOURCE_TYPE_VNODE
528.Pp
529Sources of this type monitor the virtual filesystem nodes for state changes.
530The
531.Fa handle
e85f4437 532is a file descriptor (int) referencing the node to monitor, and
0ab74447
A
533the
534.Fa mask
535may be one or more of the following:
536.Bl -tag -width "XXDISPATCH_VNODE_ATTRIB" -compact -offset indent
537.It \(bu DISPATCH_VNODE_DELETE
538The referenced node was removed from the filesystem namespace via
539.Xr unlink 2 .
540.It \(bu DISPATCH_VNODE_WRITE
beb15981 541A write to the referenced file occurred.
0ab74447 542.It \(bu DISPATCH_VNODE_EXTEND
beb15981 543The referenced file was extended.
0ab74447 544.It \(bu DISPATCH_VNODE_ATTRIB
beb15981 545The metadata attributes of the referenced node have changed.
0ab74447 546.It \(bu DISPATCH_VNODE_LINK
beb15981 547The link count on the referenced node has changed.
0ab74447 548.It \(bu DISPATCH_VNODE_RENAME
beb15981 549The referenced node was renamed.
0ab74447 550.It \(bu DISPATCH_VNODE_REVOKE
e85f4437 551Access to the referenced node was revoked via
0ab74447
A
552.Xr revoke 2
553or the underlying fileystem was unmounted.
beb15981
A
554.It \(bu DISPATCH_VNODE_FUNLOCK
555The referenced file was unlocked by
556.Xr flock 2
557or
558.Xr close 2 .
0ab74447
A
559.El
560.Pp
561The data returned by
562.Fn dispatch_source_get_data
fa22f35b 563is a bitmask that indicates which of the events in the
0ab74447
A
564.Fa mask
565were observed.
566.Pp
567.Vt DISPATCH_SOURCE_TYPE_WRITE
568.Pp
569Sources of this type monitor file descriptors for available write buffer space.
570The
571.Fa handle
572is the file descriptor (int) to monitor and the
573.Fa mask
574is unused and should be zero.
575.Pp
576Users of this source type are strongly encouraged to perform non-blocking I/O
577and handle any truncated reads or error conditions that may occur. See
e85f4437 578.Xr fcntl 2
0ab74447
A
579for additional information about setting the
580.Vt O_NONBLOCK
581flag on a file descriptor.
582.Pp
583.Sh SEE ALSO
584.Xr dispatch 3 ,
585.Xr dispatch_object 3 ,
586.Xr dispatch_queue_create 3