diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chthreads.c | 25 | ||||
-rw-r--r-- | src/include/threads.h | 46 |
2 files changed, 38 insertions, 33 deletions
diff --git a/src/chthreads.c b/src/chthreads.c index 394da5d2a..6b1121d97 100644 --- a/src/chthreads.c +++ b/src/chthreads.c @@ -40,7 +40,7 @@ Thread *init_thread(Thread *tp, tprio_t prio) { tp->p_mtxlist = NULL; #endif #ifdef CH_USE_WAITEXIT - list_init(&tp->p_waiting); + tp->p_waiting = NULL; #endif #ifdef CH_USE_MESSAGES queue_init(&tp->p_msgqueue); @@ -324,8 +324,10 @@ void chThdExit(msg_t msg) { tp->p_exitcode = msg; THREAD_EXT_EXIT(tp); #ifdef CH_USE_WAITEXIT - while (notempty(&tp->p_waiting)) - chSchReadyI(list_remove(&tp->p_waiting)); +// while (notempty(&tp->p_waiting)) +// chSchReadyI(list_remove(&tp->p_waiting)); + if (tp->p_waiting != NULL) + chSchReadyI(tp->p_waiting); #endif #ifdef CH_USE_EXIT_EVENT chEvtBroadcastI(&tp->p_exitesource); @@ -354,14 +356,21 @@ void chThdExit(msg_t msg) { * must not be used as parameter for further system calls. * @note The function is available only if the \p CH_USE_WAITEXIT * option is enabled in \p chconf.h. + * @note Only one thread can be waiting for another thread at any time. You + * should imagine the threads as having a reference counter that is set + * to one when the thread is created, chThdWait() decreases the reference + * and the memory is freed when the counter reaches zero. In the current + * implementation there is no real reference counter in the thread + * structure but it is a planned extension. */ msg_t chThdWait(Thread *tp) { msg_t msg; chSysLock(); - chDbgAssert((tp != NULL) && (tp != currp), "chthreads.c, chThdWait()"); + chDbgAssert((tp != NULL) && (tp != currp) && (tp->p_waiting != NULL), + "chthreads.c, chThdWait()"); if (tp->p_state != PREXIT) { - list_insert(currp, &tp->p_waiting); + tp->p_waiting = currp; chSchGoSleepS(PRWAIT); } msg = tp->p_exitcode; @@ -369,12 +378,8 @@ msg_t chThdWait(Thread *tp) { chSysUnlock(); return msg; #else /* CH_USE_DYNAMIC */ - if (notempty(&tp->p_waiting)) { - chSysUnlock(); - return msg; - } - /* This is the last thread waiting for termination, returning memory.*/ + /* Returning memory.*/ tmode_t mode = tp->p_flags & P_MEM_MODE_MASK; chSysUnlock(); diff --git a/src/include/threads.h b/src/include/threads.h index d5f82ea9f..959a48b0e 100644 --- a/src/include/threads.h +++ b/src/include/threads.h @@ -34,22 +34,22 @@ */ struct Thread { /** Next \p Thread in the threads list.*/ - Thread *p_next; + Thread *p_next; /* End of the fields shared with the ThreadsList structure. */ /** Previous \p Thread in the threads list.*/ - Thread *p_prev; + Thread *p_prev; /* End of the fields shared with the ThreadsQueue structure. */ /** The thread priority.*/ - tprio_t p_prio; + tprio_t p_prio; /* End of the fields shared with the ReadyList structure. */ /** Thread identifier. */ - tid_t p_tid; + tid_t p_tid; /** Current thread state.*/ - tstate_t p_state; + tstate_t p_state; /** Mode flags. */ - tmode_t p_flags; + tmode_t p_flags; /** Machine dependent processor context.*/ - Context p_ctx; + Context p_ctx; /* * The following fields are merged in unions because they are all * state-specific fields. This trick saves some extra space for each @@ -57,34 +57,34 @@ struct Thread { */ union { /** Thread wakeup code (only valid when exiting the \p PRREADY state).*/ - msg_t p_rdymsg; + msg_t p_rdymsg; /** The thread exit code (only while in \p PREXIT state).*/ - msg_t p_exitcode; + msg_t p_exitcode; #ifdef CH_USE_SEMAPHORES /** Semaphore where the thread is waiting on (only in \p PRWTSEM state).*/ - Semaphore *p_wtsemp; + Semaphore *p_wtsemp; #endif #ifdef CH_USE_MUTEXES /** Mutex where the thread is waiting on (only in \p PRWTMTX state).*/ - Mutex *p_wtmtxp; + Mutex *p_wtmtxp; #endif #ifdef CH_USE_CONDVARS /** CondVar where the thread is waiting on (only in \p PRWTCOND state).*/ - CondVar *p_wtcondp; + CondVar *p_wtcondp; #endif #ifdef CH_USE_MESSAGES /** Destination thread for message send (only in \p PRSNDMSG state).*/ - Thread *p_wtthdp; + Thread *p_wtthdp; #endif #ifdef CH_USE_EVENTS /** Enabled events mask (only while in \p PRWTOREVT or \p PRWTANDEVT states). */ - eventmask_t p_ewmask; + eventmask_t p_ewmask; #endif #ifdef CH_USE_TRACE /** Kernel object where the thread is waiting on. It is only valid when the thread is some sleeping states.*/ - void *p_wtobjp; + void *p_wtobjp; #endif }; /* @@ -92,29 +92,29 @@ struct Thread { */ #ifdef CH_USE_WAITEXIT /** The list of the threads waiting for this thread termination. */ - ThreadsList p_waiting; + Thread *p_waiting; #endif #ifdef CH_USE_EXIT_EVENT /** The thread termination \p EventSource. */ - EventSource p_exitesource; + EventSource p_exitesource; #endif #ifdef CH_USE_MESSAGES - ThreadsQueue p_msgqueue; - msg_t p_msg; + ThreadsQueue p_msgqueue; + msg_t p_msg; #endif #ifdef CH_USE_EVENTS /** Pending events mask. */ - eventmask_t p_epending; + eventmask_t p_epending; #endif #ifdef CH_USE_MUTEXES /** List of mutexes owned by this thread, \p NULL terminated. */ - Mutex *p_mtxlist; + Mutex *p_mtxlist; /** Thread's own, non-inherited, priority. */ - tprio_t p_realprio; + tprio_t p_realprio; #endif #if defined(CH_USE_DYNAMIC) && defined(CH_USE_MEMPOOLS) /** Memory Pool where the thread workspace is returned. */ - void *p_mpool; + void *p_mpool; #endif #ifdef CH_USE_THREAD_EXT THREAD_EXT_FIELDS |