diff --git a/Common/Thread/Channel.h b/Common/Thread/Channel.h index 52f7601f36..9ccbbd7096 100644 --- a/Common/Thread/Channel.h +++ b/Common/Thread/Channel.h @@ -9,6 +9,7 @@ // well as a simple blocking mailbox. Let's see if we get there. // Single item mailbox. +// T is copyable. Often T will itself just be a pointer or smart pointer of some sort. template struct Mailbox { Mailbox() : refcount_(1) {} @@ -18,9 +19,9 @@ struct Mailbox { std::mutex mutex_; std::condition_variable condvar_; - T *data_ = nullptr; + T data_ = nullptr; - T *Wait() { + T Wait() { std::unique_lock lock(mutex_); while (!data_) { condvar_.wait(lock); @@ -28,7 +29,7 @@ struct Mailbox { return data_; } - bool Poll(T **data) { + bool Poll(T *data) { std::unique_lock lock(mutex_); if (data_) { *data = data_; @@ -38,7 +39,7 @@ struct Mailbox { } } - bool Send(T *data) { + bool Send(T data) { std::unique_lock lock(mutex_); if (!data_) { data_ = data; diff --git a/Common/Thread/Promise.h b/Common/Thread/Promise.h index e6fc471141..f69bb791e9 100644 --- a/Common/Thread/Promise.h +++ b/Common/Thread/Promise.h @@ -9,7 +9,7 @@ template class PromiseTask : public Task { public: - PromiseTask(std::function fun, Mailbox *tx) : fun_(fun), tx_(tx) { + PromiseTask(std::function fun, Mailbox *tx) : fun_(fun), tx_(tx) { tx_->AddRef(); } ~PromiseTask() { @@ -17,11 +17,11 @@ public: } void Run() override { - T *value = fun_(); + T value = fun_(); tx_->Send(value); } - std::function fun_; + std::function fun_; Mailbox *tx_; }; @@ -32,7 +32,7 @@ public: template class Promise { public: - static Promise *Spawn(ThreadManager *threadman, std::function fun, TaskType taskType) { + static Promise *Spawn(ThreadManager *threadman, std::function fun, TaskType taskType) { Mailbox *mailbox = new Mailbox(); Promise *promise = new Promise(); @@ -50,8 +50,8 @@ public: delete data_; } - // Returns *T if the data is ready, nullptr if it's not. - T *Poll() { + // Returns T if the data is ready, nullptr if it's not. + T Poll() { if (ready_) { return data_; } else { @@ -66,7 +66,7 @@ public: } } - T *BlockUntilReady() { + T BlockUntilReady() { if (ready_) { return data_; } else { @@ -81,7 +81,7 @@ public: private: Promise() {} - T *data_ = nullptr; + T data_ = nullptr; bool ready_ = false; Mailbox *rx_; }; diff --git a/UI/MemStickScreen.cpp b/UI/MemStickScreen.cpp index ef617c5b5c..91826ea40d 100644 --- a/UI/MemStickScreen.cpp +++ b/UI/MemStickScreen.cpp @@ -645,7 +645,7 @@ UI::EventReturn ConfirmMemstickMoveScreen::OnConfirm(UI::EventParams ¶ms) { if (moveData_) { progressReporter_.Set(iz->T("Starting move...")); - moveDataTask_ = Promise::Spawn(&g_threadManager, [&]() -> MoveResult * { + moveDataTask_ = Promise::Spawn(&g_threadManager, [&]() -> MoveResult * { Path moveSrc = g_Config.memStickDirectory; Path moveDest = newMemstickFolder_; if (moveSrc.GetFilename() != "PSP") { diff --git a/UI/MemStickScreen.h b/UI/MemStickScreen.h index 7434b12855..f315467c56 100644 --- a/UI/MemStickScreen.h +++ b/UI/MemStickScreen.h @@ -130,7 +130,7 @@ private: ProgressReporter progressReporter_; UI::TextView *progressView_ = nullptr; - Promise *moveDataTask_ = nullptr; + Promise *moveDataTask_ = nullptr; std::string error_; }; diff --git a/unittest/TestThreadManager.cpp b/unittest/TestThreadManager.cpp index 283d6c1947..94215632da 100644 --- a/unittest/TestThreadManager.cpp +++ b/unittest/TestThreadManager.cpp @@ -17,7 +17,7 @@ ResultObject *ResultProducer() { } bool TestMailbox() { - Mailbox *mailbox = new Mailbox(); + Mailbox *mailbox = new Mailbox(); mailbox->Send(new ResultObject{ true }); ResultObject *data; data = mailbox->Wait(); @@ -60,7 +60,7 @@ bool TestThreadManager() { ThreadManager manager; manager.Init(8, 1); - Promise *object(Promise::Spawn(&manager, &ResultProducer, TaskType::IO_BLOCKING)); + Promise *object(Promise::Spawn(&manager, &ResultProducer, TaskType::IO_BLOCKING)); if (!TestParallelLoop(&manager)) { return false;