Raul Rangel has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/56229 )
Change subject: thread: Add thread_handle ......................................................................
thread: Add thread_handle
The thread_handle can be used to wait for a thread to exit. I also added a return value to the thread function that will be stored on the handle after it completes. This makes it easy for the callers to check if the thread completed successfully or had an error. The wait_for_thread method uses the handle to block until the thread completes.
BUG=b:179699789 TEST=See thread_handle state update and see error code set correctly.
Signed-off-by: Raul E Rangel rrangel@chromium.org Change-Id: Ie6f64d0c5a5acad4431a605f0b0b5100dc5358ff --- M src/include/thread.h M src/lib/thread.c 2 files changed, 87 insertions(+), 24 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/29/56229/1
diff --git a/src/include/thread.h b/src/include/thread.h index da8ed72..902a7f7 100644 --- a/src/include/thread.h +++ b/src/include/thread.h @@ -2,9 +2,22 @@ #ifndef THREAD_H_ #define THREAD_H_
-#include <stdint.h> -#include <bootstate.h> #include <arch/cpu.h> +#include <bootstate.h> +#include <commonlib/bsd/cb_err.h> +#include <stdint.h> + +enum thread_state { + THREAD_UNINITIALIZED, + THREAD_STARTED, + THREAD_DONE, +}; + +struct thread_handle { + enum thread_state state; + /* Only valid when state == THREAD_DONE */ + enum cb_err error; +};
#if ENV_RAMSTAGE && CONFIG(COOP_MULTITASKING)
@@ -13,9 +26,10 @@ uintptr_t stack_current; uintptr_t stack_orig; struct thread *next; - void (*entry)(void *); + enum cb_err (*entry)(void *); void *entry_arg; int can_yield; + struct thread_handle *handle; };
void threads_initialize(void); @@ -26,12 +40,14 @@ void *arch_get_thread_stackbase(void); /* Run func(arrg) on a new thread. Return 0 on successful start of thread, < 0 * when thread could not be started. Note that the thread will block the - * current state in the boot state machine until it is complete. */ -int thread_run(void (*func)(void *), void *arg); + * current state in the boot state machine until it is complete. The thread + * handle if populated, will reflect the state and return code of the thread. + */ +int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg); /* thread_run_until is the same as thread_run() except that it blocks state * transitions from occurring in the (state, seq) pair of the boot state * machine. */ -int thread_run_until(void (*func)(void *), void *arg, +int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg, boot_state_t state, boot_state_sequence_t seq); /* Return 0 on successful yield for the given amount of time, < 0 when thread * did not yield. */ @@ -44,6 +60,9 @@ void thread_cooperate(void); void thread_prevent_coop(void);
+/* Wait's until the thread has terminated and returns the error code */ +enum cb_err wait_for_thread(struct thread_handle *handle); + static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { ci->thread = NULL; @@ -58,9 +77,13 @@ asmlinkage void (*thread_entry)(void *), void *arg); #else static inline void threads_initialize(void) {} -static inline int thread_run(void (*func)(void *), void *arg) { return -1; } -static inline int thread_run_until(void (*func)(void *), void *arg, boot_state_t state, - boot_state_sequence_t seq) +static inline int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), + void *arg) +{ + return -1; +} +static inline int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), + void *arg, boot_state_t state, boot_state_sequence_t seq) { return -1; } @@ -70,6 +93,12 @@ } static inline void thread_cooperate(void) {} static inline void thread_prevent_coop(void) {} + +static inline enum cb_err wait_for_thread(struct thread_handle *handle) +{ + return CB_ERR; +} + struct cpu_info; static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { } #endif diff --git a/src/lib/thread.c b/src/lib/thread.c index e2280c6..671cb3e 100644 --- a/src/lib/thread.c +++ b/src/lib/thread.c @@ -127,11 +127,20 @@ /* current is still runnable. */ push_runnable(current); } + + if (t->handle) + t->handle->state = THREAD_STARTED; + switch_to_thread(t->stack_current, ¤t->stack_current); }
-static void terminate_thread(struct thread *t) +static void terminate_thread(struct thread *t, enum cb_err error) { + if (t->handle) { + t->handle->error = error; + t->handle->state = THREAD_DONE; + } + free_thread(t); schedule(NULL); } @@ -139,20 +148,23 @@ static void asmlinkage call_wrapper(void *unused) { struct thread *current = current_thread(); + enum cb_err error;
- current->entry(current->entry_arg); - terminate_thread(current); + error = current->entry(current->entry_arg); + + terminate_thread(current, error); }
/* Block the current state transitions until thread is complete. */ static void asmlinkage call_wrapper_block_current(void *unused) { struct thread *current = current_thread(); + enum cb_err error;
boot_state_current_block(); - current->entry(current->entry_arg); + error = current->entry(current->entry_arg); boot_state_current_unblock(); - terminate_thread(current); + terminate_thread(current, error); }
struct block_boot_state { @@ -165,18 +177,18 @@ { struct block_boot_state *bbs = arg; struct thread *current = current_thread(); + enum cb_err error;
boot_state_block(bbs->state, bbs->seq); - current->entry(current->entry_arg); + error = current->entry(current->entry_arg); boot_state_unblock(bbs->state, bbs->seq); - terminate_thread(current); + terminate_thread(current, error); }
/* Prepare a thread so that it starts by executing thread_entry(thread_arg). * Within thread_entry() it will call func(arg). */ -static void prepare_thread(struct thread *t, void *func, void *arg, - asmlinkage void (*thread_entry)(void *), - void *thread_arg) +static void prepare_thread(struct thread *t, struct thread_handle *handle, void *func, + void *arg, asmlinkage void (*thread_entry)(void *), void *thread_arg) { /* Stash the function and argument to run. */ t->entry = func; @@ -185,6 +197,9 @@ /* All new threads can yield by default. */ t->can_yield = 1;
+ /* Pointer used to publish the state of thread */ + t->handle = handle; + arch_prepare_thread(t, thread_entry, thread_arg); }
@@ -206,7 +221,7 @@ die("No threads available for idle thread!\n");
/* Queue idle thread to run once all other threads have yielded. */ - prepare_thread(t, idle_thread, NULL, call_wrapper, NULL); + prepare_thread(t, NULL, idle_thread, NULL, call_wrapper, NULL); push_runnable(t); /* Mark the currently executing thread to cooperate. */ thread_cooperate(); @@ -268,7 +283,7 @@ idle_thread_init(); }
-int thread_run(void (*func)(void *), void *arg) +int thread_run(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg) { struct thread *current; struct thread *t; @@ -288,13 +303,13 @@ return -1; }
- prepare_thread(t, func, arg, call_wrapper_block_current, NULL); + prepare_thread(t, handle, func, arg, call_wrapper_block_current, NULL); schedule(t);
return 0; }
-int thread_run_until(void (*func)(void *), void *arg, +int thread_run_until(struct thread_handle *handle, enum cb_err (*func)(void *), void *arg, boot_state_t state, boot_state_sequence_t seq) { struct thread *current; @@ -319,7 +334,7 @@ bbs = thread_alloc_space(t, sizeof(*bbs)); bbs->state = state; bbs->seq = seq; - prepare_thread(t, func, arg, call_wrapper_block_state, bbs); + prepare_thread(t, handle, func, arg, call_wrapper_block_state, bbs); schedule(t);
return 0; @@ -360,3 +375,22 @@ if (current != NULL) current->can_yield = 0; } + +enum cb_err wait_for_thread(struct thread_handle *handle) +{ + struct stopwatch sw; + + if (handle->state == THREAD_UNINITIALIZED) + return CB_ERR_ARG; + + stopwatch_init(&sw); + + printk(BIOS_DEBUG, "waiting for thread\n"); + + while (handle->state != THREAD_DONE) + thread_yield_microseconds(10); + + printk(BIOS_DEBUG, " took %lu us\n", stopwatch_duration_usecs(&sw)); + + return handle->error; +}