From 5f5538cc6864308885ea20f134a8656b4743c6ed Mon Sep 17 00:00:00 2001 From: Ivan Chikish Date: Thu, 20 Jul 2023 16:12:57 +0300 Subject: [PATCH] [orbis-kernel] Evf: don't use thread retval to store result --- orbis-kernel/include/orbis/thread/Thread.hpp | 6 +++++- orbis-kernel/src/evf.cpp | 15 +++++++-------- orbis-kernel/src/sys/sys_sce.cpp | 15 ++++----------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/orbis-kernel/include/orbis/thread/Thread.hpp b/orbis-kernel/include/orbis/thread/Thread.hpp index 1a0bb016c..6947e64ba 100644 --- a/orbis-kernel/include/orbis/thread/Thread.hpp +++ b/orbis-kernel/include/orbis/thread/Thread.hpp @@ -12,7 +12,6 @@ namespace orbis { struct Process; struct Thread { utils::shared_mutex mtx; - utils::shared_cv sync_cv; Process *tproc = nullptr; uint64_t retval[2]{}; void *context{}; @@ -28,6 +27,11 @@ struct Thread { ThreadState state = ThreadState::INACTIVE; std::thread handle; + // Used to wake up thread in sleep queue + utils::shared_cv sync_cv; + uint64_t evfResultPattern; + uint64_t evfIsCancelled; + // FIXME: implement thread destruction void incRef() {} void decRef() {} diff --git a/orbis-kernel/src/evf.cpp b/orbis-kernel/src/evf.cpp index ba035ab14..a39a7c1f2 100644 --- a/orbis-kernel/src/evf.cpp +++ b/orbis-kernel/src/evf.cpp @@ -29,16 +29,15 @@ orbis::ErrorCode orbis::EventFlag::wait(Thread *thread, std::uint8_t waitMode, *timeout = 0; }; - // Use retval to pass information between threads - thread->retval[0] = 0; // resultPattern - thread->retval[1] = 0; // isCanceled + thread->evfResultPattern = 0; + thread->evfIsCancelled = 0; std::unique_lock lock(queueMtx); while (true) { if (isDeleted) { return ErrorCode::ACCES; } - if (thread->retval[1]) { + if (thread->evfIsCancelled) { return ErrorCode::CANCELED; } @@ -49,7 +48,7 @@ orbis::ErrorCode orbis::EventFlag::wait(Thread *thread, std::uint8_t waitMode, waitingThread.test(patValue)) { auto resultValue = waitingThread.applyClear(patValue); value.store(resultValue, std::memory_order::relaxed); - thread->retval[0] = resultValue; + thread->evfResultPattern = resultValue; // Success break; } @@ -111,7 +110,7 @@ orbis::ErrorCode orbis::EventFlag::tryWait(Thread *thread, waitingThread.test(patValue)) { auto resultValue = waitingThread.applyClear(patValue); value.store(resultValue, std::memory_order::relaxed); - thread->retval[0] = resultValue; + thread->evfResultPattern = resultValue; return {}; } @@ -136,9 +135,9 @@ std::size_t orbis::EventFlag::notify(NotifyType type, std::uint64_t bits) { auto resultValue = thread->applyClear(patValue); patValue = resultValue; - thread->thread->retval[0] = resultValue; + thread->thread->evfResultPattern = resultValue; if (type == NotifyType::Cancel) { - thread->thread->retval[1] = true; + thread->thread->evfIsCancelled = 1; } // TODO: update thread state diff --git a/orbis-kernel/src/sys/sys_sce.cpp b/orbis-kernel/src/sys/sys_sce.cpp index 539d8dda2..76c5d2afc 100644 --- a/orbis-kernel/src/sys/sys_sce.cpp +++ b/orbis-kernel/src/sys/sys_sce.cpp @@ -207,23 +207,19 @@ orbis::SysResult orbis::sys_evf_wait(Thread *thread, sint id, } std::uint32_t resultTimeout{}; - std::uint64_t resultPattern{}; auto result = evf->wait(thread, mode, patternSet, pTimeout != nullptr ? &resultTimeout : nullptr); - resultPattern = thread->retval[0]; - ORBIS_LOG_NOTICE("sys_evf_wait wakeup", thread, resultPattern, resultTimeout); + ORBIS_LOG_NOTICE("sys_evf_wait wakeup", thread, thread->evfResultPattern); if (pPatternSet != nullptr) { - uwrite(pPatternSet, (uint64_t)resultPattern); + uwrite(pPatternSet, thread->evfResultPattern); } if (pTimeout != nullptr) { - uwrite(pTimeout, (uint32_t)resultTimeout); + uwrite(pTimeout, resultTimeout); } - thread->retval[0] = 0; - thread->retval[1] = 0; return result; } @@ -244,15 +240,12 @@ orbis::SysResult orbis::sys_evf_trywait(Thread *thread, sint id, return ErrorCode::SRCH; } - std::uint64_t resultPattern{}; auto result = evf->tryWait(thread, mode, patternSet); - resultPattern = thread->retval[0]; if (pPatternSet != nullptr) { - uwrite(pPatternSet, (uint64_t)resultPattern); + uwrite(pPatternSet, thread->evfResultPattern); } - thread->retval[0] = 0; return result; } orbis::SysResult orbis::sys_evf_set(Thread *thread, sint id, uint64_t value) {