1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

core: add thread_get()

Remove PID check duplication in `thread_getstatus()` and
`thread_getname()`.
This commit is contained in:
René Kijewski 2014-08-13 09:26:27 +02:00
parent de039b0ec6
commit a7e5157fd9
2 changed files with 22 additions and 10 deletions

View File

@ -79,6 +79,16 @@ kernel_pid_t thread_create(char *stack,
void *(*function)(void *arg),
void *arg,
const char *name);
/**
* @brief Retreive a thread control block by PID.
* @details This is a bound-checked variant of accessing `sched_threads[pid]` directly.
* If you know that the PID is valid, then don't use this function.
* @param[in] pid Thread to retreive.
* @return `NULL` if the PID is invalid or there is no such thread.
*/
volatile tcb_t *thread_get(kernel_pid_t pid);
/**
* @brief Returns the status of a process
*

View File

@ -37,22 +37,24 @@ inline kernel_pid_t thread_getpid(void)
return sched_active_thread->pid;
}
volatile tcb_t *thread_get(kernel_pid_t pid)
{
if ((pid != KERNEL_PID_UNDEF) && (0 <= pid) && (pid < MAXTHREADS)) {
return sched_threads[pid];
}
return NULL;
}
int thread_getstatus(kernel_pid_t pid)
{
if (sched_threads[pid] == NULL) {
return STATUS_NOT_FOUND;
}
return sched_threads[pid]->status;
volatile tcb_t *t = thread_get(pid);
return t ? t->status : STATUS_NOT_FOUND;
}
const char *thread_getname(kernel_pid_t pid)
{
if (sched_threads[pid] == NULL) {
return NULL;
}
return sched_threads[pid]->name;
volatile tcb_t *t = thread_get(pid);
return t ? t->name : NULL;
}
void thread_sleep(void)