Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/thread: error in thread.c corrected #21253

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/include/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ struct _thread {
*
* @return PID of newly created task on success
* @return -EINVAL, if @p priority is greater than or equal to
* @ref SCHED_PRIO_LEVELS
* @ref SCHED_PRIO_LEVELS or
* @p stacksize is too small
* @return -EOVERFLOW, if there are too many threads running already
*/
kernel_pid_t thread_create(char *stack,
Expand Down
31 changes: 28 additions & 3 deletions core/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ int thread_wakeup(kernel_pid_t pid)
else if (thread->status == STATUS_SLEEPING) {
DEBUG("thread_wakeup: Thread is sleeping.\n");

sched_set_status(thread, STATUS_RUNNING);
sched_set_status(thread, STATUS_PENDING);

irq_restore(old_state);
sched_switch(thread->priority);
Expand Down Expand Up @@ -160,6 +160,29 @@ void thread_yield(void)
thread_yield_higher();
}

/*
* Example of insertion operations in the linked list
* thread_add_to_list(list,new_node) //Prio4
* list list new_node
* +------+ +------+ +------+
* | + | -> | + | | 4 + |
* +----|-+ +----|-+ +----|-+
* +-->NULL +-----/\ +-->NULL
* thread_add_to_list(list,higher_node) //Prio2(Higher)
* list new_node higher_node
* +------+ +------+ +------+
* | + | | 4 + | | 2 + |
* +----|-+ +----|-+ +----|-+
* | +-->NULL |
* | /\-------------+
* +----------------/\
* thread_add_to_list(list,lower_node) //Prio6(Lower)
* list new_node lower_node
* +------+ +------+ +------+
* | + | | 4 + | | 6 + |
* +----|-+ +----|-+ +----|-+
* +-----/\ +-----/\ +-->NULL
*/
void thread_add_to_list(list_node_t *list, thread_t *thread)
{
assert(thread->status < STATUS_ON_RUNQUEUE);
Expand Down Expand Up @@ -240,6 +263,7 @@ kernel_pid_t thread_create(char *stack, int stacksize, uint8_t priority,

if (stacksize < 0) {
DEBUG("thread_create: stacksize is too small!\n");
return -EINVAL;
}
/* allocate our thread control block at the top of our stackspace. Cast to
* (uintptr_t) intermediately to silence -Wcast-align. (We manually made
Expand Down Expand Up @@ -372,10 +396,11 @@ static const char *state_names[STATUS_NUMOF] = {

const char *thread_state_to_string(thread_status_t state)
{
const char *name = state_names[state] ? state_names[state] : NULL;
assert(state < STATUS_NUMOF);
const char *name = state_names[state];
Comment on lines +399 to +400
Copy link
Contributor

@kfessel kfessel Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is more reliable -- check for not exceeding NUMOF even if assertion are disabled

Suggested change
assert(state < STATUS_NUMOF);
const char *name = state_names[state];
const char *name = NULL;
if (state < STATUS_NUMOF){
name = state_names[state];
}
/* if compiling with assertions, this is an error that indicates that the table above is incomplete */
assert(name != NULL)
return (name != NULL) ? name : STATE_NAME_UNKNOWN;

Copy link
Contributor

@kfessel kfessel Feb 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i could not add a suggestion to the unchanged code assert(name != NULL); -> i attached it to the first two lines


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;
return ((state < STATUS_NUMOF) && (name != NULL)) ? name : STATE_NAME_UNKNOWN;
}