[Linux/Threading] Self-suspend with condition variable instead of spin

This commit is contained in:
Herman S.
2025-12-25 12:08:05 +09:00
parent 9f8961c185
commit f45a254774
3 changed files with 51 additions and 21 deletions

View File

@@ -15,6 +15,10 @@
#include <string>
#include "xenia/base/mutex.h"
#if XE_PLATFORM_LINUX
#include <condition_variable>
#include <mutex>
#endif
#include "xenia/base/threading.h"
#include "xenia/cpu/thread.h"
#include "xenia/cpu/thread_state.h"
@@ -426,6 +430,13 @@ class XThread : public XObject, public cpu::Thread {
X_STATUS Delay(uint32_t processor_mode, uint32_t alertable,
uint64_t interval);
#if XE_PLATFORM_LINUX
// Performs self-suspension: increments suspend_count and blocks until
// another thread calls Resume() and suspend_count reaches 0.
// Returns the previous suspend_count value.
uint32_t SelfSuspend();
#endif
xe::threading::Thread* thread() { return thread_.get(); }
virtual bool Save(ByteStream* stream) override;
@@ -467,6 +478,12 @@ class XThread : public XObject, public cpu::Thread {
int32_t priority_ = 0;
#if XE_PLATFORM_LINUX
// Condition variable for thread self-suspension.
std::mutex suspend_mutex_;
std::condition_variable suspend_cv_;
#endif
// Reentry context for setjmp/longjmp based stack unwinding
std::jmp_buf reentry_jmp_buf_;
uint32_t reentry_address_ = 0;