Raul Rangel has uploaded this change for review. ( https://review.coreboot.org/c/coreboot/+/56230 )
Change subject: thread: Add mutex ......................................................................
thread: Add mutex
We need a way to protect shared resources. Since we are using cooperative multitasking the mutex implementation is pretty trivial.
BUG=b:179699789 TEST=Verify thread locks and unlocks
Signed-off-by: Raul E Rangel rrangel@chromium.org Change-Id: Ife1ac95ec064ebcdd00fcaacec37a06ac52885ff --- M src/include/thread.h M src/lib/thread.c 2 files changed, 31 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/coreboot refs/changes/30/56230/1
diff --git a/src/include/thread.h b/src/include/thread.h index 902a7f7..9c68072 100644 --- a/src/include/thread.h +++ b/src/include/thread.h @@ -19,6 +19,10 @@ enum cb_err error; };
+struct mutex { + bool locked; +}; + #if ENV_RAMSTAGE && CONFIG(COOP_MULTITASKING)
struct thread { @@ -63,6 +67,9 @@ /* Wait's until the thread has terminated and returns the error code */ enum cb_err wait_for_thread(struct thread_handle *handle);
+void mutex_lock(struct mutex *mutex); +void mutex_unlock(struct mutex *mutex); + static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { ci->thread = NULL; @@ -101,6 +108,10 @@
struct cpu_info; static inline void thread_init_cpu_info_non_bsp(struct cpu_info *ci) { } + +static inline void mutex_lock(struct mutex *mutex) {} + +static inline void mutex_unlock(struct mutex *mutex) {} #endif
#endif /* THREAD_H_ */ diff --git a/src/lib/thread.c b/src/lib/thread.c index 671cb3e..f9a3fc6 100644 --- a/src/lib/thread.c +++ b/src/lib/thread.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: GPL-2.0-only */
+#include <assert.h> #include <stddef.h> #include <stdint.h> #include <stdlib.h> @@ -394,3 +395,22 @@
return handle->error; } + +void mutex_lock(struct mutex *mutex) +{ + struct stopwatch sw; + + stopwatch_init(&sw); + + while (mutex->locked) + thread_yield_microseconds(10); + mutex->locked = 1; + + printk(BIOS_DEBUG, " took %lu us to acquire mutex\n", stopwatch_duration_usecs(&sw)); +} + +void mutex_unlock(struct mutex *mutex) +{ + assert(mutex->locked); + mutex->locked = 0; +}