From 4a3157898226b00acd59e9ca23eb2ac4c21fea25 Mon Sep 17 00:00:00 2001 From: Marian Buschsieweke Date: Wed, 5 Aug 2020 22:09:38 +0200 Subject: [PATCH] core/thread: Add/change helper access functions - Add `thread_get_active()` to access the TCB - Add `thread_get_unchecked()` as fast alternative to `thread_get()` - Drop `volatile` qualifier in `thread_get()` - Right now every caller of this function does this. It is better to contain this undefined behavior to at least one place in code --- core/include/thread.h | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/core/include/thread.h b/core/include/thread.h index 6a1e6cfb71..a2d2a1486d 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -349,6 +349,17 @@ kernel_pid_t thread_create(char *stack, void *arg, const char *name); +/** + * @brief Retrieve a thread control block by PID. + * @pre @p pid is valid + * @param[in] pid Thread to retrieve. + * @return `NULL` if the PID is invalid or there is no such thread. + */ +static inline thread_t *thread_get_unchecked(kernel_pid_t pid) +{ + return (thread_t *)sched_threads[pid]; +} + /** * @brief Retrieve a thread control block by PID. * @details This is a bound-checked variant of accessing `sched_threads[pid]` directly. @@ -356,10 +367,10 @@ kernel_pid_t thread_create(char *stack, * @param[in] pid Thread to retrieve. * @return `NULL` if the PID is invalid or there is no such thread. */ -static inline volatile thread_t *thread_get(kernel_pid_t pid) +static inline thread_t *thread_get(kernel_pid_t pid) { if (pid_is_valid(pid)) { - return sched_threads[pid]; + return thread_get_unchecked(pid); } return NULL; } @@ -449,6 +460,20 @@ static inline kernel_pid_t thread_getpid(void) return sched_active_pid; } +/** + * @brief Returns a pointer to the Thread Control Block of the currently + * running thread + * + * @return Pointer to the TCB of the currently running thread, or `NULL` if + * no thread is running + */ +static inline thread_t *thread_get_active(void) +{ + extern volatile thread_t *sched_active_thread; + + return (thread_t *)sched_active_thread; +} + /** * @brief Gets called upon thread creation to set CPU registers * @@ -495,7 +520,7 @@ const char *thread_getname(kernel_pid_t pid); * * Only works if the thread was created with the flag THREAD_CREATE_STACKTEST. * - * @param[in] stack the stack you want to measure. try `sched_active_thread->stack_start` + * @param[in] stack the stack you want to measure. Try `thread_get_active()->stack_start` * * @return the amount of unused space of the thread's stack */