2020-11-27 23:12:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-06-13 17:51:51 +00:00
|
|
|
#include <atomic>
|
2020-11-27 23:12:06 +00:00
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
2021-06-12 19:06:59 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
// Named Channel.h because I originally intended to support a multi item channel as
|
|
|
|
// well as a simple blocking mailbox. Let's see if we get there.
|
2020-11-27 23:12:06 +00:00
|
|
|
|
|
|
|
// Single item mailbox.
|
2021-11-20 21:40:10 +00:00
|
|
|
// T is copyable. Often T will itself just be a pointer or smart pointer of some sort.
|
2020-11-27 23:12:06 +00:00
|
|
|
template<class T>
|
|
|
|
struct Mailbox {
|
|
|
|
Mailbox() : refcount_(1) {}
|
2021-06-12 19:06:59 +00:00
|
|
|
~Mailbox() {
|
|
|
|
assert(refcount_ == 0);
|
|
|
|
}
|
2020-11-27 23:12:06 +00:00
|
|
|
|
|
|
|
std::mutex mutex_;
|
|
|
|
std::condition_variable condvar_;
|
2022-06-12 07:55:41 +00:00
|
|
|
T data_{};
|
2022-11-07 21:25:45 +00:00
|
|
|
bool dataReceived_ = false;
|
2020-11-27 23:12:06 +00:00
|
|
|
|
2021-11-20 21:40:10 +00:00
|
|
|
T Wait() {
|
2020-12-01 10:56:19 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
2023-03-15 21:26:23 +00:00
|
|
|
condvar_.wait(lock, [&] {return dataReceived_;});
|
2020-12-01 10:56:19 +00:00
|
|
|
return data_;
|
2020-11-27 23:12:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-20 21:40:10 +00:00
|
|
|
bool Poll(T *data) {
|
2020-12-01 10:56:19 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
2022-11-07 21:25:45 +00:00
|
|
|
if (dataReceived_) {
|
2020-12-01 10:56:19 +00:00
|
|
|
*data = data_;
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2020-11-27 23:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-20 21:40:10 +00:00
|
|
|
bool Send(T data) {
|
2020-12-01 10:56:19 +00:00
|
|
|
std::unique_lock<std::mutex> lock(mutex_);
|
2022-11-07 21:25:45 +00:00
|
|
|
if (!dataReceived_) {
|
2020-12-01 10:56:19 +00:00
|
|
|
data_ = data;
|
2022-11-07 21:25:45 +00:00
|
|
|
dataReceived_ = true;
|
2022-06-12 11:22:26 +00:00
|
|
|
condvar_.notify_all();
|
2020-12-01 10:56:19 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// Already has value.
|
|
|
|
return false;
|
2020-11-27 23:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRef() {
|
|
|
|
refcount_.fetch_add(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Release() {
|
|
|
|
int count = refcount_.fetch_sub(1);
|
|
|
|
if (count == 1) { // was definitely decreased to 0
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::atomic<int> refcount_;
|
|
|
|
};
|