diff --git a/core/include/thread.h b/core/include/thread.h index 4f2a1ed3ab..22733b654c 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -588,6 +588,34 @@ static inline int thread_has_msg_queue(const volatile struct _thread *thread) #endif } +/** + * Get a thread's status + * + * @param thread thread to work on + * @returns status of thread + */ +static inline thread_status_t thread_get_status(const thread_t *thread) { + return thread->status; +} + +/** + * Returns if a thread is active (currently running or waiting to be scheduled) + * + * @param thread thread to work on + * @returns true if thread is active, false otherwise + */ +static inline bool thread_is_active(const thread_t *thread) { + return thread->status >= STATUS_ON_RUNQUEUE; +} + +/** + * Convert a thread state code to a human readable string. + * + * @param state thread state to convert + * @returns ptr to string representation of thread state (or to "unknown") + */ +const char *thread_state_to_string(thread_status_t state); + #ifdef __cplusplus } #endif diff --git a/core/thread.c b/core/thread.c index a8172209a0..e3ae5ae8c4 100644 --- a/core/thread.c +++ b/core/thread.c @@ -318,3 +318,31 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority, return pid; } + +static const char *state_names[STATUS_NUMOF] = { + [STATUS_STOPPED] = "stopped", + [STATUS_ZOMBIE] = "zombie", + [STATUS_SLEEPING] = "sleeping", + [STATUS_MUTEX_BLOCKED] = "bl mutex", + [STATUS_RECEIVE_BLOCKED] = "bl rx", + [STATUS_SEND_BLOCKED] = "bl send", + [STATUS_REPLY_BLOCKED] = "bl reply", + [STATUS_FLAG_BLOCKED_ANY] = "bl anyfl", + [STATUS_FLAG_BLOCKED_ALL] = "bl allfl", + [STATUS_MBOX_BLOCKED] = "bl mbox", + [STATUS_COND_BLOCKED] = "bl cond", + [STATUS_RUNNING] = "running", + [STATUS_PENDING] = "pending", +}; + +#define STATE_NAME_UNKNOWN "unknown" + +const char *thread_state_to_string(thread_status_t state) +{ + const char *name = state_names[state] ? state_names[state] : NULL; + + assert(name != NULL); /* if compiling with assertions, this is an error that + indicates that the table above is incomplete */ + + return (name != NULL) ? name : STATE_NAME_UNKNOWN; +} diff --git a/sys/ps/ps.c b/sys/ps/ps.c index 808945346a..6df9ce913b 100644 --- a/sys/ps/ps.c +++ b/sys/ps/ps.c @@ -32,51 +32,11 @@ #include "tlsf-malloc.h" #endif -/* list of states copied from tcb.h */ -static const char *state_names[STATUS_NUMOF] = { - [STATUS_STOPPED] = "stopped", - [STATUS_ZOMBIE] = "zombie", - [STATUS_SLEEPING] = "sleeping", - [STATUS_MUTEX_BLOCKED] = "bl mutex", - [STATUS_RECEIVE_BLOCKED] = "bl rx", - [STATUS_SEND_BLOCKED] = "bl send", - [STATUS_REPLY_BLOCKED] = "bl reply", - [STATUS_FLAG_BLOCKED_ANY] = "bl anyfl", - [STATUS_FLAG_BLOCKED_ALL] = "bl allfl", - [STATUS_MBOX_BLOCKED] = "bl mbox", - [STATUS_COND_BLOCKED] = "bl cond", - [STATUS_RUNNING] = "running", - [STATUS_PENDING] = "pending", -}; - -#define STATE_NAME_UNKNOWN "unknown" - -/** - * Convert a thread state code to a human readable string. - * - * This function should be used instead of a direct array lookup: if ever - * state_names and the actual states in tcb.h get out of sync, a hole will be - * left in the lookup table. If compiling with NDEBUG not defined, this will - * generate an assertion which should make it clear that the table needs - * updating. With NDEBUG, any missing code will result in the string "unknown" - * (while direct access would return a NULL, possibly causing a crash.) - */ -static const char *state_to_string(thread_status_t state) -{ - const char *name = state_names[state] ? state_names[state] : NULL; - - assert(name != NULL); /* if compiling with assertions, this is an error that - indicates that the table above is incomplete */ - - return (name != NULL) ? name : STATE_NAME_UNKNOWN; -} - /** * @brief Prints a list of running threads including stack usage to stdout. */ void ps(void) { - const char queued_name[] = {'_', 'Q'}; #ifdef DEVELHELP int overall_stacksz = 0, overall_used = 0; #endif @@ -126,9 +86,9 @@ void ps(void) thread_t *p = thread_get(i); if (p != NULL) { - thread_status_t state = p->status; /* copy state */ - const char *sname = state_to_string(state); /* get state name */ - const char *queued = &queued_name[(int)(state >= STATUS_ON_RUNQUEUE)]; /* get queued flag */ + thread_status_t state = thread_get_status(p); /* copy state */ + const char *sname = thread_state_to_string(state); /* get state name */ + const char *queued = thread_is_active(p) ? "Q" : "_"; /* get queued flag */ #ifdef DEVELHELP int stacksz = p->stack_size; /* get stack size */ overall_stacksz += stacksz;