much better

This commit is contained in:
tildearrow 2023-09-06 16:39:35 -05:00
parent 5329e551d4
commit 692a1b7915
2 changed files with 23 additions and 12 deletions

View file

@ -29,7 +29,7 @@ void* _workThread(void* inst) {
} }
void DivWorkThread::run() { void DivWorkThread::run() {
std::unique_lock<std::mutex> unique(selfLock); //std::unique_lock<std::mutex> unique(selfLock);
DivPendingTask task; DivPendingTask task;
bool setFuckingPromise=false; bool setFuckingPromise=false;
@ -48,9 +48,12 @@ void DivWorkThread::run() {
if (terminate) { if (terminate) {
break; break;
} }
if (notify.wait_for(unique,std::chrono::milliseconds(100))==std::cv_status::timeout) { std::future<void> future=notify.get_future();
logE("this task timed out!"); future.wait();
} lock.lock();
notify=std::promise<void>();
promiseAlreadySet=false;
lock.unlock();
continue; continue;
} else { } else {
task=tasks.front(); task=tasks.front();
@ -94,8 +97,8 @@ bool DivWorkThread::busy() {
void DivWorkThread::finish() { void DivWorkThread::finish() {
lock.lock(); lock.lock();
terminate=true; terminate=true;
notify.set_value();
lock.unlock(); lock.unlock();
notify.notify_one();
thread->join(); thread->join();
} }
@ -133,7 +136,6 @@ void DivWorkPool::wait() {
if (!threaded) return; if (!threaded) return;
if (busyCount==0) { if (busyCount==0) {
logV("nothing to do");
return; return;
} }
@ -141,12 +143,21 @@ void DivWorkPool::wait() {
// start running // start running
for (unsigned int i=0; i<count; i++) { for (unsigned int i=0; i<count; i++) {
workThreads[i].notify.notify_one(); if (!workThreads[i].promiseAlreadySet) {
try {
workThreads[i].lock.lock();
workThreads[i].promiseAlreadySet=true;
workThreads[i].notify.set_value();
workThreads[i].lock.unlock();
} catch (std::exception& e) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"EXCEPTION ON WAIT",e.what(),NULL);
abort();
}
}
} }
std::this_thread::yield(); std::this_thread::yield();
// wait // wait
logV("waiting on future");
//SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waiting on future.",NULL); //SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waiting on future.",NULL);
future.wait(); future.wait();
//SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waited - reset promise.",NULL); //SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waited - reset promise.",NULL);

View file

@ -24,7 +24,6 @@
#include <mutex> #include <mutex>
#include <atomic> #include <atomic>
#include <functional> #include <functional>
#include <condition_variable>
#include <future> #include <future>
#include "fixedQueue.h" #include "fixedQueue.h"
@ -45,12 +44,12 @@ struct DivPendingTask {
struct DivWorkThread { struct DivWorkThread {
DivWorkPool* parent; DivWorkPool* parent;
std::mutex lock; std::mutex lock;
std::mutex selfLock;
std::thread* thread; std::thread* thread;
std::condition_variable notify; std::promise<void> notify;
FixedQueue<DivPendingTask,32> tasks; FixedQueue<DivPendingTask,32> tasks;
std::atomic<bool> isBusy; std::atomic<bool> isBusy;
bool terminate; bool terminate;
bool promiseAlreadySet;
void run(); void run();
bool assign(const std::function<void(void*)>& what, void* arg); bool assign(const std::function<void(void*)>& what, void* arg);
@ -62,7 +61,8 @@ struct DivWorkThread {
DivWorkThread(): DivWorkThread():
parent(NULL), parent(NULL),
isBusy(false), isBusy(false),
terminate(false) {} terminate(false),
promiseAlreadySet(false) {}
}; };
/** /**