Promise: Add SpawnEmpty (couldn't come up with a better name), fix bug with multiple waiters

This commit is contained in:
Henrik Rydgård committed 2022-09-03 15:21:08 +02:00
1 parent c06cf8efaa
commit 76bd2395bf
2 files changed
+12 -1

No files matched your search

+1 -1
View File
@@ -43,7 +43,7 @@ struct Mailbox {
std::unique_lock<std::mutex> lock(mutex_);
if (!data_) {
data_ = data;
condvar_.notify_one();
condvar_.notify_all();
return true;
} else {
// Already has value.
+11
View File
@@ -1,6 +1,7 @@
#pragma once
#include <functional>
#include <mutex>
#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<T()> fun, TaskType taskType) {
PromiseTask<T> *task = new PromiseTask<T>(fun, rx_, taskType);
threadman->EnqueueTask(task);
}
~Promise() {
std::lock_guard<std::mutex> 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<std::mutex> guard(readyMutex_);
if (ready_) {
return data_;
} else {
@@ -86,6 +95,7 @@ public:
}
T BlockUntilReady() {
std::lock_guard<std::mutex> 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<T> *rx_ = nullptr;
};