2010-02-21 07:24:53 +00:00
|
|
|
/*
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS - Copyright (C) 2006..2015 Giovanni Di Sirio.
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
This file is part of ChibiOS.
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2015-01-11 13:45:54 +00:00
|
|
|
ChibiOS is free software; you can redistribute it and/or modify
|
2010-02-21 07:24:53 +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,
|
2010-02-21 07:24:53 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
2015-03-07 11:07:31 +00:00
|
|
|
/*lint -sem(chThdExit, r_no) -sem(chThdExitS, r_no)*/
|
|
|
|
|
2013-06-16 16:12:33 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module constants. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module pre-compile time settings. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
#if !defined(CH_CFG_THREAD_EXTRA_FIELDS)
|
|
|
|
#error "CH_CFG_THREAD_EXTRA_FIELDS not defined in chconf.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CH_CFG_THREAD_INIT_HOOK)
|
|
|
|
#error "CH_CFG_THREAD_INIT_HOOK not defined in chconf.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CH_CFG_THREAD_EXIT_HOOK)
|
|
|
|
#error "CH_CFG_THREAD_EXIT_HOOK not defined in chconf.h"
|
|
|
|
#endif
|
|
|
|
|
2013-06-16 16:12:33 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Derived constants and error checks. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module data structures and types. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
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
|
|
|
*/
|
2015-04-03 12:48:22 +00:00
|
|
|
typedef void (*tfunc_t)(void *p);
|
2010-02-21 07:24:53 +00:00
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Type of a thread descriptor.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
/**
|
|
|
|
* @brief Thread name.
|
|
|
|
*/
|
|
|
|
const char *name;
|
|
|
|
/**
|
|
|
|
* @brief Pointer to the working area base.
|
|
|
|
*/
|
|
|
|
stkalign_t *wbase;
|
|
|
|
/**
|
|
|
|
* @brief End of the working area.
|
|
|
|
*/
|
|
|
|
stkalign_t *wend;
|
|
|
|
/**
|
|
|
|
* @brief Thread priority.
|
|
|
|
*/
|
|
|
|
tprio_t prio;
|
|
|
|
/**
|
|
|
|
* @brief Thread function pointer.
|
|
|
|
*/
|
|
|
|
tfunc_t funcp;
|
|
|
|
/**
|
|
|
|
* @brief Thread argument.
|
|
|
|
*/
|
|
|
|
void *arg;
|
|
|
|
} thread_descriptor_t;
|
|
|
|
|
2013-06-16 16:12:33 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Module macros. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-11-14 10:22:19 +00:00
|
|
|
/**
|
|
|
|
* @name Threads queues
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @brief Data part of a static threads queue object initializer.
|
|
|
|
* @details This macro should be used when statically initializing a threads
|
|
|
|
* queue that is part of a bigger structure.
|
|
|
|
*
|
|
|
|
* @param[in] name the name of the threads queue variable
|
|
|
|
*/
|
2014-09-10 10:18:02 +00:00
|
|
|
#define _THREADS_QUEUE_DATA(name) {(thread_t *)&name, (thread_t *)&name}
|
2013-11-14 10:22:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Static threads queue object initializer.
|
|
|
|
* @details Statically initialized threads queues require no explicit
|
|
|
|
* initialization using @p queue_init().
|
|
|
|
*
|
|
|
|
* @param[in] name the name of the threads queue variable
|
|
|
|
*/
|
2014-09-10 10:18:02 +00:00
|
|
|
#define _THREADS_QUEUE_DECL(name) \
|
|
|
|
threads_queue_t name = _THREADS_QUEUE_DATA(name)
|
2013-11-14 10:22:19 +00:00
|
|
|
/** @} */
|
|
|
|
|
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
|
2016-02-16 10:07:00 +00:00
|
|
|
thread_t *_thread_init(thread_t *tp, const char *name, tprio_t prio);
|
2015-03-05 21:28:51 +00:00
|
|
|
#if CH_DBG_FILL_THREADS == TRUE
|
2011-07-08 19:29:00 +00:00
|
|
|
void _thread_memfill(uint8_t *startp, uint8_t *endp, uint8_t v);
|
|
|
|
#endif
|
2016-02-16 10:07:00 +00:00
|
|
|
thread_t *chThdCreateSuspendedI(const thread_descriptor_t *tdp);
|
|
|
|
thread_t *chThdCreateSuspended(const thread_descriptor_t *tdp);
|
|
|
|
thread_t *chThdCreateI(const thread_descriptor_t *tdp);
|
|
|
|
thread_t *chThdCreate(const thread_descriptor_t *tdp);
|
2013-07-19 12:22:31 +00:00
|
|
|
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-11-14 10:22:19 +00:00
|
|
|
msg_t chThdSuspendS(thread_reference_t *trp);
|
|
|
|
msg_t chThdSuspendTimeoutS(thread_reference_t *trp, systime_t timeout);
|
|
|
|
void chThdResumeI(thread_reference_t *trp, msg_t msg);
|
|
|
|
void chThdResumeS(thread_reference_t *trp, msg_t msg);
|
|
|
|
void chThdResume(thread_reference_t *trp, msg_t msg);
|
|
|
|
msg_t chThdEnqueueTimeoutS(threads_queue_t *tqp, systime_t timeout);
|
|
|
|
void chThdDequeueNextI(threads_queue_t *tqp, msg_t msg);
|
|
|
|
void chThdDequeueAllI(threads_queue_t *tqp, msg_t msg);
|
2011-07-08 19:29:00 +00:00
|
|
|
void chThdSleep(systime_t time);
|
|
|
|
void chThdSleepUntil(systime_t time);
|
2014-08-10 16:45:47 +00:00
|
|
|
systime_t chThdSleepUntilWindowed(systime_t prev, systime_t next);
|
2011-07-08 19:29:00 +00:00
|
|
|
void chThdYield(void);
|
|
|
|
void chThdExit(msg_t msg);
|
2011-09-20 17:08:22 +00:00
|
|
|
void chThdExitS(msg_t msg);
|
2015-03-05 21:28:51 +00:00
|
|
|
#if CH_CFG_USE_WAITEXIT == TRUE
|
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.
|
|
|
|
*
|
2014-12-11 15:12:21 +00:00
|
|
|
* @return A pointer to the current thread.
|
|
|
|
*
|
2013-08-16 17:41:56 +00:00
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
static inline thread_t *chThdGetSelfX(void) {
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
return ch.rlist.current;
|
2013-08-16 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns the current thread priority.
|
|
|
|
* @note Can be invoked in any context.
|
|
|
|
*
|
2014-12-11 15:12:21 +00:00
|
|
|
* @return The current thread priority.
|
|
|
|
*
|
2013-08-16 17:41:56 +00:00
|
|
|
* @xclass
|
|
|
|
*/
|
|
|
|
static inline tprio_t chThdGetPriorityX(void) {
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
return chThdGetSelfX()->prio;
|
2013-08-16 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2014-12-11 15:12:21 +00:00
|
|
|
* @return The number of consumed system ticks.
|
2013-08-16 17:41:56 +00:00
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
2015-03-05 21:28:51 +00:00
|
|
|
#if (CH_DBG_THREADS_PROFILING == TRUE) || defined(__DOXYGEN__)
|
2013-08-16 17:41:56 +00:00
|
|
|
static inline systime_t chThdGetTicksX(thread_t *tp) {
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
return tp->time;
|
2013-08-16 17:41:56 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2016-02-16 10:07:00 +00:00
|
|
|
* @brief Returns the stack limit of the specified thread.
|
2013-08-16 17:41:56 +00:00
|
|
|
*
|
|
|
|
* @param[in] tp pointer to the thread
|
2016-02-16 10:07:00 +00:00
|
|
|
* @return The stack limit pointer.
|
2013-08-16 17:41:56 +00:00
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
2016-02-16 10:07:00 +00:00
|
|
|
static inline stkalign_t *chthdGetStackLimitX(thread_t *tp) {
|
2013-08-16 17:41:56 +00:00
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
return tp->stklimit;
|
2013-08-16 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-16 10:07:00 +00:00
|
|
|
* @brief Verifies if the specified thread is in the @p CH_STATE_FINAL state.
|
2013-08-16 17:41:56 +00:00
|
|
|
*
|
2016-02-16 10:07:00 +00:00
|
|
|
* @param[in] tp pointer to the thread
|
|
|
|
* @retval true thread terminated.
|
|
|
|
* @retval false thread not terminated.
|
2013-08-16 17:41:56 +00:00
|
|
|
*
|
|
|
|
* @xclass
|
|
|
|
*/
|
2016-02-16 10:07:00 +00:00
|
|
|
static inline bool chThdTerminatedX(thread_t *tp) {
|
2013-08-16 17:41:56 +00:00
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
return (bool)(tp->state == CH_STATE_FINAL);
|
2013-08-16 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Resumes a thread created with @p chThdCreateI().
|
|
|
|
*
|
|
|
|
* @param[in] tp pointer to the thread
|
2014-11-23 17:14:56 +00:00
|
|
|
* @return The pointer to the @p thread_t structure allocated for
|
|
|
|
* the thread into the working space area.
|
2013-08-16 17:41:56 +00:00
|
|
|
*
|
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) {
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
chDbgAssert(tp->state == CH_STATE_WTSTART, "wrong state");
|
2013-08-16 17:41:56 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2015-03-05 21:28:51 +00:00
|
|
|
(void) chSchGoSleepTimeoutS(CH_STATE_SLEEPING, time);
|
2013-08-16 17:41:56 +00:00
|
|
|
}
|
|
|
|
|
2013-11-14 10:22:19 +00:00
|
|
|
/**
|
|
|
|
* @brief Initializes a threads queue object.
|
|
|
|
*
|
|
|
|
* @param[out] tqp pointer to the threads queue object
|
|
|
|
*
|
|
|
|
* @init
|
|
|
|
*/
|
|
|
|
static inline void chThdQueueObjectInit(threads_queue_t *tqp) {
|
|
|
|
|
|
|
|
queue_init(tqp);
|
|
|
|
}
|
|
|
|
|
2014-09-10 12:35:59 +00:00
|
|
|
/**
|
|
|
|
* @brief Evaluates to @p true if the specified queue is empty.
|
|
|
|
*
|
|
|
|
* @param[out] tqp pointer to the threads queue object
|
|
|
|
* @return The queue status.
|
|
|
|
* @retval false if the queue is not empty.
|
|
|
|
* @retval true if the queue is empty.
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
static inline bool chThdQueueIsEmptyI(threads_queue_t *tqp) {
|
|
|
|
|
|
|
|
chDbgCheckClassI();
|
|
|
|
|
|
|
|
return queue_isempty(tqp);
|
|
|
|
}
|
|
|
|
|
2014-09-17 10:37:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Dequeues and wakes up one thread from the threads queue object.
|
|
|
|
* @details Dequeues one thread from the queue without checking if the queue
|
|
|
|
* is empty.
|
|
|
|
* @pre The queue must contain at least an object.
|
|
|
|
*
|
|
|
|
* @param[in] tqp pointer to the threads queue object
|
|
|
|
* @param[in] msg the message code
|
|
|
|
*
|
|
|
|
* @iclass
|
|
|
|
*/
|
|
|
|
static inline void chThdDoDequeueNextI(threads_queue_t *tqp, msg_t msg) {
|
|
|
|
thread_t *tp;
|
|
|
|
|
|
|
|
chDbgAssert(queue_notempty(tqp), "empty queue");
|
|
|
|
|
|
|
|
tp = queue_fifo_remove(tqp);
|
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
chDbgAssert(tp->state == CH_STATE_QUEUED, "invalid state");
|
2014-09-17 10:37:54 +00:00
|
|
|
|
2016-02-16 10:07:00 +00:00
|
|
|
tp->u.rdymsg = msg;
|
2015-03-05 21:28:51 +00:00
|
|
|
(void) chSchReadyI(tp);
|
2014-09-17 10:37:54 +00:00
|
|
|
}
|
|
|
|
|
2010-02-27 08:54:22 +00:00
|
|
|
#endif /* _CHTHREADS_H_ */
|
2010-02-21 07:24:53 +00:00
|
|
|
|
|
|
|
/** @} */
|