From 1ac831d4b13f3596843e5303f5bbed967b206455 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 6 Dec 2023 16:13:07 +0200 Subject: [PATCH] Kernel: Add API to block on semaphore until unblock or EINTR --- kernel/include/kernel/Thread.h | 3 +++ kernel/kernel/Thread.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/kernel/include/kernel/Thread.h b/kernel/include/kernel/Thread.h index 30b3527bc8..7ae32ec15e 100644 --- a/kernel/include/kernel/Thread.h +++ b/kernel/include/kernel/Thread.h @@ -47,6 +47,9 @@ namespace Kernel void handle_signal(int signal = 0); bool add_signal(int signal); + // blocks semaphore and returns either on unblock, eintr or spuriously + [[nodiscard]] bool block_or_eintr(Semaphore&); + void set_return_rsp(uintptr_t& rsp) { m_return_rsp = &rsp; } void set_return_rip(uintptr_t& rip) { m_return_rip = &rip; } uintptr_t return_rsp() { ASSERT(m_return_rsp); return *m_return_rsp; } diff --git a/kernel/kernel/Thread.cpp b/kernel/kernel/Thread.cpp index 8ff9670918..01acffcfdd 100644 --- a/kernel/kernel/Thread.cpp +++ b/kernel/kernel/Thread.cpp @@ -330,6 +330,14 @@ namespace Kernel return false; } + bool Thread::block_or_eintr(Semaphore& semaphore) + { + if (is_interrupted_by_signal()) + return true; + semaphore.block(); + return is_interrupted_by_signal(); + } + void Thread::validate_stack() const { if (stack_base() <= m_rsp && m_rsp <= stack_base() + stack_size())