From 5321bcde893a36bf66b6f834f34a5a9e29242486 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 25 Nov 2020 14:34:03 +0100 Subject: [PATCH 1/5] core: thread: move thread_state_to_string() from ps.c --- core/include/thread.h | 8 ++++++++ core/thread.c | 28 ++++++++++++++++++++++++++++ sys/ps/ps.c | 41 +---------------------------------------- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/core/include/thread.h b/core/include/thread.h index 4f2a1ed3ab..0987e621d8 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -588,6 +588,14 @@ static inline int thread_has_msg_queue(const volatile struct _thread *thread) #endif } +/** + * 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..8208555c85 100644 --- a/sys/ps/ps.c +++ b/sys/ps/ps.c @@ -32,45 +32,6 @@ #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. */ @@ -127,7 +88,7 @@ void ps(void) if (p != NULL) { thread_status_t state = p->status; /* copy state */ - const char *sname = state_to_string(state); /* get state name */ + const char *sname = thread_state_to_string(state); /* get state name */ const char *queued = &queued_name[(int)(state >= STATUS_ON_RUNQUEUE)]; /* get queued flag */ #ifdef DEVELHELP int stacksz = p->stack_size; /* get stack size */ From 017e9e9d4dca9a02d5d293d76f82c32e06f008ed Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 25 Nov 2020 14:39:22 +0100 Subject: [PATCH 2/5] core/thread: add thread_get_status() --- core/include/thread.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/include/thread.h b/core/include/thread.h index 0987e621d8..3dfc5513ae 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -588,6 +588,16 @@ 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; +} + /** * Convert a thread state code to a human readable string. * From 848a5a4e7a9f132b3ad3e7ece08d8c744cd852dc Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 25 Nov 2020 14:39:47 +0100 Subject: [PATCH 3/5] sys/ps: make use of thread_get_status() --- sys/ps/ps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/ps/ps.c b/sys/ps/ps.c index 8208555c85..d8baa6994e 100644 --- a/sys/ps/ps.c +++ b/sys/ps/ps.c @@ -87,7 +87,7 @@ void ps(void) thread_t *p = thread_get(i); if (p != NULL) { - thread_status_t state = p->status; /* copy state */ + thread_status_t state = thread_get_status(p); /* copy state */ const char *sname = thread_state_to_string(state); /* get state name */ const char *queued = &queued_name[(int)(state >= STATUS_ON_RUNQUEUE)]; /* get queued flag */ #ifdef DEVELHELP From ceea0ba9492220b07609a98269cb352222468e77 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 25 Nov 2020 14:46:43 +0100 Subject: [PATCH 4/5] core/thread: add thread_is_active() --- core/include/thread.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/include/thread.h b/core/include/thread.h index 3dfc5513ae..22733b654c 100644 --- a/core/include/thread.h +++ b/core/include/thread.h @@ -598,6 +598,16 @@ 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. * From 7e94050bbed001c6c8b352971c2d2f87c64eab41 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Wed, 25 Nov 2020 14:47:03 +0100 Subject: [PATCH 5/5] sys/ps: make use of thread_is_active() --- sys/ps/ps.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/sys/ps/ps.c b/sys/ps/ps.c index d8baa6994e..6df9ce913b 100644 --- a/sys/ps/ps.c +++ b/sys/ps/ps.c @@ -37,7 +37,6 @@ */ void ps(void) { - const char queued_name[] = {'_', 'Q'}; #ifdef DEVELHELP int overall_stacksz = 0, overall_used = 0; #endif @@ -89,7 +88,7 @@ void ps(void) if (p != NULL) { thread_status_t state = thread_get_status(p); /* copy state */ const char *sname = thread_state_to_string(state); /* get state name */ - const char *queued = &queued_name[(int)(state >= STATUS_ON_RUNQUEUE)]; /* get queued flag */ + const char *queued = thread_is_active(p) ? "Q" : "_"; /* get queued flag */ #ifdef DEVELHELP int stacksz = p->stack_size; /* get stack size */ overall_stacksz += stacksz;