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;
void AsyncExecutor::pushForDelete(GuiElement * ptr) {
execute([ptr] {delete ptr;});
void AsyncExecutor::pushForDeleteInternal(GuiElement * ptr) {
deleteListMutex.lock();
deleteList.push(ptr);
deleteListMutex.unlock();
}
AsyncExecutor::AsyncExecutor() {
thread = new std::thread([&]() {
while(!exitThread) {
instance->mutex.lock();
auto it = instance->elements.begin();
while (it != instance->elements.end()) {
mutex.lock();
bool emptyList = elements.empty();
auto it = elements.begin();
while (it != elements.end()) {
auto future = it;
auto status = future->wait_for(std::chrono::seconds(0));
if (status == std::future_status::ready) {
it = instance->elements.erase(it);
it = elements.erase(it);
} else {
++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));
DCFlushRange((void*)&exitThread, sizeof(exitThread));
}
});
thread->detach();
}
void AsyncExecutor::execute(std::function<void()> func) {
if(!instance) {
instance = new AsyncExecutor();
}
while(instance->elements.size() > 25) {
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
instance->mutex.lock();
instance->elements.push_back(std::async(std::launch::async,func));
instance->mutex.unlock();
AsyncExecutor::~AsyncExecutor() {
exitThread = true;
DCFlushRange((void*)&exitThread, sizeof(exitThread));
thread->join();
}
void AsyncExecutor::executeInternal(std::function<void()> func) {
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");
mutex.lock();
elements.push_back(std::async(std::launch::async,func));
mutex.unlock();
}

View File

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