| Derived from | |
| Framework | CoreFoundation/CoreFoundation.h |
| Companion guide | |
| Declared in | CFRunLoop.h |
A CFRunLoopSource object is an abstraction of an input source that can be put into a run loop. Input sources typically generate asynchronous events, such as messages arriving on a network port or actions performed by the user.
An input source type normally defines an API for creating and operating on objects of the type, as if it were a separate entity from the run loop, then provides a function to create a CFRunLoopSource for an object. The run loop source can then be registered with the run loop and act as an intermediary between the run loop and the actual input source type object. Examples of input sources include CFMachPort, CFMessagePort, and CFSocket.
There are two categories of sources. Version 0 sources, so named because the version field of their context structure is 0, are managed manually by the application. When a source is ready to fire, some part of the application, perhaps code on a separate thread waiting for an event, must call CFRunLoopSourceSignal to tell the run loop that the source is ready to fire. The run loop source for CFSocket is currently implemented as a version 0 source.
Version 1 sources are managed by the run loop and kernel. These sources use Mach ports to signal when the sources are ready to fire. A source is automatically signaled by the kernel when a message arrives on the source’s Mach port. The contents of the message are given to the source to process when the source is fired. The run loop sources for CFMachPort and CFMessagePort are currently implemented as version 1 sources.
When creating your own custom run loop source, you can choose which version works best for you.
A run loop source can be registered in multiple run loops and run loop modes at the same time. When the source is signaled, whichever run loop that happens to detect the signal first will fire the source. Adding a source to multiple threads’ run loops can be used to manage a pool of “worker” threads that is processing discrete sets of data, such as client-server messages over a network or entries in a job queue filled by a “manager” thread. As messages arrive or jobs get added to the queue, the source gets signaled and a random thread receives and processes the request.
Creates a CFRunLoopSource object.
CFRunLoopSourceRef CFRunLoopSourceCreate ( CFAllocatorRef allocator, CFIndex order, CFRunLoopSourceContext *context );
The allocator to use to allocate memory for the new object. Pass NULL or kCFAllocatorDefault to use the current default allocator.
A priority index indicating the order in which run loop sources are processed. When multiple run loop sources are firing in a single pass through the run loop, the sources are processed in increasing order of this parameter. If the run loop is set to process only one source per loop, only the highest priority source, the one with the lowest order value, is processed. This value is ignored for version 1 sources. Pass 0 unless there is a reason to do otherwise.
A structure holding contextual information for the run loop source. The function copies the information out of the structure, so the memory pointed to by context does not need to persist beyond the function call.
The new CFRunLoopSource object. You are responsible for releasing this object.
The run loop source is not automatically added to a run loop. Ownership follows the Create Rule.
CFRunLoop.h
Returns the context information for a CFRunLoopSource object.
void CFRunLoopSourceGetContext ( CFRunLoopSourceRef source, CFRunLoopSourceContext *context );
The run loop source to examine.
A pointer to the structure into which the context information for source is to be copied. The information being returned is the same information passed to CFRunLoopSourceCreate when creating source.
Run loop sources come in two versions with different-sized context structures. context must point to the correct version of the structure for source. Before calling this function, you need to initialize the version member of context with the version number (either 0 or 1) of source.
CFRunLoop.h
Returns the ordering parameter for a CFRunLoopSource object.
CFIndex CFRunLoopSourceGetOrder ( CFRunLoopSourceRef source );
The run loop source to examine.
The ordering parameter for source, which the run loop uses (for version 0 sources only) to determine the order in which sources are processed when multiple sources are firing.
CFRunLoop.h
Returns the type identifier of the CFRunLoopSource opaque type.
CFTypeID CFRunLoopSourceGetTypeID ( void );
The type identifier for the CFRunLoopSource opaque type.
CFRunLoop.h
Invalidates a CFRunLoopSource object, stopping it from ever firing again.
void CFRunLoopSourceInvalidate ( CFRunLoopSourceRef source );
The run loop source to invalidate.
Once invalidated, source will never fire and call its perform callback function again. This function automatically removes source from all the run loop modes in which it was registered. If source is a version 0 source, this function calls its cancel callback function as it is removed from each run loop mode. The memory for source is not deallocated unless the run loop held the only reference to source.
CFRunLoop.h
Returns a Boolean value that indicates whether a CFRunLoopSource object is valid and able to fire.
Boolean CFRunLoopSourceIsValid ( CFRunLoopSourceRef source );
The run loop source to examine.
true if source is valid, otherwise false.
CFRunLoop.h
Signals a CFRunLoopSource object, marking it as ready to fire.
void CFRunLoopSourceSignal ( CFRunLoopSourceRef source );
The run loop source to signal.
This function has no effect on version 1 sources, which are automatically handled when Mach messages arrive for them. After signaling a version 0 source, you need to call CFRunLoopWakeUp on one of the run loops in which the source is registered to get the source handled immediately.
CFRunLoop.hCallback invoked when a version 0 CFRunLoopSource object is removed from a run loop mode.
typedef void (*CFRunLoopCancelCallBack) ( void *info, CFRunLoopRef rl, CFStringRef mode );
If you name your function MyCallBack, you would declare it like this:
void MyCallBack ( void *info, CFRunLoopRef rl, CFStringRef mode );
The info member of the CFRunLoopSourceContext structure that was used when creating the run loop source.
The run loop from which the run loop source is being removed.
The run loop mode from which the run loop source is being removed.
You specify this callback in the CFRunLoopSourceContext structure when creating the run loop source.
Callback invoked to test two CFRunLoopSource objects for equality.
typedef Boolean (*CFRunLoopEqualCallBack) ( const void *info1, const void *info2 );
If you name your function MyCallBack, you would declare it like this:
Boolean MyCallBack ( const void *info1, const void *info2 );
The info member of the CFRunLoopSourceContext or CFRunLoopSourceContext1 structure that was used when creating the first run loop source to test.
The info member of the CFRunLoopSourceContext or CFRunLoopSourceContext1 structure that was used when creating the second run loop source to test.
true if info1 and info2 should be considered equal; otherwise false.
You specify this callback in the CFRunLoopSourceContext or CFRunLoopSourceContext1 structure when creating the run loop source.
Callback invoked to obtain the native Mach port represented by a version 1 CFRunLoopSource object.
typedef mach_port_t (*CFRunLoopGetPortCallBack) ( void *info );
If you name your function MyCallBack, you would declare it like this:
mach_port_t MyCallBack ( void *info );
The info member of the CFRunLoopSourceContext1 structure that was used when creating the run loop source.
The native Mach port for the run loop source.
This callback is called whenever the run loop needs a source’s Mach port, which can happen in each iteration of the run loop’s loop. Because of the frequency with which the run loop may call this callback, make the function as efficient as possible.
A version 1 run loop source must have a one-to-one relationship between itself and its Mach port. Each source must have only one Mach port associated with it and each Mach port must represent only one source.
You specify this callback in the CFRunLoopSourceContext1 structure when creating the run loop source.
Callback invoked to compute a hash code for the info pointer of a CFRunLoopSource object.
typedef CFHashCode (*CFRunLoopHashCallBack) ( const void *info );
If you name your function MyCallBack, you would declare it like this:
CFHashCode MyCallBack ( const void *info );
The info member of the CFRunLoopSourceContext or CFRunLoopSourceContext1 structure that was used when creating the run loop source.
A hash code value for info.
If a hash callback is not provided for a source, the info pointer is used.
You specify this callback in the CFRunLoopSourceContext or CFRunLoopSourceContext1 structure when creating the run loop source.
Callback invoked to process and optionally reply to a message received on a version 1 CFRunLoopSource object (Mach port-based sources).
typedef void *(*CFRunLoopMachPerformCallBack) ( void *msg, CFIndex size, CFAllocatorRef allocator, void *info );
If you name your function MyCallBack, you would declare it like this:
void *MyCallBack ( void *msg, CFIndex size, CFAllocatorRef allocator, void *info );
The Mach message received on the Mach port. The pointer is to a mach_msg_header_t structure. A version 0 format trailer (mach_msg_format_0_trailer_t) is at the end of the Mach message.
Size of the Mach message in msg, excluding the message trailer.
The allocator object that should be used to allocate a reply message.
The info member of the CFRunLoopSourceContext1 structure that was used when creating the run loop source.
An optional Mach message to be sent in response to the received message. The message must be allocated using allocator. Return NULL if you want an empty reply returned to the sender.
You only need to provide this callback if you create your own version 1 run loop source. CFMachPort and CFMessagePort run loop sources already implement this callback to forward the received message to the CFMachPort’s or CFMessagePort’s own callback function, which you do need to implement.
You specify this callback in the CFRunLoopSourceContext1 structure when creating the run loop source.
Callback invoked when a message is received on a version 0 CFRunLoopSource object.
typedef void (*CFRunLoopPerformCallBack) ( void *info );
If you name your function MyCallBack, you would declare it like this:
void MyCallBack ( void *info );
The info member of the CFRunLoopSourceContext structure that was used when creating the run loop source.
You only need to provide this callback if you create your own version 0 run loop source. CFSocket run loop sources already implement this callback to forward the received message to the CFSocket’s own callback function, which you do need to implement.
You specify this callback in the CFRunLoopSourceContext structure when creating the run loop source.
Callback invoked when a version 0 CFRunLoopSource object is added to a run loop mode.
typedef void (*CFRunLoopScheduleCallBack) ( void *info, CFRunLoopRef rl, CFStringRef mode );
If you name your function MyCallBack, you would declare it like this:
void MyCallBack ( void *info, CFRunLoopRef rl, CFStringRef mode );
The info member of the CFRunLoopSourceContext structure that was used when creating the run loop source.
The run loop in which the source is being scheduled.
The run loop mode in which the source is being scheduled.
You specify this callback in the CFRunLoopSourceContext structure when creating the run loop source.
A structure that contains program-defined data and callbacks with which you can configure a version 0 CFRunLoopSource’s behavior.
struct CFRunLoopSourceContext {
CFIndex version;
void *info;
CFAllocatorRetainCallBack retain;
CFAllocatorReleaseCallBack release;
CFAllocatorCopyDescriptionCallBack copyDescription;
CFRunLoopEqualCallBack equal;
CFRunLoopHashCallBack hash;
CFRunLoopScheduleCallBack schedule;
CFRunLoopCancelCallBack cancel;
CFRunLoopPerformCallBack perform;
};
typedef struct CFRunLoopSourceContext CFRunLoopSourceContext;
versionVersion number of the structure. Must be 0.
infoAn arbitrary pointer to program-defined data, which can be associated with the CFRunLoopSource at creation time. This pointer is passed to all the callbacks defined in the context.
retainA retain callback for your program-defined info pointer. Can be NULL.
releaseA release callback for your program-defined info pointer. Can be NULL.
copyDescriptionA copy description callback for your program-defined info pointer. Can be NULL.
equalAn equality test callback for your program-defined info pointer. Can be NULL.
hashA hash calculation callback for your program-defined info pointer. Can be NULL.
scheduleA scheduling callback for the run loop source. This callback is called when the source is added to a run loop mode. Can be NULL.
cancelA cancel callback for the run loop source. This callback is called when the source is removed from a run loop mode. Can be NULL.
performA perform callback for the run loop source. This callback is called when the source has fired.
CFRunLoop.h
A structure that contains program-defined data and callbacks with which you can configure a version 1 CFRunLoopSource’s behavior.
struct CFRunLoopSourceContext1 {
CFIndex version;
void *info;
CFAllocatorRetainCallBack retain;
CFAllocatorReleaseCallBack release;
CFAllocatorCopyDescriptionCallBack copyDescription;
CFRunLoopEqualCallBack equal;
CFRunLoopHashCallBack hash;
CFRunLoopGetPortCallBack getPort;
CFRunLoopMachPerformCallBack perform;
};
typedef struct CFRunLoopSourceContext1 CFRunLoopSourceContext1;
versionVersion number of the structure. Must be 1.
infoAn arbitrary pointer to program-defined data, which can be associated with the run loop source at creation time. This pointer is passed to all the callbacks defined in the context.
retainA retain callback for your program-defined info pointer. Can be NULL.
releaseA release callback for your program-defined info pointer. Can be NULL.
copyDescriptionA copy description callback for your program-defined info pointer. Can be NULL.
equalAn equality test callback for your program-defined info pointer. Can be NULL.
hashA hash calculation callback for your program-defined info pointer. Can be NULL.
getPortA callback to retrieve the native Mach port represented by the source. This callback is called when the source is either added to or removed from a run loop mode.
performA perform callback for the run loop source. This callback is called when the source has fired.
CFRunLoop.hA reference to a run loop source object.
typedef struct __CFRunLoopSource *CFRunLoopSourceRef;
CFRunLoop.h
© 2003, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-02-07)