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() {
std::unique_lock<std::mutex> unique(selfLock);
//std::unique_lock<std::mutex> unique(selfLock);
DivPendingTask task;
bool setFuckingPromise=false;
@ -48,9 +48,12 @@ void DivWorkThread::run() {
if (terminate) {
break;
}
if (notify.wait_for(unique,std::chrono::milliseconds(100))==std::cv_status::timeout) {
logE("this task timed out!");
}
std::future<void> future=notify.get_future();
future.wait();
lock.lock();
notify=std::promise<void>();
promiseAlreadySet=false;
lock.unlock();
continue;
} else {
task=tasks.front();
@ -94,8 +97,8 @@ bool DivWorkThread::busy() {
void DivWorkThread::finish() {
lock.lock();
terminate=true;
notify.set_value();
lock.unlock();
notify.notify_one();
thread->join();
}
@ -133,7 +136,6 @@ void DivWorkPool::wait() {
if (!threaded) return;
if (busyCount==0) {
logV("nothing to do");
return;
}
@ -141,12 +143,21 @@ void DivWorkPool::wait() {
// start running
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();
// wait
logV("waiting on future");
//SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waiting on future.",NULL);
future.wait();
//SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,"Error","waited - reset promise.",NULL);

View file

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