From ed5bbc1eebb66bc0f93cab877f36c93d05116e00 Mon Sep 17 00:00:00 2001 From: David Yat Sin Date: Mon, 11 Nov 2024 05:35:40 +0000 Subject: [PATCH] rocr: Fix sem_post overflow errors WaitSemaphore and PostSemaphore are used in the HybridMutex implementation. If HybridMutex did not have to call WaitSemaphore when acquired, then calling PostSemaphore would cause the internal count inside sem_t to slowly grow to large values and eventually cause overflow. Change-Id: I173fc17c874b49926e56991405e9086ea8c138fc [ROCm/ROCR-Runtime commit: f58aff630cae089a6d3315f715b741543f3e612b] --- .../runtime/hsa-runtime/core/util/lnx/os_linux.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp index 84021f15b0..0cc65b1ce6 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/util/lnx/os_linux.cpp @@ -369,6 +369,14 @@ bool WaitSemaphore(Semaphore sem) { } void PostSemaphore(Semaphore sem) { + int waitval = 1; + if (sem_getvalue(*(sem_t**)&sem, &waitval)) + assert(false && "Failed to get semaphore waiters"); + + /* sem_getvalue return <= 0 when there are threads blocked on sem_wait */ + if (waitval > 0) + return; + if (sem_post(*(sem_t**)&sem)) assert(false && "Failed to post semaphore"); }