diff --git a/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp b/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp index 860c4505c7..3117d8a7b3 100644 --- a/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp +++ b/Common/Cpp/Concurrency/Backends/Thread_Qt.tpp @@ -21,7 +21,7 @@ namespace PokemonAutomation{ // PIMPL implementation - hides std::thread from header struct Thread::Data : public QThread{ - Data(std::function&& function) + Data(MoveOnlyFunction&& function) : m_function(std::move(function)) // , m_finished(false) { @@ -69,7 +69,7 @@ struct Thread::Data : public QThread{ // cout << "exiting: " << this << endl; } - std::function m_function; + MoveOnlyFunction m_function; std::atomic m_finished; }; @@ -95,7 +95,7 @@ Thread::Thread() = default; // Main constructor - starts thread immediately with given function // - Uses CONSTRUCT_TOKEN to construct Pimpl with Data constructor // - Thread starts running immediately (same as std::thread behavior) -Thread::Thread(std::function&& function) +Thread::Thread(MoveOnlyFunction&& function) : m_data(CONSTRUCT_TOKEN, std::move(function)) {} diff --git a/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp b/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp index b3e42146f8..9fb090098a 100644 --- a/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp +++ b/Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp @@ -19,7 +19,7 @@ namespace PokemonAutomation{ struct Thread::Data{ std::thread m_thread; - Data(std::function&& function) + Data(MoveOnlyFunction&& function) : m_thread(std::move(function)) {} }; @@ -45,7 +45,7 @@ Thread::Thread() = default; // Main constructor - starts thread immediately with given function // - Uses CONSTRUCT_TOKEN to construct Pimpl with Data constructor // - Thread starts running immediately (same as std::thread behavior) -Thread::Thread(std::function&& function) +Thread::Thread(MoveOnlyFunction&& function) : m_data(CONSTRUCT_TOKEN, std::move(function)) {} diff --git a/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp b/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp index 4f78e38949..02c3bcd8fb 100644 --- a/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp +++ b/Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp @@ -25,10 +25,10 @@ struct Thread::Data{ std::thread m_thread; - Data(std::function&& function) + Data(MoveOnlyFunction&& function) : m_stopped(false) , m_thread( - [this, function = std::move(function)]{ + [this, function = std::move(function)]() mutable{ function(); // std::lock_guard lg(m_lock); // m_stopped = true; @@ -60,7 +60,7 @@ Thread::Thread() = default; // Main constructor - starts thread immediately with given function // - Uses CONSTRUCT_TOKEN to construct Pimpl with Data constructor // - Thread starts running immediately (same as std::thread behavior) -Thread::Thread(std::function&& function) +Thread::Thread(MoveOnlyFunction&& function) : m_data(CONSTRUCT_TOKEN, std::move(function)) {} diff --git a/Common/Cpp/Concurrency/FireForgetDispatcher.cpp b/Common/Cpp/Concurrency/FireForgetDispatcher.cpp index 10e1a8a0cb..df021077c3 100644 --- a/Common/Cpp/Concurrency/FireForgetDispatcher.cpp +++ b/Common/Cpp/Concurrency/FireForgetDispatcher.cpp @@ -38,7 +38,7 @@ FireForgetDispatcher::~FireForgetDispatcher(){ stop(); } -void FireForgetDispatcher::dispatch(std::function&& func){ +void FireForgetDispatcher::dispatch(MoveOnlyFunction&& func){ std::lock_guard lg(m_lock); m_queue.emplace_back(std::move(func)); m_cv.notify_one(); @@ -58,7 +58,7 @@ void FireForgetDispatcher::dispatch(std::function&& func){ void FireForgetDispatcher::thread_loop(){ while (true){ - std::function task; + MoveOnlyFunction task; { std::unique_lock lg(m_lock); if (m_stopping){ diff --git a/Common/Cpp/Concurrency/FireForgetDispatcher.h b/Common/Cpp/Concurrency/FireForgetDispatcher.h index 3364284ad7..cb18cc49bc 100644 --- a/Common/Cpp/Concurrency/FireForgetDispatcher.h +++ b/Common/Cpp/Concurrency/FireForgetDispatcher.h @@ -8,7 +8,7 @@ #define PokemonAutomation_FireForgetDispatcher_H #include -#include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Mutex.h" #include "ConditionVariable.h" #include "Thread.h" @@ -23,7 +23,7 @@ class FireForgetDispatcher{ // Dispatch the specified task and return a handle to it. // Call "handle->wait()" to wait for the task to finish. - void dispatch(std::function&& func); + void dispatch(MoveOnlyFunction&& func); void stop(); @@ -32,7 +32,7 @@ class FireForgetDispatcher{ void thread_loop(); private: - std::deque> m_queue; + std::deque> m_queue; Thread m_thread; bool m_stopping; Mutex m_lock; diff --git a/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp b/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp index 488d95bac7..a3378a67e4 100644 --- a/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp +++ b/Common/Cpp/Concurrency/ScheduledTaskRunner.cpp @@ -52,7 +52,7 @@ WallClock ScheduledTaskRunner::next_event() const{ } return m_schedule.begin()->first; } -void ScheduledTaskRunner::add_event(WallClock time, std::function callback){ +void ScheduledTaskRunner::add_event(WallClock time, MoveOnlyFunction callback){ std::lock_guard lg(m_lock); if (m_stopped){ return; @@ -60,7 +60,7 @@ void ScheduledTaskRunner::add_event(WallClock time, std::function callba m_schedule.emplace(time, std::move(callback)); m_cv.notify_all(); } -void ScheduledTaskRunner::add_event(std::chrono::milliseconds time_from_now, std::function callback){ +void ScheduledTaskRunner::add_event(std::chrono::milliseconds time_from_now, MoveOnlyFunction callback){ std::lock_guard lg(m_lock); if (m_stopped){ return; diff --git a/Common/Cpp/Concurrency/ScheduledTaskRunner.h b/Common/Cpp/Concurrency/ScheduledTaskRunner.h index 45b4bc19a0..c7b10f7dae 100644 --- a/Common/Cpp/Concurrency/ScheduledTaskRunner.h +++ b/Common/Cpp/Concurrency/ScheduledTaskRunner.h @@ -10,6 +10,7 @@ #define PokemonAutomation_ScheduledTaskRunner_H #include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Time.h" //#include "Common/Cpp/CancellableScope.h" #include "Common/Cpp/Concurrency/Mutex.h" @@ -33,8 +34,8 @@ class ScheduledTaskRunner{ // events are scheduled. WallClock next_event() const; - void add_event(WallClock time, std::function callback); - void add_event(std::chrono::milliseconds time_from_now, std::function callback); + void add_event(WallClock time, MoveOnlyFunction callback); + void add_event(std::chrono::milliseconds time_from_now, MoveOnlyFunction callback); // virtual bool cancel(std::exception_ptr exception) noexcept override; @@ -46,7 +47,7 @@ class ScheduledTaskRunner{ ConditionVariable m_cv; bool m_stopped; - std::multimap> m_schedule; + std::multimap> m_schedule; AsyncTask m_runner; }; diff --git a/Common/Cpp/Concurrency/Thread.h b/Common/Cpp/Concurrency/Thread.h index 759d8b3707..97bede7d39 100644 --- a/Common/Cpp/Concurrency/Thread.h +++ b/Common/Cpp/Concurrency/Thread.h @@ -21,7 +21,7 @@ #ifndef PokemonAutomation_Concurrency_Thread_H #define PokemonAutomation_Concurrency_Thread_H -#include +#include "Common/Cpp/MoveOnlyFunction.h" #include "Common/Cpp/Containers/Pimpl.h" namespace PokemonAutomation{ @@ -42,7 +42,7 @@ class Thread{ // Constructor - starts thread immediately with given function like std::thread // Function is moved (not copied) for efficiency - Thread(std::function&& function); + Thread(MoveOnlyFunction&& function); // Returns true if Thread has valid data (not default-constructed or moved-from) // and not joined.