When a state change occurs, the dispatch source will submit its event handler block to its target queue.
The Fn dispatch_source_create function creates a new dispatch source object that may be retained and released with calls to Fn dispatch_retain and Fn dispatch_release respectively. The Fa queue parameter specifies the target queue of the new source object, it will be retained by the source object. Pass the DISPATCH_TARGET_QUEUE_DEFAULT constant to use the default target queue (the default priority global concurrent queue).
Newly created sources are created in a suspended state. After the source has been configured by setting an event handler, cancellation handler, registration handler, context, etc., the source must be activated by a call to Fn dispatch_resume before any events will be delivered.
Dispatch sources may be one of the following types:
The Fa handle and Fa mask arguments to Fn dispatch_source_create and the return values of the Fn dispatch_source_get_handle , Fn dispatch_source_get_mask , and Fn dispatch_source_get_data functions should be interpreted according to the type of the dispatch source.
The Fn dispatch_source_get_handle function returns the underlying handle to the dispatch source (i.e. file descriptor, mach port, process identifer, etc.). The result of this function may be cast directly to the underlying type.
The Fn dispatch_source_get_mask function returns the set of flags that were specified at source creation time via the Fa mask argument.
The Fn dispatch_source_get_data function returns the currently pending data for the dispatch source. This function should only be called from within the source's event handler. The result of calling this function from any other context is undefined.
The Fn dispatch_source_merge_data function is intended for use with the Vt DISPATCH_SOURCE_TYPE_DATA_ADD , Vt DISPATCH_SOURCE_TYPE_DATA_OR and Vt DISPATCH_SOURCE_TYPE_DATA_REPLACE source types. The result of using this function with any other source type is undefined. Data merging is performed according to the source type:
If the source data value resulting from the merge operation is 0, the source handler will not be invoked. This can happen if:
Dispatch sources may be suspended or resumed independently of their target queues using Fn dispatch_suspend and Fn dispatch_resume on the dispatch source directly. The data describing events which occur while a source is suspended are coalesced and delivered once the source is resumed.
The Fa handler block need not be reentrant safe, as it is not resubmitted to the target Fa queue until any prior invocation for that dispatch source has completed. When the handler is set, the dispatch source will perform a Fn Block_copy on the Fa handler block.
To unset the event handler, call Fn dispatch_source_set_event_handler_f and pass NULL as Fa function . This unsets the event handler regardless of whether the handler was a function pointer or a block. Registration and cancellation handlers (see below) may be unset in the same way, but as noted below, a cancellation handler may be required.
Once the dispatch source is registered with the underlying system and is ready to process all events its optional registration handler will be submitted to its target queue. This registration handler may be specified via Fn dispatch_source_set_registration_handler .
The event handler will not be called until the registration handler finishes. If the source is canceled (see below) before it is registered, its registration handler will not be called.
The Fn dispatch_source_testcancel function may be used to determine whether the specified source has been canceled. A non-zero value will be returned if the source is canceled.
When a dispatch source is canceled its optional cancellation handler will be submitted to its target queue. The cancellation handler may be specified via Fn dispatch_source_set_cancel_handler . This cancellation handler is invoked only once, and only as a direct consequence of calling Fn dispatch_source_cancel .
Important: a cancellation handler is required for file descriptor and mach port based sources in order to safely close the descriptor or destroy the port. Closing the descriptor or port before the cancellation handler has run may result in a race condition: if a new descriptor is allocated with the same value as the recently closed descriptor while the source's event handler is still running, the event handler may read/write data to the wrong descriptor.
Vt DISPATCH_SOURCE_TYPE_DATA_ADD , Vt DISPATCH_SOURCE_TYPE_DATA_OR , Vt DISPATCH_SOURCE_TYPE_DATA_REPLACE
Sources of this type allow applications to manually trigger the source's event handler via a call to Fn dispatch_source_merge_data . The data will be merged with the source's pending data via an atomic add or atomic bitwise OR, or direct replacement (based on the source's type), and the event handler block will be submitted to the source's target queue. The Fa data is application defined. These sources have no Fa handle or Fa mask and zero should be used.
Vt DISPATCH_SOURCE_TYPE_MACH_SEND
Sources of this type monitor a mach port with a send right for state changes. The Fa handle is the mach port (mach_port_t) to monitor and the Fa mask may be:
The data returned by Fn dispatch_source_get_data is a bitmask that indicates which of the events in the Fa mask were observed. Note that because this source type will request notifications on the provided port, it should not be mixed with the use of Fn mach_port_request_notification on the same port.
Vt DISPATCH_SOURCE_TYPE_MACH_RECV
Sources of this type monitor a mach port with a receive right for state changes. The Fa handle is the mach port (mach_port_t) to monitor and the Fa mask is unused and should be zero. The event handler block will be submitted to the target queue when a message on the mach port is waiting to be received.
Vt DISPATCH_SOURCE_TYPE_MEMORYPRESSURE
Sources of this type monitor the system memory pressure condition for state changes. The Fa handle is unused and should be zero. The Fa mask may be one or more of the following:
The data returned by Fn dispatch_source_get_data indicates which of the events in the Fa mask were observed.
Elevated memory pressure is a system-wide condition that applications registered for this source should react to by changing their future memory use behavior, e.g. by reducing cache sizes of newly initiated operations until memory pressure returns back to normal.
However, applications should NOT traverse and discard existing caches for past operations when the system memory pressure enters an elevated state, as that is likely to trigger VM operations that will further aggravate system memory pressure.
Vt DISPATCH_SOURCE_TYPE_PROC
Sources of this type monitor processes for state changes. The Fa handle is the process identifier (pid_t) of the process to monitor and the Fa mask may be one or more of the following:
The data returned by Fn dispatch_source_get_data is a bitmask that indicates which of the events in the Fa mask were observed.
Vt DISPATCH_SOURCE_TYPE_READ
Sources of this type monitor file descriptors for pending data. The Fa handle is the file descriptor (int) to monitor and the Fa mask is unused and should be zero.
The data returned by Fn dispatch_source_get_data is an estimated number of bytes available to be read from the descriptor. This estimate should be treated as a suggested minimum read buffer size. There are no guarantees that a complete read of this size will be performed.
Users of this source type are strongly encouraged to perform non-blocking I/O and handle any truncated reads or error conditions that may occur. See fcntl(2) for additional information about setting the Vt O_NONBLOCK flag on a file descriptor.
Vt DISPATCH_SOURCE_TYPE_SIGNAL
Sources of this type monitor signals delivered to the current process. The Fa handle is the signal number to monitor (int) and the Fa mask is unused and should be zero.
The data returned by Fn dispatch_source_get_data is the number of signals received since the last invocation of the event handler block.
Unlike signal handlers specified via Fn sigaction , the execution of the event handler block does not interrupt the current thread of execution; therefore the handler block is not limited to the use of signal safe interfaces defined in sigaction(2). Furthermore, multiple observers of a given signal are supported; thus allowing applications and libraries to cooperate safely. However, a dispatch source does not install a signal handler or otherwise alter the behavior of signal delivery. Therefore, applications must ignore or at least catch any signal that terminates a process by default. For example, near the top of Fn main :
signal(SIGTERM, SIG_IGN);
Vt DISPATCH_SOURCE_TYPE_TIMER
Sources of this type periodically submit the event handler block to the target queue. The Fa handle argument is unused and should be zero.
The data returned by Fn dispatch_source_get_data is the number of times the timer has fired since the last invocation of the event handler block.
The timer parameters are configured with the Fn dispatch_source_set_timer function. Once this function returns, any pending source data accumulated for the previous timer parameters has been cleared; the next fire of the timer will occur at Fa start , and every Fa interval nanoseconds thereafter until the timer source is canceled.
Any fire of the timer may be delayed by the system in order to improve power consumption and system performance. The upper limit to the allowable delay may be configured with the Fa leeway argument, the lower limit is under the control of the system.
For the initial timer fire at Fa start , the upper limit to the allowable delay is set to Fa leeway nanoseconds. For the subsequent timer fires at Fa start + N * Fa interval , the upper limit is MIN( Fa leeway , Fa interval / 2 )
The lower limit to the allowable delay may vary with process state such as visibility of application UI. If the specified timer source was created with a Fa mask of Vt DISPATCH_TIMER_STRICT , the system will make a best effort to strictly observe the provided Fa leeway value even if it is smaller than the current lower limit. Note that a minimal amount of delay is to be expected even if this flag is specified.
The Fa start argument also determines which clock will be used for the timer: If Fa start is Vt DISPATCH_TIME_NOW or was created with dispatch_time3, the timer is based on up time (which is obtained from Fn mach_absolute_time on Apple platforms). If Fa start was created with dispatch_walltime3, the timer is based on gettimeofday(3).
Vt DISPATCH_SOURCE_TYPE_VNODE
Sources of this type monitor the virtual filesystem nodes for state changes. The Fa handle is a file descriptor (int) referencing the node to monitor, and the Fa mask may be one or more of the following:
The data returned by Fn dispatch_source_get_data is a bitmask that indicates which of the events in the Fa mask were observed.
Vt DISPATCH_SOURCE_TYPE_WRITE
Sources of this type monitor file descriptors for available write buffer space. The Fa handle is the file descriptor (int) to monitor and the Fa mask is unused and should be zero.
Users of this source type are strongly encouraged to perform non-blocking I/O and handle any truncated reads or error conditions that may occur. See fcntl(2) for additional information about setting the Vt O_NONBLOCK flag on a file descriptor.