123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- #ifndef CHTHREADS_H
- #define CHTHREADS_H
- typedef void (*tfunc_t)(void *p);
- typedef struct {
-
- const char *name;
-
- stkalign_t *wbase;
-
- stkalign_t *wend;
-
- tprio_t prio;
-
- tfunc_t funcp;
-
- void *arg;
- } thread_descriptor_t;
- #define _THREADS_QUEUE_DATA(name) {(thread_t *)&name, (thread_t *)&name}
- #define _THREADS_QUEUE_DECL(name) \
- threads_queue_t name = _THREADS_QUEUE_DATA(name)
- #define THD_WORKING_AREA_SIZE(n) \
- MEM_ALIGN_NEXT(sizeof(thread_t) + PORT_WA_SIZE(n), PORT_STACK_ALIGN)
- #define THD_WORKING_AREA(s, n) PORT_WORKING_AREA(s, n)
- #define THD_WORKING_AREA_BASE(s) ((stkalign_t *)(s))
- #define THD_WORKING_AREA_END(s) (THD_WORKING_AREA_BASE(s) + \
- (sizeof (s) / sizeof (stkalign_t)))
- #define THD_FUNCTION(tname, arg) PORT_THD_FUNCTION(tname, arg)
- #define chThdSleepSeconds(sec) chThdSleep(TIME_S2I(sec))
- #define chThdSleepMilliseconds(msec) chThdSleep(TIME_MS2I(msec))
- #define chThdSleepMicroseconds(usec) chThdSleep(TIME_US2I(usec))
- #ifdef __cplusplus
- extern "C" {
- #endif
- thread_t *_thread_init(thread_t *tp, const char *name, tprio_t prio);
- #if CH_DBG_FILL_THREADS == TRUE
- void _thread_memfill(uint8_t *startp, uint8_t *endp, uint8_t v);
- #endif
- 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);
- thread_t *chThdCreateStatic(void *wsp, size_t size,
- tprio_t prio, tfunc_t pf, void *arg);
- thread_t *chThdStart(thread_t *tp);
- #if CH_CFG_USE_REGISTRY == TRUE
- thread_t *chThdAddRef(thread_t *tp);
- void chThdRelease(thread_t *tp);
- #endif
- void chThdExit(msg_t msg);
- void chThdExitS(msg_t msg);
- #if CH_CFG_USE_WAITEXIT == TRUE
- msg_t chThdWait(thread_t *tp);
- #endif
- tprio_t chThdSetPriority(tprio_t newprio);
- void chThdTerminate(thread_t *tp);
- msg_t chThdSuspendS(thread_reference_t *trp);
- msg_t chThdSuspendTimeoutS(thread_reference_t *trp, sysinterval_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, sysinterval_t timeout);
- void chThdDequeueNextI(threads_queue_t *tqp, msg_t msg);
- void chThdDequeueAllI(threads_queue_t *tqp, msg_t msg);
- void chThdSleep(sysinterval_t time);
- void chThdSleepUntil(systime_t time);
- systime_t chThdSleepUntilWindowed(systime_t prev, systime_t next);
- void chThdYield(void);
- #ifdef __cplusplus
- }
- #endif
-
- static inline thread_t *chThdGetSelfX(void) {
- return ch.rlist.current;
- }
- static inline tprio_t chThdGetPriorityX(void) {
- return chThdGetSelfX()->prio;
- }
- #if (CH_DBG_THREADS_PROFILING == TRUE) || defined(__DOXYGEN__)
- static inline systime_t chThdGetTicksX(thread_t *tp) {
- return tp->time;
- }
- #endif
- #if (CH_DBG_ENABLE_STACK_CHECK == TRUE) || (CH_CFG_USE_DYNAMIC == TRUE) || \
- defined(__DOXYGEN__)
- static inline stkalign_t *chThdGetWorkingAreaX(thread_t *tp) {
- return tp->wabase;
- }
- #endif
- static inline bool chThdTerminatedX(thread_t *tp) {
- return (bool)(tp->state == CH_STATE_FINAL);
- }
- static inline bool chThdShouldTerminateX(void) {
- return (bool)((chThdGetSelfX()->flags & CH_FLAG_TERMINATE) != (tmode_t)0);
- }
- static inline thread_t *chThdStartI(thread_t *tp) {
- chDbgAssert(tp->state == CH_STATE_WTSTART, "wrong state");
- return chSchReadyI(tp);
- }
- static inline void chThdSleepS(sysinterval_t ticks) {
- chDbgCheck(ticks != TIME_IMMEDIATE);
- (void) chSchGoSleepTimeoutS(CH_STATE_SLEEPING, ticks);
- }
- static inline void chThdQueueObjectInit(threads_queue_t *tqp) {
- queue_init(tqp);
- }
- static inline bool chThdQueueIsEmptyI(threads_queue_t *tqp) {
- chDbgCheckClassI();
- return queue_isempty(tqp);
- }
- 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);
- chDbgAssert(tp->state == CH_STATE_QUEUED, "invalid state");
- tp->u.rdymsg = msg;
- (void) chSchReadyI(tp);
- }
- #endif
|