summaryrefslogtreecommitdiff
path: root/src/include
diff options
context:
space:
mode:
authorRaul E Rangel <rrangel@chromium.org>2021-07-12 13:43:48 -0600
committerRaul Rangel <rrangel@chromium.org>2021-07-18 15:15:45 +0000
commitcc01da50b72e34859b5507ed0f4ae367da4ffaea (patch)
tree7d28755d558bad965b6409af1bc6bf248511e52a /src/include
parent3af732ada0b4bcc2dce3a982f83dc4632f75f784 (diff)
lib/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 thread_join 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 Reviewed-on: https://review.coreboot.org/c/coreboot/+/56229 Reviewed-by: Karthik Ramasubramanian <kramasub@google.com> Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Diffstat (limited to 'src/include')
-rw-r--r--src/include/thread.h41
1 files changed, 32 insertions, 9 deletions
diff --git a/src/include/thread.h b/src/include/thread.h
index 4bc04db00a..f9591c949f 100644
--- a/src/include/thread.h
+++ b/src/include/thread.h
@@ -2,14 +2,30 @@
#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>
struct thread_mutex {
bool locked;
};
+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;
+};
+
+/* Waits until the thread has terminated and returns the error code */
+enum cb_err thread_join(struct thread_handle *handle);
+
#if ENV_RAMSTAGE && CONFIG(COOP_MULTITASKING)
struct thread {
@@ -17,9 +33,10 @@ struct thread {
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);
@@ -30,12 +47,14 @@ void threads_initialize(void);
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, < 0 when thread did not yield. */
@@ -74,9 +93,13 @@ void arch_prepare_thread(struct thread *t,
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;
}