core: add thread_get()
Remove PID check duplication in `thread_getstatus()` and `thread_getname()`.
This commit is contained in:
parent
de039b0ec6
commit
a7e5157fd9
@ -79,6 +79,16 @@ kernel_pid_t thread_create(char *stack,
|
|||||||
void *(*function)(void *arg),
|
void *(*function)(void *arg),
|
||||||
void *arg,
|
void *arg,
|
||||||
const char *name);
|
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
|
* @brief Returns the status of a process
|
||||||
*
|
*
|
||||||
|
|||||||
@ -37,22 +37,24 @@ inline kernel_pid_t thread_getpid(void)
|
|||||||
return sched_active_thread->pid;
|
return sched_active_thread->pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
int thread_getstatus(kernel_pid_t pid)
|
volatile tcb_t *thread_get(kernel_pid_t pid)
|
||||||
{
|
{
|
||||||
if (sched_threads[pid] == NULL) {
|
if ((pid != KERNEL_PID_UNDEF) && (0 <= pid) && (pid < MAXTHREADS)) {
|
||||||
return STATUS_NOT_FOUND;
|
return sched_threads[pid];
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sched_threads[pid]->status;
|
int thread_getstatus(kernel_pid_t pid)
|
||||||
|
{
|
||||||
|
volatile tcb_t *t = thread_get(pid);
|
||||||
|
return t ? t->status : STATUS_NOT_FOUND;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *thread_getname(kernel_pid_t pid)
|
const char *thread_getname(kernel_pid_t pid)
|
||||||
{
|
{
|
||||||
if (sched_threads[pid] == NULL) {
|
volatile tcb_t *t = thread_get(pid);
|
||||||
return NULL;
|
return t ? t->name : NULL;
|
||||||
}
|
|
||||||
|
|
||||||
return sched_threads[pid]->name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void thread_sleep(void)
|
void thread_sleep(void)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user