From 6c282aa1da11cd657aec8fef02d46c3940dc1beb Mon Sep 17 00:00:00 2001 From: junminlee2004 <62260557+junminlee2004@users.noreply.github.com> Date: Wed, 3 Jun 2026 15:26:34 -0400 Subject: [PATCH] kernel: fix recursive read-lock deadlock on PthreadRwlock (#4517) --- src/common/shared_first_mutex.h | 30 +++++++++++++++++++++ src/core/libraries/kernel/threads/pthread.h | 3 ++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/common/shared_first_mutex.h b/src/common/shared_first_mutex.h index fcf9d0c4f..dc4a2cac8 100644 --- a/src/common/shared_first_mutex.h +++ b/src/common/shared_first_mutex.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include @@ -26,6 +27,16 @@ public: return true; } + template + bool try_lock_until(const std::chrono::time_point& abs_time) { + std::unique_lock lock(mtx); + if (!cv.wait_until(lock, abs_time, [this]() { return !writer_active && readers == 0; })) { + return false; + } + writer_active = true; + return true; + } + void unlock() { std::lock_guard lock(mtx); writer_active = false; @@ -38,6 +49,25 @@ public: ++readers; } + bool try_lock_shared() { + std::lock_guard lock(mtx); + if (writer_active) { + return false; + } + ++readers; + return true; + } + + template + bool try_lock_shared_until(const std::chrono::time_point& abs_time) { + std::unique_lock lock(mtx); + if (!cv.wait_until(lock, abs_time, [this]() { return !writer_active; })) { + return false; + } + ++readers; + return true; + } + void unlock_shared() { std::lock_guard lock(mtx); if (--readers == 0) { diff --git a/src/core/libraries/kernel/threads/pthread.h b/src/core/libraries/kernel/threads/pthread.h index 0763c74a3..5f6c10721 100644 --- a/src/core/libraries/kernel/threads/pthread.h +++ b/src/core/libraries/kernel/threads/pthread.h @@ -10,6 +10,7 @@ #include #include "common/enum.h" +#include "common/shared_first_mutex.h" #include "core/libraries/kernel/sync/mutex.h" #include "core/libraries/kernel/sync/semaphore.h" #include "core/libraries/kernel/time.h" @@ -202,7 +203,7 @@ struct PthreadRwlockAttr { using PthreadRwlockAttrT = PthreadRwlockAttr*; struct PthreadRwlock { - std::shared_timed_mutex lock; + Common::SharedFirstMutex lock; Pthread* owner; int Wrlock(const OrbisKernelTimespec* abstime);