GEVENT doxygen
This commit is contained in:
parent
19d45eaeda
commit
cafb454745
104
src/gevent.c
104
src/gevent.c
@ -40,14 +40,14 @@
|
||||
#define GEVENT_ASSERT(x)
|
||||
#endif
|
||||
|
||||
// This mutex protects access to our tables
|
||||
/* This mutex protects access to our tables */
|
||||
static MUTEX_DECL(geventMutex);
|
||||
|
||||
// Our table of listener/source pairs
|
||||
/* Our table of listener/source pairs */
|
||||
static GSourceListener Assignments[MAX_SOURCE_LISTENERS];
|
||||
|
||||
// Loop through the assignment table deleting this listener/source pair.
|
||||
// Null is treated as a wildcard.
|
||||
/* Loop through the assignment table deleting this listener/source pair. */
|
||||
/* Null is treated as a wildcard. */
|
||||
static void deleteAssignments(GListener *pl, GSourceHandle gsh) {
|
||||
GSourceListener *psl;
|
||||
|
||||
@ -64,9 +64,12 @@ static void deleteAssignments(GListener *pl, GSourceHandle gsh) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Create a Listener.
|
||||
* If insufficient resources are available it will either assert or return NULL
|
||||
* depending on the value of GEVENT_ASSERT_NO_RESOURCE.
|
||||
/**
|
||||
* @brief Create a Listener
|
||||
* @details If insufficient resources are available it will either assert or return NULL
|
||||
* depending on the value of GEVENT_ASSERT_NO_RESOURCE.
|
||||
*
|
||||
* @param[in] pl A listener
|
||||
*/
|
||||
void geventListenerInit(GListener *pl) {
|
||||
chSemInit(&pl->waitqueue, 0); // Next wait'er will block
|
||||
@ -74,11 +77,18 @@ void geventListenerInit(GListener *pl) {
|
||||
pl->event.type = GEVENT_NULL; // Always safety
|
||||
}
|
||||
|
||||
/* Attach a source to a listener.
|
||||
* Flags are interpreted by the source when generating events for each listener.
|
||||
* If this source is already assigned to the listener it will update the flags.
|
||||
* If insufficient resources are available it will either assert or return FALSE
|
||||
* depending on the value of GEVENT_ASSERT_NO_RESOURCE.
|
||||
/**
|
||||
* @brief Attach a source to a listener
|
||||
* @details Flags are interpreted by the source when generating events for each listener.
|
||||
* If this source is already assigned to the listener it will update the flags.
|
||||
* If insufficient resources are available it will either assert or return FALSE
|
||||
* depending on the value of GEVENT_ASSERT_NO_RESOURCE.
|
||||
*
|
||||
* @param[in] pl The listener
|
||||
* @param[in] gsh The source which has to be attached to the listener
|
||||
* @param[in] flags The flags
|
||||
*
|
||||
* @return TRUE if succeeded, FALSE otherwise
|
||||
*/
|
||||
bool_t geventAttachSource(GListener *pl, GSourceHandle gsh, unsigned flags) {
|
||||
GSourceListener *psl, *pslfree;
|
||||
@ -119,9 +129,13 @@ bool_t geventAttachSource(GListener *pl, GSourceHandle gsh, unsigned flags) {
|
||||
return pslfree != 0;
|
||||
}
|
||||
|
||||
/* Detach a source from a listener
|
||||
* If gsh is NULL detach all sources from this listener and if there is still
|
||||
* a thread waiting for events on this listener, it is sent the exit event.
|
||||
/**
|
||||
* @brief Detach a source from a listener
|
||||
* @details If gsh is NULL detach all sources from this listener and if there is still
|
||||
* a thread waiting for events on this listener, it is sent the exit event.
|
||||
*
|
||||
* @param[in] pl The listener
|
||||
* @param[in] gsh The source
|
||||
*/
|
||||
void geventDetachSource(GListener *pl, GSourceHandle gsh) {
|
||||
if (pl && gsh) {
|
||||
@ -137,24 +151,34 @@ void geventDetachSource(GListener *pl, GSourceHandle gsh) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait for an event on a listener from an assigned source.
|
||||
* The type of the event should be checked (pevent->type) and then pevent should be typecast to the
|
||||
* actual event type if it needs to be processed.
|
||||
* timeout specifies the time to wait in system ticks.
|
||||
* TIME_INFINITE means no timeout - wait forever for an event.
|
||||
* TIME_IMMEDIATE means return immediately
|
||||
* Returns NULL on timeout.
|
||||
* Note: The GEvent buffer is staticly allocated within the GListener so the event does not
|
||||
/**
|
||||
* @brief Wait for an event on a listener from an assigned source.
|
||||
* @details The type of the event should be checked (pevent->type) and then pevent should
|
||||
* be typecast to the actual event type if it needs to be processed.
|
||||
* timeout specifies the time to wait in system ticks.
|
||||
* TIME_INFINITE means no timeout - wait forever for an event.
|
||||
* TIME_IMMEDIATE means return immediately
|
||||
* @note The GEvent buffer is staticly allocated within the GListener so the event does not
|
||||
* need to be dynamicly freed however it will get overwritten by the next call to
|
||||
* this routine.
|
||||
*
|
||||
* @param[in] pl The listener
|
||||
* @param[in] timeout The timeout
|
||||
*
|
||||
* @return NULL on timeout
|
||||
*/
|
||||
GEvent *geventEventWait(GListener *pl, systime_t timeout) {
|
||||
return chSemWaitTimeout(&pl->waitqueue, timeout) == RDY_OK ? &pl->event : 0;
|
||||
}
|
||||
|
||||
/* Called by a source with a possible event to get a listener record.
|
||||
* 'lastlr' should be NULL on the first call and thereafter the result of the previous call.
|
||||
* It will return NULL when there are no more listeners for this source.
|
||||
/**
|
||||
* @brief Called by a source with a possible event to get a listener record.
|
||||
* @details @p lastlr should be NULL on the first call and thereafter the result of the previous call.
|
||||
*
|
||||
* @param[in] gsh The source handler
|
||||
* @param[in] lastlr The source listener
|
||||
*
|
||||
* @return NULL when there are no more listeners for this source
|
||||
*/
|
||||
GSourceListener *geventGetSourceListener(GSourceHandle gsh, GSourceListener *lastlr) {
|
||||
GSourceListener *psl;
|
||||
@ -181,19 +205,27 @@ GSourceListener *geventGetSourceListener(GSourceHandle gsh, GSourceListener *las
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Get the event buffer from the GSourceListener.
|
||||
* Returns NULL if the listener is not currently listening.
|
||||
* A NULL return allows the source to record (perhaps in glr->scrflags) that the listener has missed events.
|
||||
* This can then be notified as part of the next event for the listener.
|
||||
* The buffer can only be accessed untill the next call to geventGetSourceListener or geventSendEvent
|
||||
/**
|
||||
* @brief Get the event buffer from the GSourceListener.
|
||||
* @details A NULL return allows the source to record (perhaps in glr->scrflags) that the listener
|
||||
* has missed events. This can then be notified as part of the next event for the listener.
|
||||
* The buffer can only be accessed untill the next call to geventGetSourceListener
|
||||
* or geventSendEvent
|
||||
*
|
||||
* @param[in] psl The source listener
|
||||
*
|
||||
* @return NULL if the listener is not currently listening.
|
||||
*/
|
||||
GEvent *geventGetEventBuffer(GSourceListener *psl) {
|
||||
// We already know we have the event lock
|
||||
return chSemGetCounterI(&psl->pListener->waitqueue) < 0 ? &psl->pListener->event : 0;
|
||||
}
|
||||
|
||||
/* Called by a source to indicate the listener's event buffer has been filled.
|
||||
* After calling this function the source must not reference in fields in the GSourceListener or the event buffer.
|
||||
/**
|
||||
* @brief Called by a source to indicate the listener's event buffer has been filled.
|
||||
* @details After calling this function the source must not reference in fields in the GSourceListener or the event buffer.
|
||||
*
|
||||
* @param[in] psl The source listener
|
||||
*/
|
||||
void geventSendEvent(GSourceListener *psl) {
|
||||
chMtxLock(&geventMutex);
|
||||
@ -203,7 +235,11 @@ void geventSendEvent(GSourceListener *psl) {
|
||||
chMtxUnlock();
|
||||
}
|
||||
|
||||
/* Detach any listener that has this source attached */
|
||||
/**
|
||||
* @brief Detach any listener that has this source attached
|
||||
*
|
||||
* @param[in] gsh The source handle
|
||||
*/
|
||||
void geventDetachSourceListeners(GSourceHandle gsh) {
|
||||
chMtxLock(&geventMutex);
|
||||
deleteAssignments(0, gsh);
|
||||
|
Loading…
Reference in New Issue
Block a user