Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Common/Cpp/Concurrency/Backends/Thread_Qt.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace PokemonAutomation{

// PIMPL implementation - hides std::thread from header
struct Thread::Data : public QThread{
Data(std::function<void()>&& function)
Data(MoveOnlyFunction<void()>&& function)
: m_function(std::move(function))
// , m_finished(false)
{
Expand Down Expand Up @@ -69,7 +69,7 @@ struct Thread::Data : public QThread{
// cout << "exiting: " << this << endl;
}

std::function<void()> m_function;
MoveOnlyFunction<void()> m_function;

std::atomic<bool> m_finished;
};
Expand All @@ -95,7 +95,7 @@ Thread::Thread() = default;
// Main constructor - starts thread immediately with given function
// - Uses CONSTRUCT_TOKEN to construct Pimpl<Data> with Data constructor
// - Thread starts running immediately (same as std::thread behavior)
Thread::Thread(std::function<void()>&& function)
Thread::Thread(MoveOnlyFunction<void()>&& function)
: m_data(CONSTRUCT_TOKEN, std::move(function))
{}

Expand Down
4 changes: 2 additions & 2 deletions Common/Cpp/Concurrency/Backends/Thread_StdThread.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace PokemonAutomation{
struct Thread::Data{
std::thread m_thread;

Data(std::function<void()>&& function)
Data(MoveOnlyFunction<void()>&& function)
: m_thread(std::move(function))
{}
};
Expand All @@ -45,7 +45,7 @@ Thread::Thread() = default;
// Main constructor - starts thread immediately with given function
// - Uses CONSTRUCT_TOKEN to construct Pimpl<Data> with Data constructor
// - Thread starts running immediately (same as std::thread behavior)
Thread::Thread(std::function<void()>&& function)
Thread::Thread(MoveOnlyFunction<void()>&& function)
: m_data(CONSTRUCT_TOKEN, std::move(function))
{}

Expand Down
6 changes: 3 additions & 3 deletions Common/Cpp/Concurrency/Backends/Thread_StdThreadDetach.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ struct Thread::Data{

std::thread m_thread;

Data(std::function<void()>&& function)
Data(MoveOnlyFunction<void()>&& function)
: m_stopped(false)
, m_thread(
[this, function = std::move(function)]{
[this, function = std::move(function)]() mutable{
function();
// std::lock_guard<std::mutex> lg(m_lock);
// m_stopped = true;
Expand Down Expand Up @@ -60,7 +60,7 @@ Thread::Thread() = default;
// Main constructor - starts thread immediately with given function
// - Uses CONSTRUCT_TOKEN to construct Pimpl<Data> with Data constructor
// - Thread starts running immediately (same as std::thread behavior)
Thread::Thread(std::function<void()>&& function)
Thread::Thread(MoveOnlyFunction<void()>&& function)
: m_data(CONSTRUCT_TOKEN, std::move(function))
{}

Expand Down
4 changes: 2 additions & 2 deletions Common/Cpp/Concurrency/FireForgetDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ FireForgetDispatcher::~FireForgetDispatcher(){
stop();
}

void FireForgetDispatcher::dispatch(std::function<void()>&& func){
void FireForgetDispatcher::dispatch(MoveOnlyFunction<void()>&& func){
std::lock_guard<Mutex> lg(m_lock);
m_queue.emplace_back(std::move(func));
m_cv.notify_one();
Expand All @@ -58,7 +58,7 @@ void FireForgetDispatcher::dispatch(std::function<void()>&& func){

void FireForgetDispatcher::thread_loop(){
while (true){
std::function<void()> task;
MoveOnlyFunction<void()> task;
{
std::unique_lock<Mutex> lg(m_lock);
if (m_stopping){
Expand Down
6 changes: 3 additions & 3 deletions Common/Cpp/Concurrency/FireForgetDispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define PokemonAutomation_FireForgetDispatcher_H

#include <deque>
#include <functional>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Mutex.h"
#include "ConditionVariable.h"
#include "Thread.h"
Expand All @@ -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<void()>&& func);
void dispatch(MoveOnlyFunction<void()>&& func);

void stop();

Expand All @@ -32,7 +32,7 @@ class FireForgetDispatcher{
void thread_loop();

private:
std::deque<std::function<void()>> m_queue;
std::deque<MoveOnlyFunction<void()>> m_queue;
Thread m_thread;
bool m_stopping;
Mutex m_lock;
Expand Down
4 changes: 2 additions & 2 deletions Common/Cpp/Concurrency/ScheduledTaskRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ WallClock ScheduledTaskRunner::next_event() const{
}
return m_schedule.begin()->first;
}
void ScheduledTaskRunner::add_event(WallClock time, std::function<void()> callback){
void ScheduledTaskRunner::add_event(WallClock time, MoveOnlyFunction<void()> callback){
std::lock_guard<Mutex> lg(m_lock);
if (m_stopped){
return;
}
m_schedule.emplace(time, std::move(callback));
m_cv.notify_all();
}
void ScheduledTaskRunner::add_event(std::chrono::milliseconds time_from_now, std::function<void()> callback){
void ScheduledTaskRunner::add_event(std::chrono::milliseconds time_from_now, MoveOnlyFunction<void()> callback){
std::lock_guard<Mutex> lg(m_lock);
if (m_stopped){
return;
Expand Down
7 changes: 4 additions & 3 deletions Common/Cpp/Concurrency/ScheduledTaskRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define PokemonAutomation_ScheduledTaskRunner_H

#include <map>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Common/Cpp/Time.h"
//#include "Common/Cpp/CancellableScope.h"
#include "Common/Cpp/Concurrency/Mutex.h"
Expand All @@ -33,8 +34,8 @@ class ScheduledTaskRunner{
// events are scheduled.
WallClock next_event() const;

void add_event(WallClock time, std::function<void()> callback);
void add_event(std::chrono::milliseconds time_from_now, std::function<void()> callback);
void add_event(WallClock time, MoveOnlyFunction<void()> callback);
void add_event(std::chrono::milliseconds time_from_now, MoveOnlyFunction<void()> callback);

// virtual bool cancel(std::exception_ptr exception) noexcept override;

Expand All @@ -46,7 +47,7 @@ class ScheduledTaskRunner{
ConditionVariable m_cv;
bool m_stopped;

std::multimap<WallClock, std::function<void()>> m_schedule;
std::multimap<WallClock, MoveOnlyFunction<void()>> m_schedule;

AsyncTask m_runner;
};
Expand Down
4 changes: 2 additions & 2 deletions Common/Cpp/Concurrency/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#ifndef PokemonAutomation_Concurrency_Thread_H
#define PokemonAutomation_Concurrency_Thread_H

#include <functional>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Common/Cpp/Containers/Pimpl.h"

namespace PokemonAutomation{
Expand All @@ -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<void()>&& function);
Thread(MoveOnlyFunction<void()>&& function);

// Returns true if Thread has valid data (not default-constructed or moved-from)
// and not joined.
Expand Down
Loading