AsyncDeleter: Have an own queue for just the delete tasks. This fixes exiting the application.

This commit is contained in:
Maschell 2020-02-23 19:07:18 +01:00
parent 4d2f1aea3e
commit 3915e03ca0
2 changed files with 56 additions and 31 deletions

View File

@ -3,42 +3,57 @@
AsyncExecutor * AsyncExecutor::instance = NULL; AsyncExecutor * AsyncExecutor::instance = NULL;
void AsyncExecutor::pushForDelete(GuiElement * ptr) { void AsyncExecutor::pushForDeleteInternal(GuiElement * ptr) {
execute([ptr] {delete ptr;}); deleteListMutex.lock();
deleteList.push(ptr);
deleteListMutex.unlock();
} }
AsyncExecutor::AsyncExecutor() { AsyncExecutor::AsyncExecutor() {
thread = new std::thread([&]() { thread = new std::thread([&]() {
while(!exitThread) { while(!exitThread) {
instance->mutex.lock(); mutex.lock();
auto it = instance->elements.begin(); bool emptyList = elements.empty();
while (it != instance->elements.end()) { auto it = elements.begin();
while (it != elements.end()) {
auto future = it; auto future = it;
auto status = future->wait_for(std::chrono::seconds(0)); auto status = future->wait_for(std::chrono::seconds(0));
if (status == std::future_status::ready) { if (status == std::future_status::ready) {
it = instance->elements.erase(it); it = elements.erase(it);
} else { } else {
++it; ++it;
} }
} }
instance->mutex.unlock(); if(!emptyList && elements.empty()){
DEBUG_FUNCTION_LINE("All tasks are done\n");
}
mutex.unlock();
deleteListMutex.lock();
while(!deleteList.empty()) {
GuiElement * ptr = deleteList.front();
deleteList.pop();
delete ptr;
}
deleteListMutex.unlock();
std::this_thread::sleep_for(std::chrono::milliseconds(16)); std::this_thread::sleep_for(std::chrono::milliseconds(16));
DCFlushRange((void*)&exitThread, sizeof(exitThread)); DCFlushRange((void*)&exitThread, sizeof(exitThread));
} }
}); });
thread->detach();
} }
void AsyncExecutor::execute(std::function<void()> func) { AsyncExecutor::~AsyncExecutor() {
if(!instance) { exitThread = true;
instance = new AsyncExecutor(); DCFlushRange((void*)&exitThread, sizeof(exitThread));
thread->join();
} }
while(instance->elements.size() > 25) { void AsyncExecutor::executeInternal(std::function<void()> func) {
std::this_thread::sleep_for(std::chrono::milliseconds(16)); if(elements.size() > 10) {
DEBUG_FUNCTION_LINE("Warning, many tasks running currently\n");
//std::this_thread::sleep_for(std::chrono::milliseconds(16));
} }
DEBUG_FUNCTION_LINE("Add new task\n");
instance->mutex.lock(); mutex.lock();
instance->elements.push_back(std::async(std::launch::async,func)); elements.push_back(std::async(std::launch::async,func));
instance->mutex.unlock(); mutex.unlock();
} }

View File

@ -1,18 +1,27 @@
#ifndef ___ASYNCEXECUTER_H_ #pragma once
#define ___ASYNCEXECUTER_H_
#include <vector> #include <vector>
#include <future> #include <future>
#include <thread> #include <thread>
#include <queue>
#include <gui/GuiElement.h> #include <gui/GuiElement.h>
#include <coreinit/cache.h> #include <coreinit/cache.h>
#include "utils/logger.h" #include "utils/logger.h"
class AsyncExecutor { class AsyncExecutor {
public: public:
static void pushForDelete(GuiElement * element); static void pushForDelete(GuiElement * element) {
static void execute(std::function<void()> func); if(!instance) {
instance = new AsyncExecutor();
}
instance->pushForDeleteInternal(element);
}
static void execute(std::function<void()> func) {
if(!instance) {
instance = new AsyncExecutor();
}
instance->executeInternal(func);
}
static void destroyInstance() { static void destroyInstance() {
if(instance) { if(instance) {
@ -25,16 +34,17 @@ private:
static AsyncExecutor *instance; static AsyncExecutor *instance;
AsyncExecutor(); AsyncExecutor();
~AsyncExecutor();
~AsyncExecutor() { void pushForDeleteInternal(GuiElement * element);
exitThread = true; void executeInternal(std::function<void()> func);
DCFlushRange((void*)&exitThread, sizeof(exitThread));
}
std::recursive_mutex mutex; std::recursive_mutex mutex;
std::thread * thread; std::thread * thread;
volatile bool exitThread = false; volatile bool exitThread = false;
std::vector<std::future<void>> elements; std::vector<std::future<void>> elements;
std::recursive_mutex deleteListMutex;
std::queue<GuiElement*> deleteList;
}; };
#endif