diff --git a/Common/Thread/Channel.h b/Common/Thread/Channel.h index 2e522caf11..b6b23fdebe 100644 --- a/Common/Thread/Channel.h +++ b/Common/Thread/Channel.h @@ -43,7 +43,7 @@ struct Mailbox { std::unique_lock lock(mutex_); if (!data_) { data_ = data; - condvar_.notify_one(); + condvar_.notify_all(); return true; } else { // Already has value. diff --git a/Common/Thread/Promise.h b/Common/Thread/Promise.h index 1e83d62564..4145b4b6c1 100644 --- a/Common/Thread/Promise.h +++ b/Common/Thread/Promise.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "Common/Log.h" #include "Common/Thread/Channel.h" @@ -63,7 +64,14 @@ public: return promise; } + // Allow an empty promise to spawn, too, in case we want to delay it. + void SpawnEmpty(ThreadManager *threadman, std::function fun, TaskType taskType) { + PromiseTask *task = new PromiseTask(fun, rx_, taskType); + threadman->EnqueueTask(task); + } + ~Promise() { + std::lock_guard guard(readyMutex_); // A promise should have been fulfilled before it's destroyed. _assert_(ready_); _assert_(!rx_); @@ -71,6 +79,7 @@ public: // Returns T if the data is ready, nullptr if it's not. T Poll() { + std::lock_guard guard(readyMutex_); if (ready_) { return data_; } else { @@ -86,6 +95,7 @@ public: } T BlockUntilReady() { + std::lock_guard guard(readyMutex_); if (ready_) { return data_; } else { @@ -108,5 +118,6 @@ private: // Promise can only be constructed in Spawn (or AlreadyDone). T data_{}; bool ready_ = false; + std::mutex readyMutex_; Mailbox *rx_ = nullptr; };