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

Merge pull request #15506 from kaspar030/refactor_thread_info

core: refactor thread info
This commit is contained in:
benpicco 2020-11-26 13:01:09 +01:00 committed by GitHub
commit aabd5eaca5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 43 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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;