2009-08-16 13:07:24 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2012-01-21 14:29:42 +00:00
|
|
|
2011,2012 Giovanni Di Sirio.
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2012-09-16 08:31:38 +00:00
|
|
|
/*
|
|
|
|
Concepts and parts of this file have been contributed by Scott (skute).
|
|
|
|
*/
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @file chevents.c
|
|
|
|
* @brief Events code.
|
|
|
|
*
|
2009-08-30 06:59:43 +00:00
|
|
|
* @addtogroup events
|
2010-03-16 19:36:21 +00:00
|
|
|
* @details Event Flags, Event Sources and Event Listeners.
|
|
|
|
* <h2>Operation mode</h2>
|
|
|
|
* Each thread has a mask of pending event flags inside its @p Thread
|
|
|
|
* structure.
|
2010-03-17 16:24:43 +00:00
|
|
|
* Operations defined for event flags:
|
2010-03-16 19:36:21 +00:00
|
|
|
* - <b>Wait</b>, the invoking thread goes to sleep until a certain
|
|
|
|
* AND/OR combination of event flags becomes pending.
|
|
|
|
* - <b>Clear</b>, a mask of event flags is cleared from the pending
|
|
|
|
* events mask, the cleared event flags mask is returned (only the
|
|
|
|
* flags that were actually pending and then cleared).
|
|
|
|
* - <b>Signal</b>, an event mask is directly ORed to the mask of the
|
|
|
|
* signaled thread.
|
|
|
|
* - <b>Broadcast</b>, each thread registered on an Event Source is
|
|
|
|
* signaled with the event flags specified in its Event Listener.
|
|
|
|
* - <b>Dispatch</b>, an events mask is scanned and for each bit set
|
|
|
|
* to one an associated handler function is invoked. Bit masks are
|
|
|
|
* scanned from bit zero upward.
|
|
|
|
* .
|
|
|
|
* An Event Source is a special object that can be "broadcasted" by
|
|
|
|
* a thread or an interrupt service routine. Broadcasting an Event
|
|
|
|
* Source has the effect that all the threads registered on the
|
|
|
|
* Event Source will be signaled with an events mask.<br>
|
|
|
|
* An unlimited number of Event Sources can exists in a system and
|
|
|
|
* each thread can be listening on an unlimited number of
|
2010-09-18 06:48:56 +00:00
|
|
|
* them.
|
|
|
|
* @pre In order to use the Events APIs the @p CH_USE_EVENTS option must be
|
2010-03-16 19:36:21 +00:00
|
|
|
* enabled in @p chconf.h.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @post Enabling events requires 1-4 (depending on the architecture)
|
|
|
|
* extra bytes in the @p Thread structure.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
2010-04-27 11:53:03 +00:00
|
|
|
|
2009-12-08 17:37:49 +00:00
|
|
|
#include "ch.h"
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2010-09-18 06:48:56 +00:00
|
|
|
#if CH_USE_EVENTS || defined(__DOXYGEN__)
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Registers an Event Listener on an Event Source.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @details Once a thread has registered as listener on an event source it
|
|
|
|
* will be notified of all events broadcasted there.
|
|
|
|
* @note Multiple Event Listeners can specify the same bits to be ORed to
|
|
|
|
* different threads.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] esp pointer to the @p EventSource structure
|
|
|
|
* @param[in] elp pointer to the @p EventListener structure
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask the mask of event flags to be ORed to the thread when
|
2010-02-06 10:55:53 +00:00
|
|
|
* the event source is broadcasted
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-06 10:55:53 +00:00
|
|
|
void chEvtRegisterMask(EventSource *esp, EventListener *elp, eventmask_t mask) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chDbgCheck((esp != NULL) && (elp != NULL), "chEvtRegisterMask");
|
|
|
|
|
|
|
|
chSysLock();
|
2012-09-16 08:31:38 +00:00
|
|
|
elp->el_next = esp->es_next;
|
|
|
|
esp->es_next = elp;
|
2009-08-16 13:07:24 +00:00
|
|
|
elp->el_listener = currp;
|
2012-09-16 08:31:38 +00:00
|
|
|
elp->el_mask = mask;
|
|
|
|
elp->el_flags = 0;
|
2009-08-16 13:07:24 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Unregisters an Event Listener from its Event Source.
|
|
|
|
* @note If the event listener is not registered on the specified event
|
|
|
|
* source then the function does nothing.
|
|
|
|
* @note For optimal performance it is better to perform the unregister
|
|
|
|
* operations in inverse order of the register operations (elements
|
|
|
|
* are found on top of the list).
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] esp pointer to the @p EventSource structure
|
|
|
|
* @param[in] elp pointer to the @p EventListener structure
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
|
|
|
void chEvtUnregister(EventSource *esp, EventListener *elp) {
|
|
|
|
EventListener *p;
|
|
|
|
|
|
|
|
chDbgCheck((esp != NULL) && (elp != NULL), "chEvtUnregister");
|
|
|
|
|
|
|
|
p = (EventListener *)esp;
|
|
|
|
chSysLock();
|
|
|
|
while (p->el_next != (EventListener *)esp) {
|
|
|
|
if (p->el_next == elp) {
|
|
|
|
p->el_next = elp->el_next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = p->el_next;
|
|
|
|
}
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Clears the pending events specified in the mask.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] mask the events to be cleared
|
|
|
|
* @return The pending events that were cleared.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-09-18 06:48:56 +00:00
|
|
|
eventmask_t chEvtClearFlags(eventmask_t mask) {
|
2009-08-16 13:07:24 +00:00
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
|
|
|
m = currp->p_epending & mask;
|
|
|
|
currp->p_epending &= ~mask;
|
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-09-18 06:48:56 +00:00
|
|
|
* @brief Adds (OR) a set of event flags on the current thread, this is
|
|
|
|
* @b much faster than using @p chEvtBroadcast() or @p chEvtSignal().
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask the event flags to be ORed
|
2010-02-06 10:55:53 +00:00
|
|
|
* @return The current pending events mask.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-09-18 06:48:56 +00:00
|
|
|
eventmask_t chEvtAddFlags(eventmask_t mask) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
|
|
|
mask = (currp->p_epending |= mask);
|
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-16 08:31:38 +00:00
|
|
|
* @brief Adds a set of event flags directly to specified @p Thread.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] tp the thread to be signaled
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask the event flags set to be ORed
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2012-09-16 08:31:38 +00:00
|
|
|
void chEvtSignal(Thread *tp, eventmask_t mask) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chDbgCheck(tp != NULL, "chEvtSignal");
|
|
|
|
|
|
|
|
chSysLock();
|
2012-09-16 08:31:38 +00:00
|
|
|
chEvtSignalI(tp, mask);
|
2010-03-01 16:41:24 +00:00
|
|
|
chSchRescheduleS();
|
2009-08-16 13:07:24 +00:00
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-16 08:31:38 +00:00
|
|
|
* @brief Adds a set of event flags directly to specified @p Thread.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @post This function does not reschedule so a call to a rescheduling
|
|
|
|
* function must be performed before unlocking the kernel. Note that
|
|
|
|
* interrupt handlers always reschedule on exit so an explicit
|
|
|
|
* reschedule must not be performed in ISRs.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] tp the thread to be signaled
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask the event flags set to be ORed
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2012-09-16 08:31:38 +00:00
|
|
|
void chEvtSignalI(Thread *tp, eventmask_t mask) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
2011-08-12 11:10:19 +00:00
|
|
|
chDbgCheckClassI();
|
2009-08-16 13:07:24 +00:00
|
|
|
chDbgCheck(tp != NULL, "chEvtSignalI");
|
|
|
|
|
|
|
|
tp->p_epending |= mask;
|
|
|
|
/* Test on the AND/OR conditions wait states.*/
|
2010-01-20 14:39:34 +00:00
|
|
|
if (((tp->p_state == THD_STATE_WTOREVT) &&
|
|
|
|
((tp->p_epending & tp->p_u.ewmask) != 0)) ||
|
|
|
|
((tp->p_state == THD_STATE_WTANDEVT) &&
|
|
|
|
((tp->p_epending & tp->p_u.ewmask) == tp->p_u.ewmask)))
|
|
|
|
chSchReadyI(tp)->p_u.rdymsg = RDY_OK;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Signals all the Event Listeners registered on the specified Event
|
|
|
|
* Source.
|
2011-02-21 18:00:06 +00:00
|
|
|
* @details This function variants ORs the specified event flags to all the
|
|
|
|
* threads registered on the @p EventSource in addition to the event
|
|
|
|
* flags specified by the threads themselves in the
|
|
|
|
* @p EventListener objects.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] esp pointer to the @p EventSource structure
|
2012-09-16 08:31:38 +00:00
|
|
|
* @param[in] flags the flags set to be added to the listener flags mask
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2012-09-16 08:31:38 +00:00
|
|
|
void chEvtBroadcastFlags(EventSource *esp, flagsmask_t flags) {
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysLock();
|
2012-09-16 08:31:38 +00:00
|
|
|
chEvtBroadcastFlagsI(esp, flags);
|
2009-08-16 13:07:24 +00:00
|
|
|
chSchRescheduleS();
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Signals all the Event Listeners registered on the specified Event
|
|
|
|
* Source.
|
2011-02-21 18:00:06 +00:00
|
|
|
* @details This function variants ORs the specified event flags to all the
|
|
|
|
* threads registered on the @p EventSource in addition to the event
|
|
|
|
* flags specified by the threads themselves in the
|
|
|
|
* @p EventListener objects.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @post This function does not reschedule so a call to a rescheduling
|
|
|
|
* function must be performed before unlocking the kernel. Note that
|
|
|
|
* interrupt handlers always reschedule on exit so an explicit
|
|
|
|
* reschedule must not be performed in ISRs.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] esp pointer to the @p EventSource structure
|
2012-09-16 08:31:38 +00:00
|
|
|
* @param[in] flags the flags set to be added to the listener flags mask
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @iclass
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2012-09-16 08:31:38 +00:00
|
|
|
void chEvtBroadcastFlagsI(EventSource *esp, flagsmask_t flags) {
|
2009-08-16 13:07:24 +00:00
|
|
|
EventListener *elp;
|
|
|
|
|
2011-08-12 11:10:19 +00:00
|
|
|
chDbgCheckClassI();
|
2011-02-21 18:00:06 +00:00
|
|
|
chDbgCheck(esp != NULL, "chEvtBroadcastMaskI");
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
elp = esp->es_next;
|
|
|
|
while (elp != (EventListener *)esp) {
|
2012-09-16 08:31:38 +00:00
|
|
|
elp->el_flags |= flags;
|
|
|
|
chEvtSignalI(elp->el_listener, elp->el_mask);
|
2009-08-16 13:07:24 +00:00
|
|
|
elp = elp->el_next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-16 08:31:38 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the flags associated to an @p EventListener.
|
|
|
|
* @details The flags are returned and the @p EventListener flags mask is
|
|
|
|
* cleared.
|
|
|
|
*
|
|
|
|
* @param[in] elp pointer to the @p EventListener structure
|
|
|
|
* @return The flags added to the listener by the associated
|
|
|
|
* event source.
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
flagsmask_t chEvtGetAndClearFlagsI(EventListener *elp) {
|
|
|
|
flagsmask_t flags;
|
|
|
|
|
|
|
|
flags = elp->el_flags;
|
|
|
|
elp->el_flags = 0;
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the flags associated to an @p EventListener.
|
|
|
|
* @details The flags are returned and the @p EventListener flags mask is
|
|
|
|
* cleared.
|
|
|
|
*
|
|
|
|
* @param[in] elp pointer to the @p EventListener structure
|
|
|
|
* @return The flags added to the listener by the associated
|
|
|
|
* event source.
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
flagsmask_t chEvtGetAndClearFlags(EventListener *elp) {
|
|
|
|
flagsmask_t flags;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
flags = elp->el_flags;
|
|
|
|
elp->el_flags = 0;
|
|
|
|
chSysUnlock();
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Invokes the event handlers associated to an event flags mask.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask mask of the event flags to be dispatched
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] handlers an array of @p evhandler_t. The array must have size
|
|
|
|
* equal to the number of bits in eventmask_t.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-24 10:41:11 +00:00
|
|
|
void chEvtDispatch(const evhandler_t *handlers, eventmask_t mask) {
|
2009-08-16 13:07:24 +00:00
|
|
|
eventid_t eid;
|
|
|
|
|
|
|
|
chDbgCheck(handlers != NULL, "chEvtDispatch");
|
|
|
|
|
|
|
|
eid = 0;
|
|
|
|
while (mask) {
|
|
|
|
if (mask & EVENT_MASK(eid)) {
|
|
|
|
chDbgAssert(handlers[eid] != NULL,
|
|
|
|
"chEvtDispatch(), #1",
|
|
|
|
"null handler");
|
|
|
|
mask &= ~EVENT_MASK(eid);
|
|
|
|
handlers[eid](eid);
|
|
|
|
}
|
|
|
|
eid++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CH_OPTIMIZE_SPEED || !CH_USE_EVENTS_TIMEOUT || defined(__DOXYGEN__)
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits for exactly one of the specified events.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details The function waits for one event among those specified in
|
2010-02-06 10:55:53 +00:00
|
|
|
* @p mask to become pending then the event is cleared and returned.
|
|
|
|
* @note One and only one event is served in the function, the one with the
|
|
|
|
* lowest event id. The function is meant to be invoked into a loop in
|
|
|
|
* order to serve all the pending events.<br>
|
|
|
|
* This means that Event Listeners with a lower event identifier have
|
|
|
|
* an higher priority.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
|
|
|
* for, @p ALL_EVENTS enables all the events
|
2010-02-06 10:55:53 +00:00
|
|
|
* @return The mask of the lowest id served and cleared event.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-06 10:55:53 +00:00
|
|
|
eventmask_t chEvtWaitOne(eventmask_t mask) {
|
2010-01-21 19:57:30 +00:00
|
|
|
Thread *ctp = currp;
|
2009-08-16 13:07:24 +00:00
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
2010-02-06 10:55:53 +00:00
|
|
|
if ((m = (ctp->p_epending & mask)) == 0) {
|
|
|
|
ctp->p_u.ewmask = mask;
|
2010-01-20 14:39:34 +00:00
|
|
|
chSchGoSleepS(THD_STATE_WTOREVT);
|
2010-02-06 10:55:53 +00:00
|
|
|
m = ctp->p_epending & mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
m &= -m;
|
2010-01-21 19:57:30 +00:00
|
|
|
ctp->p_epending &= ~m;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits for any of the specified events.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details The function waits for any event among those specified in
|
2010-02-06 10:55:53 +00:00
|
|
|
* @p mask to become pending then the events are cleared and returned.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
|
|
|
* for, @p ALL_EVENTS enables all the events
|
2010-02-06 10:55:53 +00:00
|
|
|
* @return The mask of the served and cleared events.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-06 10:55:53 +00:00
|
|
|
eventmask_t chEvtWaitAny(eventmask_t mask) {
|
2010-01-21 19:57:30 +00:00
|
|
|
Thread *ctp = currp;
|
2009-08-16 13:07:24 +00:00
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
2010-02-06 10:55:53 +00:00
|
|
|
if ((m = (ctp->p_epending & mask)) == 0) {
|
|
|
|
ctp->p_u.ewmask = mask;
|
2010-01-20 14:39:34 +00:00
|
|
|
chSchGoSleepS(THD_STATE_WTOREVT);
|
2010-02-06 10:55:53 +00:00
|
|
|
m = ctp->p_epending & mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
2010-01-21 19:57:30 +00:00
|
|
|
ctp->p_epending &= ~m;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits for all the specified events.
|
|
|
|
* @details The function waits for all the events specified in @p mask to
|
2009-08-16 13:07:24 +00:00
|
|
|
* become pending then the events are cleared and returned.
|
|
|
|
*
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
|
|
|
* for, @p ALL_EVENTS requires all the events
|
2010-02-06 10:55:53 +00:00
|
|
|
* @return The mask of the served and cleared events.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-06 10:55:53 +00:00
|
|
|
eventmask_t chEvtWaitAll(eventmask_t mask) {
|
2010-01-21 19:57:30 +00:00
|
|
|
Thread *ctp = currp;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
2010-02-06 10:55:53 +00:00
|
|
|
if ((ctp->p_epending & mask) != mask) {
|
|
|
|
ctp->p_u.ewmask = mask;
|
2010-01-20 14:39:34 +00:00
|
|
|
chSchGoSleepS(THD_STATE_WTANDEVT);
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
2010-02-06 10:55:53 +00:00
|
|
|
ctp->p_epending &= ~mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysUnlock();
|
2010-02-06 10:55:53 +00:00
|
|
|
return mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
#endif /* CH_OPTIMIZE_SPEED || !CH_USE_EVENTS_TIMEOUT */
|
|
|
|
|
2010-09-18 06:48:56 +00:00
|
|
|
#if CH_USE_EVENTS_TIMEOUT || defined(__DOXYGEN__)
|
2009-08-16 13:07:24 +00:00
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits for exactly one of the specified events.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details The function waits for one event among those specified in
|
2010-02-06 10:55:53 +00:00
|
|
|
* @p mask to become pending then the event is cleared and returned.
|
|
|
|
* @note One and only one event is served in the function, the one with the
|
|
|
|
* lowest event id. The function is meant to be invoked into a loop in
|
|
|
|
* order to serve all the pending events.<br>
|
|
|
|
* This means that Event Listeners with a lower event identifier have
|
|
|
|
* an higher priority.
|
2009-08-16 13:07:24 +00:00
|
|
|
*
|
2012-03-26 09:13:37 +00:00
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
2010-09-18 06:48:56 +00:00
|
|
|
* for, @p ALL_EVENTS enables all the events
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] time the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
|
|
|
* @return The mask of the lowest id served and cleared event.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @retval 0 if the operation has timed out.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-06 10:55:53 +00:00
|
|
|
eventmask_t chEvtWaitOneTimeout(eventmask_t mask, systime_t time) {
|
2010-01-21 19:57:30 +00:00
|
|
|
Thread *ctp = currp;
|
2009-08-16 13:07:24 +00:00
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
2010-02-06 10:55:53 +00:00
|
|
|
if ((m = (ctp->p_epending & mask)) == 0) {
|
2010-11-20 10:46:51 +00:00
|
|
|
if (TIME_IMMEDIATE == time) {
|
|
|
|
chSysUnlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
return (eventmask_t)0;
|
2010-11-20 10:46:51 +00:00
|
|
|
}
|
2010-02-06 10:55:53 +00:00
|
|
|
ctp->p_u.ewmask = mask;
|
2010-11-20 10:46:51 +00:00
|
|
|
if (chSchGoSleepTimeoutS(THD_STATE_WTOREVT, time) < RDY_OK) {
|
|
|
|
chSysUnlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
return (eventmask_t)0;
|
2010-11-20 10:46:51 +00:00
|
|
|
}
|
2010-02-06 10:55:53 +00:00
|
|
|
m = ctp->p_epending & mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
m &= -m;
|
2010-01-21 19:57:30 +00:00
|
|
|
ctp->p_epending &= ~m;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits for any of the specified events.
|
2009-08-16 13:07:24 +00:00
|
|
|
* @details The function waits for any event among those specified in
|
2010-02-06 10:55:53 +00:00
|
|
|
* @p mask to become pending then the events are cleared and
|
2009-08-16 13:07:24 +00:00
|
|
|
* returned.
|
|
|
|
*
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
|
|
|
* for, @p ALL_EVENTS enables all the events
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] time the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
|
|
|
* @return The mask of the served and cleared events.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @retval 0 if the operation has timed out.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-06 10:55:53 +00:00
|
|
|
eventmask_t chEvtWaitAnyTimeout(eventmask_t mask, systime_t time) {
|
2010-01-21 19:57:30 +00:00
|
|
|
Thread *ctp = currp;
|
2009-08-16 13:07:24 +00:00
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
2010-02-06 10:55:53 +00:00
|
|
|
if ((m = (ctp->p_epending & mask)) == 0) {
|
2010-11-20 10:46:51 +00:00
|
|
|
if (TIME_IMMEDIATE == time) {
|
|
|
|
chSysUnlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
return (eventmask_t)0;
|
2010-11-20 10:46:51 +00:00
|
|
|
}
|
2010-02-06 10:55:53 +00:00
|
|
|
ctp->p_u.ewmask = mask;
|
2010-11-20 10:46:51 +00:00
|
|
|
if (chSchGoSleepTimeoutS(THD_STATE_WTOREVT, time) < RDY_OK) {
|
|
|
|
chSysUnlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
return (eventmask_t)0;
|
2010-11-20 10:46:51 +00:00
|
|
|
}
|
2010-02-06 10:55:53 +00:00
|
|
|
m = ctp->p_epending & mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
2010-01-21 19:57:30 +00:00
|
|
|
ctp->p_epending &= ~m;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysUnlock();
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-02-06 10:55:53 +00:00
|
|
|
* @brief Waits for all the specified events.
|
|
|
|
* @details The function waits for all the events specified in @p mask to
|
2009-08-16 13:07:24 +00:00
|
|
|
* become pending then the events are cleared and returned.
|
|
|
|
*
|
2010-09-18 06:48:56 +00:00
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
|
|
|
* for, @p ALL_EVENTS requires all the events
|
2010-02-06 10:55:53 +00:00
|
|
|
* @param[in] time the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_IMMEDIATE immediate timeout.
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
|
|
|
* @return The mask of the served and cleared events.
|
2010-09-18 06:48:56 +00:00
|
|
|
* @retval 0 if the operation has timed out.
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2009-08-16 13:07:24 +00:00
|
|
|
*/
|
2010-02-06 10:55:53 +00:00
|
|
|
eventmask_t chEvtWaitAllTimeout(eventmask_t mask, systime_t time) {
|
2010-01-21 19:57:30 +00:00
|
|
|
Thread *ctp = currp;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
|
2010-02-06 10:55:53 +00:00
|
|
|
if ((ctp->p_epending & mask) != mask) {
|
2010-11-20 10:46:51 +00:00
|
|
|
if (TIME_IMMEDIATE == time) {
|
|
|
|
chSysUnlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
return (eventmask_t)0;
|
2010-11-20 10:46:51 +00:00
|
|
|
}
|
2010-02-06 10:55:53 +00:00
|
|
|
ctp->p_u.ewmask = mask;
|
2010-11-20 10:46:51 +00:00
|
|
|
if (chSchGoSleepTimeoutS(THD_STATE_WTANDEVT, time) < RDY_OK) {
|
|
|
|
chSysUnlock();
|
2009-08-16 13:07:24 +00:00
|
|
|
return (eventmask_t)0;
|
2010-11-20 10:46:51 +00:00
|
|
|
}
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
2010-02-06 10:55:53 +00:00
|
|
|
ctp->p_epending &= ~mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
|
|
|
|
chSysUnlock();
|
2010-02-06 10:55:53 +00:00
|
|
|
return mask;
|
2009-08-16 13:07:24 +00:00
|
|
|
}
|
|
|
|
#endif /* CH_USE_EVENTS_TIMEOUT */
|
|
|
|
|
|
|
|
#endif /* CH_USE_EVENTS */
|
|
|
|
|
|
|
|
/** @} */
|