2013-09-01 08:19:24 +00:00
|
|
|
/*
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio.
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
This file is part of ChibiOS.
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS is free software; you can redistribute it and/or modify
|
2013-09-01 08:19:24 +00:00
|
|
|
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.
|
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS is distributed in the hope that it will be useful,
|
2013-09-01 08:19:24 +00:00
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file nil.c
|
|
|
|
* @brief Nil RTOS main source file.
|
|
|
|
*
|
2014-12-08 19:18:25 +00:00
|
|
|
* @addtogroup NIL_KERNEL
|
2013-09-01 08:19:24 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nil.h"
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief System data structures.
|
|
|
|
*/
|
|
|
|
nil_system_t nil;
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initializes the kernel.
|
|
|
|
* @details Initializes the kernel structures, the current instructions flow
|
|
|
|
* becomes the idle thread upon return. The idle thread must not
|
|
|
|
* invoke any kernel primitive able to change state to not runnable.
|
2013-09-05 14:54:24 +00:00
|
|
|
* @note This function assumes that the @p nil global variable has been
|
|
|
|
* zeroed by the runtime environment. If this is not the case then
|
|
|
|
* make sure to clear it before calling this function.
|
2013-09-01 08:19:24 +00:00
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSysInit(void) {
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *tp;
|
2013-09-01 08:19:24 +00:00
|
|
|
const thread_config_t *tcp;
|
|
|
|
|
|
|
|
/* Port layer initialization.*/
|
|
|
|
port_init();
|
|
|
|
|
2014-07-25 08:36:08 +00:00
|
|
|
/* System initialization hook.*/
|
|
|
|
NIL_CFG_SYSTEM_INIT_HOOK();
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/* Iterates through the list of defined threads.*/
|
2013-09-05 14:54:24 +00:00
|
|
|
tp = &nil.threads[0];
|
|
|
|
tcp = nil_thd_configs;
|
|
|
|
while (tp < &nil.threads[NIL_CFG_NUM_THREADS]) {
|
|
|
|
#if NIL_CFG_ENABLE_STACK_CHECK
|
|
|
|
tp->stklim = (stkalign_t *)tcp->wbase;
|
2013-09-05 12:33:42 +00:00
|
|
|
#endif
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/* Port dependent thread initialization.*/
|
2013-09-05 14:54:24 +00:00
|
|
|
PORT_SETUP_CONTEXT(tp, tcp->wend, tcp->funcp, tcp->arg);
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/* Initialization hook.*/
|
2013-09-05 12:33:42 +00:00
|
|
|
NIL_CFG_THREAD_EXT_INIT_HOOK(tp);
|
2013-09-05 14:54:24 +00:00
|
|
|
|
2015-03-10 15:53:36 +00:00
|
|
|
tp++;
|
|
|
|
tcp++;
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-05 14:54:24 +00:00
|
|
|
#if NIL_CFG_ENABLE_STACK_CHECK
|
|
|
|
/* The idle thread is a special case because its stack is set up by the
|
|
|
|
runtime environment.*/
|
|
|
|
tp->stklim = THD_IDLE_BASE;
|
|
|
|
#endif
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/* Runs the highest priority thread, the current one becomes the null
|
|
|
|
thread.*/
|
|
|
|
nil.current = nil.next = nil.threads;
|
2013-09-05 14:54:24 +00:00
|
|
|
port_switch(nil.current, tp);
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/* Interrupts enabled for the idle thread.*/
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysEnable();
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 13:21:10 +00:00
|
|
|
/**
|
|
|
|
* @brief Halts the system.
|
|
|
|
* @details This function is invoked by the operating system when an
|
|
|
|
* unrecoverable error is detected, for example because a programming
|
|
|
|
* error in the application code that triggers an assertion while
|
|
|
|
* in debug mode.
|
|
|
|
* @note Can be invoked from any system state.
|
|
|
|
*
|
2014-12-08 18:01:38 +00:00
|
|
|
* @param[in] reason pointer to an error string
|
|
|
|
*
|
2013-09-03 13:21:10 +00:00
|
|
|
* @special
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSysHalt(const char *reason) {
|
2013-09-03 13:21:10 +00:00
|
|
|
|
|
|
|
port_disable();
|
|
|
|
|
|
|
|
#if NIL_DBG_ENABLED
|
|
|
|
nil.dbg_panic_msg = reason;
|
|
|
|
#else
|
|
|
|
(void)reason;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NIL_CFG_SYSTEM_HALT_HOOK(reason);
|
|
|
|
|
|
|
|
/* Harmless infinite loop.*/
|
2015-03-10 15:53:36 +00:00
|
|
|
while (true) {
|
|
|
|
}
|
2013-09-03 13:21:10 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Time management handler.
|
|
|
|
* @note This handler has to be invoked by a periodic ISR in order to
|
|
|
|
* reschedule the waiting threads.
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSysTimerHandlerI(void) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-11 12:00:01 +00:00
|
|
|
#if NIL_CFG_ST_TIMEDELTA == 0
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *tp = &nil.threads[0];
|
2013-09-01 08:19:24 +00:00
|
|
|
nil.systime++;
|
|
|
|
do {
|
|
|
|
/* Is the thread in a wait state with timeout?.*/
|
2015-03-10 15:53:36 +00:00
|
|
|
if (tp->timeout > (systime_t)0) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
chDbgAssert(!NIL_THD_IS_READY(tp), "is ready");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/* Did the timer reach zero?*/
|
2015-03-10 15:53:36 +00:00
|
|
|
if (--tp->timeout == (systime_t)0) {
|
2013-09-01 08:19:24 +00:00
|
|
|
/* Timeout on semaphores requires a special handling because the
|
|
|
|
semaphore counter must be incremented.*/
|
2015-03-10 15:53:36 +00:00
|
|
|
/*lint -save -e9013 [15.7] There is no else because it is not needed.*/
|
|
|
|
if (NIL_THD_IS_WTSEM(tp)) {
|
2013-09-05 12:33:42 +00:00
|
|
|
tp->u1.semp->cnt++;
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
|
|
|
else if (NIL_THD_IS_SUSP(tp)) {
|
2013-09-13 12:58:50 +00:00
|
|
|
*tp->u1.trp = NULL;
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
|
|
|
/*lint -restore*/
|
|
|
|
(void) chSchReadyI(tp, MSG_TIMEOUT);
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Lock released in order to give a preemption chance on those
|
|
|
|
architectures supporting IRQ preemption.*/
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysUnlockFromISR();
|
2013-09-05 12:33:42 +00:00
|
|
|
tp++;
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysLockFromISR();
|
2013-09-05 12:33:42 +00:00
|
|
|
} while (tp < &nil.threads[NIL_CFG_NUM_THREADS]);
|
2013-09-01 08:19:24 +00:00
|
|
|
#else
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *tp = &nil.threads[0];
|
2015-03-10 15:53:36 +00:00
|
|
|
systime_t next = (systime_t)0;
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-04 12:41:44 +00:00
|
|
|
chDbgAssert(nil.nexttime == port_timer_get_alarm(), "time mismatch");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
/* Is the thread in a wait state with timeout?.*/
|
2015-03-10 15:53:36 +00:00
|
|
|
if (tp->timeout > (systime_t)0) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
chDbgAssert(!NIL_THD_IS_READY(tp), "is ready");
|
2015-03-12 09:28:03 +00:00
|
|
|
chDbgAssert(tp->timeout >= (nil.nexttime - nil.lasttime), "skipped one");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
tp->timeout -= nil.nexttime - nil.lasttime;
|
2015-03-10 15:53:36 +00:00
|
|
|
if (tp->timeout == (systime_t)0) {
|
2013-09-01 08:19:24 +00:00
|
|
|
/* Timeout on semaphores requires a special handling because the
|
|
|
|
semaphore counter must be incremented.*/
|
2015-03-10 15:53:36 +00:00
|
|
|
/*lint -save -e9013 [15.7] There is no else because it is not needed.*/
|
|
|
|
if (NIL_THD_IS_WTSEM(tp)) {
|
2013-09-05 12:33:42 +00:00
|
|
|
tp->u1.semp->cnt++;
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
|
|
|
else if (NIL_THD_IS_SUSP(tp)) {
|
2013-09-13 12:59:53 +00:00
|
|
|
*tp->u1.trp = NULL;
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
|
|
|
/*lint -restore*/
|
|
|
|
(void) chSchReadyI(tp, MSG_TIMEOUT);
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-03-10 15:53:36 +00:00
|
|
|
if (tp->timeout <= (systime_t)(next - (systime_t)1)) {
|
2013-09-05 12:33:42 +00:00
|
|
|
next = tp->timeout;
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Lock released in order to give a preemption chance on those
|
|
|
|
architectures supporting IRQ preemption.*/
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysUnlockFromISR();
|
2013-09-05 12:33:42 +00:00
|
|
|
tp++;
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysLockFromISR();
|
2013-09-05 12:33:42 +00:00
|
|
|
} while (tp < &nil.threads[NIL_CFG_NUM_THREADS]);
|
2013-09-01 08:19:24 +00:00
|
|
|
nil.lasttime = nil.nexttime;
|
2015-03-10 15:53:36 +00:00
|
|
|
if (next > (systime_t)0) {
|
2013-09-01 08:19:24 +00:00
|
|
|
nil.nexttime += next;
|
|
|
|
port_timer_set_alarm(nil.nexttime);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* No tick event needed.*/
|
|
|
|
port_timer_stop_alarm();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:05:36 +00:00
|
|
|
/**
|
2014-12-10 14:25:41 +00:00
|
|
|
* @brief Unconditionally enters the kernel lock state.
|
2014-08-12 16:05:36 +00:00
|
|
|
* @note Can be called without previous knowledge of the current lock state.
|
|
|
|
* The final state is "s-locked".
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2014-12-10 14:25:41 +00:00
|
|
|
void chSysUnconditionalLock(void) {
|
2014-08-12 16:05:36 +00:00
|
|
|
|
2015-03-10 15:53:36 +00:00
|
|
|
if (port_irq_enabled(port_get_irq_status())) {
|
2014-08-12 16:05:36 +00:00
|
|
|
chSysLock();
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
2014-08-12 16:05:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-10 14:25:41 +00:00
|
|
|
* @brief Unconditionally leaves the kernel lock state.
|
2014-08-12 16:05:36 +00:00
|
|
|
* @note Can be called without previous knowledge of the current lock state.
|
|
|
|
* The final state is "normal".
|
|
|
|
*
|
|
|
|
* @special
|
|
|
|
*/
|
2014-12-10 14:25:41 +00:00
|
|
|
void chSysUnconditionalUnlock(void) {
|
2014-08-12 16:05:36 +00:00
|
|
|
|
2015-03-10 15:53:36 +00:00
|
|
|
if (!port_irq_enabled(port_get_irq_status())) {
|
2014-08-12 16:05:36 +00:00
|
|
|
chSysUnlock();
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
2014-08-12 16:05:36 +00:00
|
|
|
}
|
|
|
|
|
2013-09-04 10:26:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns the execution status and enters a critical zone.
|
|
|
|
* @details This functions enters into a critical zone and can be called
|
|
|
|
* from any context. Because its flexibility it is less efficient
|
|
|
|
* than @p chSysLock() which is preferable when the calling context
|
|
|
|
* is known.
|
|
|
|
* @post The system is in a critical zone.
|
|
|
|
*
|
|
|
|
* @return The previous system status, the encoding of this
|
|
|
|
* status word is architecture-dependent and opaque.
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
syssts_t chSysGetStatusAndLockX(void) {
|
|
|
|
|
|
|
|
syssts_t sts = port_get_irq_status();
|
|
|
|
if (port_irq_enabled(sts)) {
|
2015-03-10 15:53:36 +00:00
|
|
|
if (port_is_isr_context()) {
|
2013-09-04 10:26:42 +00:00
|
|
|
chSysLockFromISR();
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-09-04 10:26:42 +00:00
|
|
|
chSysLock();
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
2013-09-04 10:26:42 +00:00
|
|
|
}
|
|
|
|
return sts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Restores the specified execution status and leaves a critical zone.
|
|
|
|
* @note A call to @p chSchRescheduleS() is automatically performed
|
|
|
|
* if exiting the critical zone and if not in ISR context.
|
|
|
|
*
|
|
|
|
* @param[in] sts the system status to be restored.
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
void chSysRestoreStatusX(syssts_t sts) {
|
|
|
|
|
|
|
|
if (port_irq_enabled(sts)) {
|
2015-03-10 15:53:36 +00:00
|
|
|
if (port_is_isr_context()) {
|
2013-09-04 10:26:42 +00:00
|
|
|
chSysUnlockFromISR();
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
2013-09-04 10:26:42 +00:00
|
|
|
else {
|
|
|
|
chSchRescheduleS();
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-15 08:56:56 +00:00
|
|
|
#if (PORT_SUPPORTS_RT == TRUE) || defined(__DOXYGEN__)
|
|
|
|
/**
|
|
|
|
* @brief Realtime window test.
|
|
|
|
* @details This function verifies if the current realtime counter value
|
|
|
|
* lies within the specified range or not. The test takes care
|
|
|
|
* of the realtime counter wrapping to zero on overflow.
|
|
|
|
* @note When start==end then the function returns always true because the
|
|
|
|
* whole time range is specified.
|
|
|
|
* @note This function is only available if the port layer supports the
|
|
|
|
* option @p PORT_SUPPORTS_RT.
|
|
|
|
*
|
|
|
|
* @param[in] cnt the counter value to be tested
|
|
|
|
* @param[in] start the start of the time window (inclusive)
|
|
|
|
* @param[in] end the end of the time window (non inclusive)
|
|
|
|
* @retval true current time within the specified time window.
|
|
|
|
* @retval false current time not within the specified time window.
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
bool chSysIsCounterWithinX(rtcnt_t cnt, rtcnt_t start, rtcnt_t end) {
|
|
|
|
|
|
|
|
return (bool)((cnt - start) < (end - start));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Polled delay.
|
|
|
|
* @note The real delay is always few cycles in excess of the specified
|
|
|
|
* value.
|
|
|
|
* @note This function is only available if the port layer supports the
|
|
|
|
* option @p PORT_SUPPORTS_RT.
|
|
|
|
*
|
|
|
|
* @param[in] cycles number of cycles
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
void chSysPolledDelayX(rtcnt_t cycles) {
|
|
|
|
rtcnt_t start = chSysGetRealtimeCounterX();
|
|
|
|
rtcnt_t end = start + cycles;
|
|
|
|
|
|
|
|
while (chSysIsCounterWithinX(chSysGetRealtimeCounterX(), start, end)) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* PORT_SUPPORTS_RT == TRUE */
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/**
|
|
|
|
* @brief Makes the specified thread ready for execution.
|
|
|
|
*
|
2013-09-05 12:33:42 +00:00
|
|
|
* @param[in] tp pointer to the @p thread_t object
|
2013-09-01 08:19:24 +00:00
|
|
|
* @param[in] msg the wakeup message
|
|
|
|
*
|
|
|
|
* @return The same reference passed as parameter.
|
|
|
|
*/
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *chSchReadyI(thread_t *tp, msg_t msg) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
chDbgAssert((tp >= nil.threads) &&
|
|
|
|
(tp < &nil.threads[NIL_CFG_NUM_THREADS]),
|
|
|
|
"pointer out of range");
|
|
|
|
chDbgAssert(!NIL_THD_IS_READY(tp), "already ready");
|
2013-09-03 13:50:15 +00:00
|
|
|
chDbgAssert(nil.next <= nil.current, "priority ordering");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
tp->u1.msg = msg;
|
|
|
|
tp->state = NIL_STATE_READY;
|
2015-03-10 15:53:36 +00:00
|
|
|
tp->timeout = (systime_t)0;
|
|
|
|
if (tp < nil.next) {
|
2013-09-05 12:33:42 +00:00
|
|
|
nil.next = tp;
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
2013-09-05 12:33:42 +00:00
|
|
|
return tp;
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-05 10:34:09 +00:00
|
|
|
* @brief Reschedules if needed.
|
2013-09-01 08:19:24 +00:00
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
2013-09-05 10:34:09 +00:00
|
|
|
void chSchRescheduleS(void) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 10:34:09 +00:00
|
|
|
if (chSchIsRescRequiredI()) {
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *otp = nil.current;
|
2013-09-05 10:34:09 +00:00
|
|
|
|
|
|
|
nil.current = nil.next;
|
2013-09-05 12:33:42 +00:00
|
|
|
if (otp == &nil.threads[NIL_CFG_NUM_THREADS]) {
|
2013-09-01 08:19:24 +00:00
|
|
|
NIL_CFG_IDLE_LEAVE_HOOK();
|
|
|
|
}
|
2013-09-05 12:33:42 +00:00
|
|
|
port_switch(nil.next, otp);
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Puts the current thread to sleep into the specified state with
|
|
|
|
* timeout specification.
|
|
|
|
* @details The thread goes into a sleeping state, if it is not awakened
|
|
|
|
* explicitly within the specified system time then it is forcibly
|
|
|
|
* awakened with a @p NIL_MSG_TMO low level message.
|
|
|
|
*
|
|
|
|
* @param[in] newstate the new thread state or a semaphore pointer
|
|
|
|
* @param[in] timeout the number of ticks before the operation timeouts.
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
|
|
|
* @return The wakeup message.
|
|
|
|
* @retval NIL_MSG_TMO if a timeout occurred.
|
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
msg_t chSchGoSleepTimeoutS(tstate_t newstate, systime_t timeout) {
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *ntp, *otp = nil.current;
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
chDbgAssert(otp != &nil.threads[NIL_CFG_NUM_THREADS],
|
2013-09-03 13:21:10 +00:00
|
|
|
"idle cannot sleep");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/* Storing the wait object for the current thread.*/
|
2013-09-05 12:33:42 +00:00
|
|
|
otp->state = newstate;
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-11 12:00:01 +00:00
|
|
|
#if NIL_CFG_ST_TIMEDELTA > 0
|
2013-09-01 08:19:24 +00:00
|
|
|
if (timeout != TIME_INFINITE) {
|
2013-09-13 13:15:31 +00:00
|
|
|
systime_t abstime;
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/* TIMEDELTA makes sure to have enough time to reprogram the timer
|
|
|
|
before the free-running timer counter reaches the selected timeout.*/
|
2015-03-12 09:28:03 +00:00
|
|
|
if (timeout < (systime_t)NIL_CFG_ST_TIMEDELTA) {
|
|
|
|
timeout = (systime_t)NIL_CFG_ST_TIMEDELTA;
|
|
|
|
}
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-13 13:15:31 +00:00
|
|
|
/* Absolute time of the timeout event.*/
|
|
|
|
abstime = chVTGetSystemTimeX() + timeout;
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
if (nil.lasttime == nil.nexttime) {
|
|
|
|
/* Special case, first thread asking for a timeout.*/
|
2013-09-13 13:15:31 +00:00
|
|
|
port_timer_start_alarm(abstime);
|
|
|
|
nil.nexttime = abstime;
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Special case, there are already other threads with a timeout
|
|
|
|
activated, evaluating the order.*/
|
2013-09-13 13:15:31 +00:00
|
|
|
if (chVTIsTimeWithinX(abstime, nil.lasttime, nil.nexttime)) {
|
|
|
|
port_timer_set_alarm(abstime);
|
|
|
|
nil.nexttime = abstime;
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Timeout settings.*/
|
2013-09-13 13:15:31 +00:00
|
|
|
otp->timeout = abstime - nil.lasttime;
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* Timeout settings.*/
|
2013-09-05 12:33:42 +00:00
|
|
|
otp->timeout = timeout;
|
2013-09-01 08:19:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Scanning the whole threads array.*/
|
2013-09-05 12:33:42 +00:00
|
|
|
ntp = nil.threads;
|
2013-09-01 08:19:24 +00:00
|
|
|
while (true) {
|
|
|
|
/* Is this thread ready to execute?*/
|
2013-09-05 12:33:42 +00:00
|
|
|
if (NIL_THD_IS_READY(ntp)) {
|
|
|
|
nil.current = nil.next = ntp;
|
|
|
|
if (ntp == &nil.threads[NIL_CFG_NUM_THREADS]) {
|
2013-09-01 08:19:24 +00:00
|
|
|
NIL_CFG_IDLE_ENTER_HOOK();
|
|
|
|
}
|
2013-09-05 12:33:42 +00:00
|
|
|
port_switch(ntp, otp);
|
2013-09-01 08:19:24 +00:00
|
|
|
return nil.current->u1.msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Points to the next thread in lowering priority order.*/
|
2013-09-05 12:33:42 +00:00
|
|
|
ntp++;
|
|
|
|
chDbgAssert(ntp <= &nil.threads[NIL_CFG_NUM_THREADS],
|
|
|
|
"pointer out of range");
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Sends the current thread sleeping and sets a reference variable.
|
|
|
|
* @note This function must reschedule, it can only be called from thread
|
|
|
|
* context.
|
|
|
|
*
|
|
|
|
* @param[in] trp a pointer to a thread reference object
|
|
|
|
* @param[in] timeout the number of ticks before the operation timeouts,
|
|
|
|
* the following special values are allowed:
|
|
|
|
* - @a TIME_INFINITE no timeout.
|
|
|
|
* .
|
|
|
|
* @return The wake up message.
|
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
2013-09-04 08:19:38 +00:00
|
|
|
msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chDbgAssert(*trp == NULL, "not NULL");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
*trp = nil.current;
|
|
|
|
nil.current->u1.trp = trp;
|
2013-09-03 13:50:15 +00:00
|
|
|
return chSchGoSleepTimeoutS(NIL_STATE_SUSP, timeout);
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Wakes up a thread waiting on a thread reference object.
|
|
|
|
* @note This function must not reschedule because it can be called from
|
|
|
|
* ISR context.
|
|
|
|
*
|
|
|
|
* @param[in] trp a pointer to a thread reference object
|
|
|
|
* @param[in] msg the message code
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
2013-09-04 08:19:38 +00:00
|
|
|
void chThdResumeI(thread_reference_t *trp, msg_t msg) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
if (*trp != NULL) {
|
2013-09-04 08:19:38 +00:00
|
|
|
thread_reference_t tr = *trp;
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chDbgAssert(NIL_THD_IS_SUSP(tr), "not suspended");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
*trp = NULL;
|
2015-03-10 15:53:36 +00:00
|
|
|
(void) chSchReadyI(tr, msg);
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Suspends the invoking thread for the specified time.
|
|
|
|
*
|
2015-03-10 15:53:36 +00:00
|
|
|
* @param[in] timeout the delay in system ticks
|
2013-09-01 08:19:24 +00:00
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2015-03-10 15:53:36 +00:00
|
|
|
void chThdSleep(systime_t timeout) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysLock();
|
2015-03-12 09:28:03 +00:00
|
|
|
chThdSleepS(timeout);
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysUnlock();
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Suspends the invoking thread until the system time arrives to the
|
|
|
|
* specified value.
|
|
|
|
*
|
2015-03-10 15:53:36 +00:00
|
|
|
* @param[in] abstime absolute system time
|
2013-09-01 08:19:24 +00:00
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2015-03-10 15:53:36 +00:00
|
|
|
void chThdSleepUntil(systime_t abstime) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysLock();
|
2015-03-10 15:53:36 +00:00
|
|
|
chThdSleepUntilS(abstime);
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysUnlock();
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a wait operation on a semaphore with timeout specification.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
* @param[in] timeout 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 A message specifying how the invoking thread has been
|
|
|
|
* released from the semaphore.
|
|
|
|
* @retval NIL_MSG_OK if the thread has not stopped on the semaphore or the
|
|
|
|
* semaphore has been signaled.
|
2013-09-03 13:50:15 +00:00
|
|
|
* @retval NIL_MSG_RST if the semaphore has been reset using @p chSemReset().
|
2013-09-01 08:19:24 +00:00
|
|
|
* @retval NIL_MSG_TMO if the semaphore has not been signaled or reset within
|
|
|
|
* the specified timeout.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
msg_t chSemWaitTimeout(semaphore_t *sp, systime_t timeout) {
|
2013-09-01 08:19:24 +00:00
|
|
|
msg_t msg;
|
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysLock();
|
|
|
|
msg = chSemWaitTimeoutS(sp, timeout);
|
|
|
|
chSysUnlock();
|
2015-03-10 15:53:36 +00:00
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a wait operation on a semaphore with timeout specification.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
* @param[in] timeout 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 A message specifying how the invoking thread has been
|
|
|
|
* released from the semaphore.
|
|
|
|
* @retval NIL_MSG_OK if the thread has not stopped on the semaphore or the
|
|
|
|
* semaphore has been signaled.
|
2013-09-03 13:50:15 +00:00
|
|
|
* @retval NIL_MSG_RST if the semaphore has been reset using @p chSemReset().
|
2013-09-01 08:19:24 +00:00
|
|
|
* @retval NIL_MSG_TMO if the semaphore has not been signaled or reset within
|
|
|
|
* the specified timeout.
|
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
msg_t chSemWaitTimeoutS(semaphore_t *sp, systime_t timeout) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
/* Note, the semaphore counter is a volatile variable so accesses are
|
|
|
|
manually optimized.*/
|
|
|
|
cnt_t cnt = sp->cnt;
|
2015-03-10 15:53:36 +00:00
|
|
|
if (cnt <= (cnt_t)0) {
|
|
|
|
if (TIME_IMMEDIATE == timeout) {
|
2013-09-03 13:21:10 +00:00
|
|
|
return MSG_TIMEOUT;
|
2015-03-10 15:53:36 +00:00
|
|
|
}
|
|
|
|
sp->cnt = cnt - (cnt_t)1;
|
2013-09-01 08:19:24 +00:00
|
|
|
nil.current->u1.semp = sp;
|
2013-09-03 13:50:15 +00:00
|
|
|
return chSchGoSleepTimeoutS(NIL_STATE_WTSEM, timeout);
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
2015-03-10 15:53:36 +00:00
|
|
|
sp->cnt = cnt - (cnt_t)1;
|
2013-09-03 13:21:10 +00:00
|
|
|
return MSG_OK;
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a signal operation on a semaphore.
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSemSignal(semaphore_t *sp) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysLock();
|
|
|
|
chSemSignalI(sp);
|
|
|
|
chSchRescheduleS();
|
|
|
|
chSysUnlock();
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a signal operation on a semaphore.
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSemSignalI(semaphore_t *sp) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2015-03-10 15:53:36 +00:00
|
|
|
if (++sp->cnt <= (cnt_t)0) {
|
2013-09-04 08:19:38 +00:00
|
|
|
thread_reference_t tr = nil.threads;
|
2013-09-01 08:19:24 +00:00
|
|
|
while (true) {
|
|
|
|
/* Is this thread waiting on this semaphore?*/
|
|
|
|
if (tr->u1.semp == sp) {
|
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chDbgAssert(NIL_THD_IS_WTSEM(tr), "not waiting");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2015-03-10 15:53:36 +00:00
|
|
|
(void) chSchReadyI(tr, MSG_OK);
|
2013-09-01 08:19:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
tr++;
|
2013-09-04 08:19:38 +00:00
|
|
|
|
|
|
|
chDbgAssert(tr < &nil.threads[NIL_CFG_NUM_THREADS],
|
|
|
|
"pointer out of range");
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a reset operation on the semaphore.
|
|
|
|
* @post After invoking this function all the threads waiting on the
|
|
|
|
* semaphore, if any, are released and the semaphore counter is set
|
|
|
|
* to the specified, non negative, value.
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
* @param[in] n the new value of the semaphore counter. The value must
|
|
|
|
* be non-negative.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSemReset(semaphore_t *sp, cnt_t n) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-03 13:50:15 +00:00
|
|
|
chSysLock();
|
|
|
|
chSemResetI(sp, n);
|
|
|
|
chSchRescheduleS();
|
|
|
|
chSysUnlock();
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Performs a reset operation on the semaphore.
|
|
|
|
* @post After invoking this function all the threads waiting on the
|
|
|
|
* semaphore, if any, are released and the semaphore counter is set
|
|
|
|
* to the specified, non negative, value.
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param[in] sp pointer to a @p semaphore_t structure
|
|
|
|
* @param[in] n the new value of the semaphore counter. The value must
|
|
|
|
* be non-negative.
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
2013-09-03 13:50:15 +00:00
|
|
|
void chSemResetI(semaphore_t *sp, cnt_t n) {
|
2013-09-05 12:33:42 +00:00
|
|
|
thread_t *tp;
|
2013-09-01 08:19:24 +00:00
|
|
|
cnt_t cnt;
|
|
|
|
|
|
|
|
cnt = sp->cnt;
|
|
|
|
sp->cnt = n;
|
2013-09-05 12:33:42 +00:00
|
|
|
tp = nil.threads;
|
2015-03-10 15:53:36 +00:00
|
|
|
while (cnt < (cnt_t)0) {
|
2013-09-01 08:19:24 +00:00
|
|
|
/* Is this thread waiting on this semaphore?*/
|
2013-09-05 12:33:42 +00:00
|
|
|
if (tp->u1.semp == sp) {
|
2013-09-01 08:19:24 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
chDbgAssert(NIL_THD_IS_WTSEM(tp), "not waiting");
|
2013-09-01 08:19:24 +00:00
|
|
|
|
|
|
|
cnt++;
|
2015-03-10 15:53:36 +00:00
|
|
|
(void) chSchReadyI(tp, MSG_RESET);
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
2013-09-05 12:33:42 +00:00
|
|
|
tp++;
|
2013-09-04 08:19:38 +00:00
|
|
|
|
2013-09-05 12:33:42 +00:00
|
|
|
chDbgAssert(tp < &nil.threads[NIL_CFG_NUM_THREADS],
|
2013-09-04 08:19:38 +00:00
|
|
|
"pointer out of range");
|
2013-09-01 08:19:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-04 10:26:42 +00:00
|
|
|
/**
|
|
|
|
* @brief Adds a set of event flags directly to the specified @p thread_t.
|
|
|
|
*
|
|
|
|
* @param[in] tp the thread to be signaled
|
|
|
|
* @param[in] mask the event flags set to be ORed
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
void chEvtSignal(thread_t *tp, eventmask_t mask) {
|
|
|
|
|
|
|
|
chSysLock();
|
|
|
|
chEvtSignalI(tp, mask);
|
|
|
|
chSchRescheduleS();
|
|
|
|
chSysUnlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Adds a set of event flags directly to the specified @p thread_t.
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param[in] tp the thread to be signaled
|
|
|
|
* @param[in] mask the event flags set to be ORed
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
void chEvtSignalI(thread_t *tp, eventmask_t mask) {
|
|
|
|
|
|
|
|
tp->epmask |= mask;
|
2015-03-10 15:53:36 +00:00
|
|
|
if (NIL_THD_IS_WTOREVT(tp) &&
|
|
|
|
((tp->epmask & tp->u1.ewmask) != (eventmask_t)0)) {
|
|
|
|
(void) chSchReadyI(tp, MSG_OK);
|
|
|
|
}
|
2013-09-04 10:26:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Waits for any of the specified events.
|
|
|
|
* @details The function waits for any event among those specified in
|
|
|
|
* @p mask to become pending then the events are cleared and
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
|
|
|
* for, @p ALL_EVENTS enables all the events
|
|
|
|
* @param[in] timeout 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.
|
|
|
|
* @retval 0 if the operation has timed out.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
eventmask_t chEvtWaitAnyTimeout(eventmask_t mask, systime_t timeout) {
|
|
|
|
eventmask_t m;
|
|
|
|
|
|
|
|
chSysLock();
|
2014-02-11 13:00:24 +00:00
|
|
|
m = chEvtWaitAnyTimeoutS(mask, timeout);
|
|
|
|
chSysUnlock();
|
2015-03-10 15:53:36 +00:00
|
|
|
|
2014-02-11 13:00:24 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Waits for any of the specified events.
|
|
|
|
* @details The function waits for any event among those specified in
|
|
|
|
* @p mask to become pending then the events are cleared and
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @param[in] mask mask of the event flags that the function should wait
|
|
|
|
* for, @p ALL_EVENTS enables all the events
|
|
|
|
* @param[in] timeout 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.
|
|
|
|
* @retval 0 if the operation has timed out.
|
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
|
|
|
eventmask_t chEvtWaitAnyTimeoutS(eventmask_t mask, systime_t timeout) {
|
|
|
|
thread_t *ctp = nil.current;
|
|
|
|
eventmask_t m;
|
|
|
|
|
2015-03-10 15:53:36 +00:00
|
|
|
if ((m = (ctp->epmask & mask)) == (eventmask_t)0) {
|
2013-09-04 10:26:42 +00:00
|
|
|
if (TIME_IMMEDIATE == timeout) {
|
|
|
|
chSysUnlock();
|
2015-03-10 15:53:36 +00:00
|
|
|
|
2013-09-04 10:26:42 +00:00
|
|
|
return (eventmask_t)0;
|
|
|
|
}
|
|
|
|
ctp->u1.ewmask = mask;
|
|
|
|
if (chSchGoSleepTimeoutS(NIL_STATE_WTOREVT, timeout) < MSG_OK) {
|
|
|
|
chSysUnlock();
|
2015-03-10 15:53:36 +00:00
|
|
|
|
2013-09-04 10:26:42 +00:00
|
|
|
return (eventmask_t)0;
|
|
|
|
}
|
|
|
|
m = ctp->epmask & mask;
|
|
|
|
}
|
|
|
|
ctp->epmask &= ~m;
|
2015-03-10 15:53:36 +00:00
|
|
|
|
2013-09-04 10:26:42 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2013-09-01 08:19:24 +00:00
|
|
|
/** @} */
|