this is horrible

This commit is contained in:
tildearrow 2023-09-06 06:22:03 -05:00
parent e1cd05e8e9
commit e8dbacf6e0
2 changed files with 30 additions and 11 deletions

View file

@ -21,6 +21,8 @@
#include "../ta-log.h" #include "../ta-log.h"
#include <thread> #include <thread>
#include <SDL.h>
void* _workThread(void* inst) { void* _workThread(void* inst) {
((DivWorkThread*)inst)->run(); ((DivWorkThread*)inst)->run();
return NULL; return NULL;
@ -29,6 +31,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;
logV("running work thread"); logV("running work thread");
@ -37,7 +40,10 @@ void DivWorkThread::run() {
if (tasks.empty()) { if (tasks.empty()) {
lock.unlock(); lock.unlock();
isBusy=false; isBusy=false;
parent->notify.notify_one(); if (setFuckingPromise) {
parent->notify.set_value();
setFuckingPromise=false;
}
if (terminate) { if (terminate) {
break; break;
} }
@ -50,10 +56,13 @@ void DivWorkThread::run() {
task.func(task.funcArg); task.func(task.funcArg);
if (--parent->busyCount<0) { int busyCount=--parent->busyCount;
if (busyCount<0) {
logE("oh no PROBLEM..."); logE("oh no PROBLEM...");
} }
parent->notify.notify_one(); if (busyCount==0) {
setFuckingPromise=true;
}
} }
} }
} }
@ -119,7 +128,14 @@ bool DivWorkPool::busy() {
void DivWorkPool::wait() { void DivWorkPool::wait() {
if (!threaded) return; if (!threaded) return;
std::unique_lock<std::mutex> unique(selfLock); //std::unique_lock<std::mutex> unique(selfLock);
if (busyCount==0) {
logV("nothing to do");
return;
}
std::future<void> future=notify.get_future();
// start running // start running
for (unsigned int i=0; i<count; i++) { for (unsigned int i=0; i<count; i++) {
@ -127,11 +143,13 @@ void DivWorkPool::wait() {
} }
// wait // wait
while (busyCount>0) { logV("waiting on future");
if (notify.wait_for(unique,std::chrono::milliseconds(30))==std::cv_status::timeout) { //SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waiting on future.",NULL);
logW("DivWorkPool: wait() timed out!"); future.wait();
} //SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waited - reset promise.",NULL);
}
notify=std::promise<void>();
//SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","YES",NULL);
pos=0; pos=0;
} }

View file

@ -25,6 +25,7 @@
#include <atomic> #include <atomic>
#include <functional> #include <functional>
#include <condition_variable> #include <condition_variable>
#include <future>
#include "fixedQueue.h" #include "fixedQueue.h"
@ -75,7 +76,7 @@ class DivWorkPool {
unsigned int pos; unsigned int pos;
DivWorkThread* workThreads; DivWorkThread* workThreads;
public: public:
std::condition_variable notify; std::promise<void> notify;
std::atomic<int> busyCount; std::atomic<int> busyCount;
/** /**
@ -98,4 +99,4 @@ class DivWorkPool {
~DivWorkPool(); ~DivWorkPool();
}; };
#endif #endif