2010-02-21 07:24:53 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 Giovanni Di Sirio.
|
2010-02-21 07:24:53 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-02-27 08:54:22 +00:00
|
|
|
* @file chthreads.h
|
2013-06-16 16:12:33 +00:00
|
|
|
* @brief Threads module macros and structures.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
|
|
|
* @addtogroup threads
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#ifndef _CHTHREADS_H_
|
|
|
|
#define _CHTHREADS_H_
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2013-06-16 16:12:33 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-08-22 17:18:52 +00:00
|
|
|
/**
|
|
|
|
* @name Thread states
|
|
|
|
* @{
|
|
|
|
*/
|
2013-07-24 14:54:26 +00:00
|
|
|
#define CH_STATE_READY 0 /**< @brief Waiting on the ready list. */
|
|
|
|
#define CH_STATE_CURRENT 1 /**< @brief Currently running. */
|
2013-08-16 17:41:56 +00:00
|
|
|
#define CH_STATE_WTSTART 2 /**< @brief Created but not started. */
|
|
|
|
#define CH_STATE_SUSPENDED 3 /**< @brief Created in suspended state. */
|
2013-08-17 11:04:57 +00:00
|
|
|
#define CH_STATE_QUEUED 4 /**< @brief Waiting on an I/O queue. */
|
|
|
|
#define CH_STATE_WTSEM 5 /**< @brief Waiting on a semaphore. */
|
|
|
|
#define CH_STATE_WTMTX 6 /**< @brief Waiting on a mutex. */
|
|
|
|
#define CH_STATE_WTCOND 7 /**< @brief Waiting on a condition
|
2011-08-22 17:18:52 +00:00
|
|
|
variable. */
|
2013-08-17 11:04:57 +00:00
|
|
|
#define CH_STATE_SLEEPING 8 /**< @brief Waiting in @p chThdSleep()
|
2011-08-22 17:18:52 +00:00
|
|
|
or @p chThdSleepUntil(). */
|
2013-08-17 11:04:57 +00:00
|
|
|
#define CH_STATE_WTEXIT 9 /**< @brief Waiting in @p chThdWait(). */
|
|
|
|
#define CH_STATE_WTOREVT 10 /**< @brief Waiting for an event. */
|
|
|
|
#define CH_STATE_WTANDEVT 11 /**< @brief Waiting for several events. */
|
|
|
|
#define CH_STATE_SNDMSGQ 12 /**< @brief Sending a message, in queue.*/
|
|
|
|
#define CH_STATE_SNDMSG 13 /**< @brief Sent a message, waiting
|
2011-08-22 17:18:52 +00:00
|
|
|
answer. */
|
2013-08-17 11:04:57 +00:00
|
|
|
#define CH_STATE_WTMSG 14 /**< @brief Waiting for a message. */
|
2013-08-16 17:41:56 +00:00
|
|
|
#define CH_STATE_FINAL 15 /**< @brief Thread terminated. */
|
2011-10-23 11:39:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Thread states as array of strings.
|
|
|
|
* @details Each element in an array initialized with this macro can be
|
|
|
|
* indexed using the numeric thread state values.
|
|
|
|
*/
|
2013-07-24 14:54:26 +00:00
|
|
|
#define CH_STATE_NAMES \
|
2013-08-17 11:04:57 +00:00
|
|
|
"READY", "WTSTART", "CURRENT", "SUSPENDED", "QUEUED", "WTSEM", "WTMTX", \
|
|
|
|
"WTCOND", "SLEEPING", "WTEXIT", "WTOREVT", "WTANDEVT", "SNDMSGQ", \
|
|
|
|
"SNDMSG", "WTMSG", "FINAL"
|
2011-08-22 17:18:52 +00:00
|
|
|
/** @} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Thread flags and attributes
|
|
|
|
* @{
|
|
|
|
*/
|
2013-07-24 14:54:26 +00:00
|
|
|
#define CH_FLAG_MODE_MASK 3 /**< @brief Thread memory mode mask. */
|
|
|
|
#define CH_FLAG_MODE_STATIC 0 /**< @brief Static thread. */
|
|
|
|
#define CH_FLAG_MODE_HEAP 1 /**< @brief Thread allocated from a
|
2011-08-23 10:09:08 +00:00
|
|
|
Memory Heap. */
|
2013-07-24 14:54:26 +00:00
|
|
|
#define CH_FLAG_MODE_MEMPOOL 2 /**< @brief Thread allocated from a
|
2011-08-23 10:09:08 +00:00
|
|
|
Memory Pool. */
|
2013-07-24 14:54:26 +00:00
|
|
|
#define CH_FLAG_TERMINATE 4 /**< @brief Termination requested flag. */
|
2011-08-22 17:18:52 +00:00
|
|
|
/** @} */
|
|
|
|
|
2013-06-16 16:12:33 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-08-17 11:04:57 +00:00
|
|
|
/**
|
|
|
|
* @brief Type of a thread reference.
|
|
|
|
*/
|
|
|
|
typedef thread_t * thread_reference_t;
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2011-07-08 19:29:00 +00:00
|
|
|
/**
|
2013-07-19 12:22:31 +00:00
|
|
|
* @brief Thread function.
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
2011-07-08 19:29:00 +00:00
|
|
|
typedef msg_t (*tfunc_t)(void *);
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2013-06-16 16:12:33 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-09-03 09:51:32 +00:00
|
|
|
/**
|
|
|
|
* @name Working Areas and Alignment
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Enforces a correct alignment for a stack area size value.
|
|
|
|
*
|
|
|
|
* @param[in] n the stack size to be aligned to the next stack
|
|
|
|
* alignment boundary
|
|
|
|
* @return The aligned stack size.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define THD_ALIGN_STACK_SIZE(n) \
|
|
|
|
((((n) - 1) | (sizeof(stkalign_t) - 1)) + 1)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Calculates the total Working Area size.
|
|
|
|
*
|
|
|
|
* @param[in] n the stack size to be assigned to the thread
|
|
|
|
* @return The total used memory in bytes.
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define THD_WORKING_AREA_SIZE(n) \
|
|
|
|
THD_ALIGN_STACK_SIZE(sizeof(thread_t) + PORT_WA_SIZE(n))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Static working area allocation.
|
|
|
|
* @details This macro is used to allocate a static thread working area
|
|
|
|
* aligned as both position and size.
|
|
|
|
*
|
|
|
|
* @param[in] s the name to be assigned to the stack array
|
|
|
|
* @param[in] n the stack size to be assigned to the thread
|
|
|
|
*
|
|
|
|
* @api
|
|
|
|
*/
|
|
|
|
#define THD_WORKING_AREA(s, n) \
|
|
|
|
stkalign_t s[THD_WORKING_AREA_SIZE(n) / sizeof(stkalign_t)]
|
|
|
|
/** @} */
|
|
|
|
|
2011-08-23 10:09:08 +00:00
|
|
|
/**
|
|
|
|
* @name Macro Functions
|
|
|
|
* @{
|
|
|
|
*/
|
2010-02-21 07:24:53 +00:00
|
|
|
/**
|
|
|
|
* @brief Delays the invoking thread for the specified number of seconds.
|
|
|
|
* @note The specified time is rounded up to a value allowed by the real
|
2013-04-11 08:25:03 +00:00
|
|
|
* system tick clock.
|
|
|
|
* @note The maximum specifiable value is implementation dependent.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2011-09-20 20:31:20 +00:00
|
|
|
* @param[in] sec time in seconds, must be different from zero
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
|
|
|
#define chThdSleepSeconds(sec) chThdSleep(S2ST(sec))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delays the invoking thread for the specified number of
|
|
|
|
* milliseconds.
|
|
|
|
* @note The specified time is rounded up to a value allowed by the real
|
2013-04-11 08:25:03 +00:00
|
|
|
* system tick clock.
|
|
|
|
* @note The maximum specifiable value is implementation dependent.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2011-09-20 20:31:20 +00:00
|
|
|
* @param[in] msec time in milliseconds, must be different from zero
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
|
|
|
#define chThdSleepMilliseconds(msec) chThdSleep(MS2ST(msec))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Delays the invoking thread for the specified number of
|
|
|
|
* microseconds.
|
|
|
|
* @note The specified time is rounded up to a value allowed by the real
|
2013-04-11 08:25:03 +00:00
|
|
|
* system tick clock.
|
|
|
|
* @note The maximum specifiable value is implementation dependent.
|
2010-02-21 07:24:53 +00:00
|
|
|
*
|
2011-09-20 20:31:20 +00:00
|
|
|
* @param[in] usec time in microseconds, must be different from zero
|
2010-09-21 10:22:06 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-02-21 07:24:53 +00:00
|
|
|
*/
|
|
|
|
#define chThdSleepMicroseconds(usec) chThdSleep(US2ST(usec))
|
2011-08-23 10:09:08 +00:00
|
|
|
/** @} */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2013-06-16 16:48:40 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* External declarations. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-07-08 19:29:00 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
2013-07-19 12:22:31 +00:00
|
|
|
thread_t *_thread_init(thread_t *tp, tprio_t prio);
|
2011-07-08 19:29:00 +00:00
|
|
|
#if CH_DBG_FILL_THREADS
|
|
|
|
void _thread_memfill(uint8_t *startp, uint8_t *endp, uint8_t v);
|
|
|
|
#endif
|
2013-07-19 12:22:31 +00:00
|
|
|
thread_t *chThdCreateI(void *wsp, size_t size,
|
|
|
|
tprio_t prio, tfunc_t pf, void *arg);
|
|
|
|
thread_t *chThdCreateStatic(void *wsp, size_t size,
|
|
|
|
tprio_t prio, tfunc_t pf, void *arg);
|
2013-08-17 11:04:57 +00:00
|
|
|
thread_t *chThdStart(thread_t *tp);
|
2011-07-08 19:29:00 +00:00
|
|
|
tprio_t chThdSetPriority(tprio_t newprio);
|
2013-08-17 11:04:57 +00:00
|
|
|
msg_t chThreadSuspendS(thread_reference_t *trp);
|
2013-08-19 11:45:52 +00:00
|
|
|
msg_t chThreadSuspendTimeoutS(thread_reference_t *trp, systime_t timeout);
|
2013-08-17 11:04:57 +00:00
|
|
|
void chThreadResumeI(thread_reference_t *trp, msg_t msg);
|
|
|
|
void chThreadResumeS(thread_reference_t *trp, msg_t msg);
|
|
|
|
void chThreadResume(thread_reference_t *trp, msg_t msg);
|
2013-07-19 12:22:31 +00:00
|
|
|
void chThdTerminate(thread_t *tp);
|
2011-07-08 19:29:00 +00:00
|
|
|
void chThdSleep(systime_t time);
|
|
|
|
void chThdSleepUntil(systime_t time);
|
|
|
|
void chThdYield(void);
|
|
|
|
void chThdExit(msg_t msg);
|
2011-09-20 17:08:22 +00:00
|
|
|
void chThdExitS(msg_t msg);
|
2013-07-20 10:12:44 +00:00
|
|
|
#if CH_CFG_USE_WAITEXIT
|
2013-07-19 12:22:31 +00:00
|
|
|
msg_t chThdWait(thread_t *tp);
|
2011-07-08 19:29:00 +00:00
|
|
|
#endif
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-16 16:12:33 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module inline functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-08-16 17:41:56 +00:00
|
|
|
/**
|
|
|
|
* @brief Returns a pointer to the current @p thread_t.
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
static inline thread_t *chThdGetSelfX(void) {
|
|
|
|
|
|
|
|
return ch.rlist.r_current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the current thread priority.
|
|
|
|
* @note Can be invoked in any context.
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
static inline tprio_t chThdGetPriorityX(void) {
|
|
|
|
|
|
|
|
return chThdGetSelfX()->p_prio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the number of ticks consumed by the specified thread.
|
|
|
|
* @note This function is only available when the
|
|
|
|
* @p CH_DBG_THREADS_PROFILING configuration option is enabled.
|
|
|
|
*
|
|
|
|
* @param[in] tp pointer to the thread
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
#if CH_DBG_THREADS_PROFILING || defined(__DOXYGEN__)
|
|
|
|
static inline systime_t chThdGetTicksX(thread_t *tp) {
|
|
|
|
|
|
|
|
return tp->p_time;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Verifies if the specified thread is in the @p CH_STATE_FINAL state.
|
|
|
|
*
|
|
|
|
* @param[in] tp pointer to the thread
|
|
|
|
* @retval true thread terminated.
|
|
|
|
* @retval false thread not terminated.
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
static inline bool chThdTerminatedX(thread_t *tp) {
|
|
|
|
|
|
|
|
return (bool)(tp->p_state == CH_STATE_FINAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Verifies if the current thread has a termination request pending.
|
|
|
|
*
|
|
|
|
* @retval true termination request pending.
|
|
|
|
* @retval false termination request not pending.
|
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
static inline bool chThdShouldTerminateX(void) {
|
|
|
|
|
|
|
|
return (bool)(chThdGetSelfX()->p_flags & CH_FLAG_TERMINATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Resumes a thread created with @p chThdCreateI().
|
|
|
|
*
|
|
|
|
* @param[in] tp pointer to the thread
|
|
|
|
*
|
2013-08-17 11:04:57 +00:00
|
|
|
* @iclass
|
2013-08-16 17:41:56 +00:00
|
|
|
*/
|
|
|
|
static inline thread_t *chThdStartI(thread_t *tp) {
|
|
|
|
|
|
|
|
chDbgAssert(tp->p_state == CH_STATE_WTSTART, "wrong state");
|
|
|
|
|
|
|
|
return chSchReadyI(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Suspends the invoking thread for the specified time.
|
|
|
|
*
|
|
|
|
* @param[in] time the delay in system ticks, the special values are
|
|
|
|
* handled as follow:
|
|
|
|
* - @a TIME_INFINITE the thread enters an infinite sleep
|
|
|
|
* state.
|
|
|
|
* - @a TIME_IMMEDIATE this value is not allowed.
|
|
|
|
* .
|
|
|
|
*
|
|
|
|
* @sclass
|
|
|
|
*/
|
|
|
|
static inline void chThdSleepS(systime_t time) {
|
|
|
|
|
|
|
|
chDbgCheck(time != TIME_IMMEDIATE);
|
|
|
|
|
|
|
|
chSchGoSleepTimeoutS(CH_STATE_SLEEPING, time);
|
|
|
|
}
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHTHREADS_H_ */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
|
|
|
/** @} */
|