From 1643f7b4cca88faa47d1fc7ea0fe7f77160076f8 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 2 Nov 2023 22:36:48 -0700 Subject: [PATCH] [Zion] Update FIXME in threading. --- zion/object/thread.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/zion/object/thread.cpp b/zion/object/thread.cpp index 286d731..f9c7629 100644 --- a/zion/object/thread.cpp +++ b/zion/object/thread.cpp @@ -69,7 +69,7 @@ void Thread::Exit() { #endif state_ = FINISHED; process_.CheckState(); - while (!blocked_threads_.size() == 0) { + while (blocked_threads_.size() != 0) { auto thread = blocked_threads_.PopFront(); thread->SetState(Thread::RUNNABLE); gScheduler->Enqueue(thread); @@ -78,7 +78,16 @@ void Thread::Exit() { } void Thread::Wait() { - // FIXME: We need synchronization code here. + // TODO: We need synchronization code here. + // Race condition is for A waiting on B. + // 1. A checks if B is finished. + // 2. Context Switch A -> B + // 3. B finishes. + // 4. Context Switch B -> A + // 5. A forever blocks on B. + if (state_ == Thread::FINISHED) { + return; + } auto thread = gScheduler->CurrentThread(); thread->SetState(Thread::BLOCKED); blocked_threads_.PushBack(thread);