Skip to content
6 changes: 3 additions & 3 deletions Common/Cpp/Concurrency/Backends/AsyncTask_Default.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#ifndef PokemonAutomation_AsyncTask_Default_H
#define PokemonAutomation_AsyncTask_Default_H

#include <functional>
#include <atomic>
#include <exception>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Common/Cpp/Concurrency/SpinPause.h"
#include "Common/Cpp/Concurrency/Mutex.h"
#include "Common/Cpp/Concurrency/ConditionVariable.h"
Expand Down Expand Up @@ -50,7 +50,7 @@ class AsyncTask_Cpp : public AsyncTaskCore{
pause();
}
}
AsyncTask_Cpp(std::function<void()> task)
AsyncTask_Cpp(MoveOnlyFunction<void()> task)
: m_task(std::move(task))
, m_state(State::NOT_STARTED)
{}
Expand Down Expand Up @@ -102,7 +102,7 @@ class AsyncTask_Cpp : public AsyncTaskCore{


private:
std::function<void()> m_task;
MoveOnlyFunction<void()> m_task;
std::atomic<State> m_state;

std::exception_ptr m_exception;
Expand Down
6 changes: 3 additions & 3 deletions Common/Cpp/Concurrency/Backends/ThreadPool_Default.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ WallDuration ThreadPool_Default::cpu_time() const{
return ret;
}

AsyncTask ThreadPool_Default::dispatch(std::function<void()>&& func){
AsyncTask ThreadPool_Default::dispatch(MoveOnlyFunction<void()>&& func){
AsyncTask task;
{
std::unique_lock<Mutex> lg(m_lock);
Expand All @@ -114,7 +114,7 @@ AsyncTask ThreadPool_Default::dispatch(std::function<void()>&& func){
m_thread_cv.notify_one();
return task;
}
AsyncTask ThreadPool_Default::dispatch_now_blocking(std::function<void()>&& func){
AsyncTask ThreadPool_Default::dispatch_now_blocking(MoveOnlyFunction<void()>&& func){
AsyncTask task;
{
std::unique_lock<Mutex> lg(m_lock);
Expand All @@ -138,7 +138,7 @@ AsyncTask ThreadPool_Default::dispatch_now_blocking(std::function<void()>&& func
m_thread_cv.notify_one();
return task;
}
AsyncTask ThreadPool_Default::try_dispatch_now(std::function<void()>& func){
AsyncTask ThreadPool_Default::try_dispatch_now(MoveOnlyFunction<void()>& func){
AsyncTask task;
{
std::lock_guard<Mutex> lg(m_lock);
Expand Down
6 changes: 3 additions & 3 deletions Common/Cpp/Concurrency/Backends/ThreadPool_Default.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class ThreadPool_Default final : public ThreadPool{


public:
[[nodiscard]] virtual AsyncTask dispatch(std::function<void()>&& func) override;
[[nodiscard]] virtual AsyncTask dispatch_now_blocking(std::function<void()>&& func) override;
[[nodiscard]] virtual AsyncTask try_dispatch_now(std::function<void()>& func) override;
[[nodiscard]] virtual AsyncTask dispatch(MoveOnlyFunction<void()>&& func) override;
[[nodiscard]] virtual AsyncTask dispatch_now_blocking(MoveOnlyFunction<void()>&& func) override;
[[nodiscard]] virtual AsyncTask try_dispatch_now(MoveOnlyFunction<void()>& func) override;

virtual void run_in_parallel(
const std::function<void(size_t index)>& func,
Expand Down
7 changes: 4 additions & 3 deletions Common/Cpp/Concurrency/ThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#define PokemonAutomation_ThreadPool_H

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

namespace PokemonAutomation{
Expand All @@ -40,17 +41,17 @@ class ThreadPool{

// Dispatch the function and return immediately.
// The function is not guaranteed to begin running immediately.
[[nodiscard]] virtual AsyncTask dispatch(std::function<void()>&& func) = 0;
[[nodiscard]] virtual AsyncTask dispatch(MoveOnlyFunction<void()>&& func) = 0;

// Dispatch the function and begin running now.
// If there are no threads available, it waits until there are.
// This function will not return until the function has begin running.
[[nodiscard]] virtual AsyncTask dispatch_now_blocking(std::function<void()>&& func) = 0;
[[nodiscard]] virtual AsyncTask dispatch_now_blocking(MoveOnlyFunction<void()>&& func) = 0;

// Try to dispatch the function and begin running now.
// Returns null if no threads are available. "func" will be moved-from only on success.
// This function returns immediately.
[[nodiscard]] virtual AsyncTask try_dispatch_now(std::function<void()>& func) = 0;
[[nodiscard]] virtual AsyncTask try_dispatch_now(MoveOnlyFunction<void()>& func) = 0;

// Run function for all the indices [start, end).
// Lower indices are not allowed to block on higher indices.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

//#include "Common/Cpp/Concurrency/ReverseLockGuard.h"
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Common/Cpp/Concurrency/AsyncTask.h"
#include "CommonFramework/ImageTypes/ImageRGB32_Qt.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
Expand Down Expand Up @@ -100,7 +101,7 @@ bool SnapshotManager::try_dispatch_conversion(uint64_t seqnum, QVideoFrame frame
}

try{
std::function<void()> lambda = [=, this, frame = std::move(frame)](){
MoveOnlyFunction<void()> lambda = [=, this, frame = std::move(frame)](){
convert(seqnum, std::move(frame), timestamp);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <deque>
#include <sstream>
#include "Common/Cpp/MoveOnlyFunction.h"
#include "Common/Cpp/Concurrency/SpinLock.h"
#include "Common/Cpp/Concurrency/AsyncTask.h"
#include "Kernels/Waterfill/Kernels_Waterfill_Session.h"
Expand Down Expand Up @@ -126,7 +127,7 @@
// running one of these calls, all of them wait for a free thread that never
// comes, and the program freezes with the pool stuck at 100% per thread.
//
ShinySparkleSetSwSh find_sparkles(size_t screen_area, WaterfillSession& session){

Check warning on line 130 in SerialPrograms/Source/VideoGames/PokemonSwSh/Inference/ShinyDetection/PokemonSwSh_ShinySparkleSet.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonSwSh::find_sparkles( size_t screen_area , WaterfillSession & session): function_lines=27 (advisory above 25, limit 50)
ThreadPool& pool = GlobalThreadPools::computation_realtime();
SpinLock lock;
ShinySparkleSetSwSh sparkles;
Expand All @@ -134,7 +135,7 @@
auto finder = session.make_iterator(20);
WaterfillObject object;
while (finder->find_next(object, true)){
std::function<void()> func = [&, screen_area, object = std::move(object)]{
MoveOnlyFunction<void()> func = [&, screen_area, object = std::move(object)]{
find_sparkles(
screen_area,
lock,
Expand Down
Loading