furnace/src/engine/workPool.cpp

205 lines
4.5 KiB
C++
Raw Normal View History

2023-09-04 06:18:48 +00:00
/**
* Furnace Tracker - multi-system chiptune tracker
* Copyright (C) 2021-2023 tildearrow and contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
2023-09-05 09:38:57 +00:00
#include "workPool.h"
#include "../ta-log.h"
#include <thread>
void* _workThread(void* inst) {
((DivWorkThread*)inst)->run();
return NULL;
}
void DivWorkThread::run() {
2023-09-06 21:39:35 +00:00
//std::unique_lock<std::mutex> unique(selfLock);
2023-09-05 09:38:57 +00:00
DivPendingTask task;
2023-09-06 11:22:03 +00:00
bool setFuckingPromise=false;
2023-09-05 09:38:57 +00:00
logV("running work thread");
while (true) {
lock.lock();
if (tasks.empty()) {
lock.unlock();
isBusy=false;
2023-09-06 11:22:03 +00:00
if (setFuckingPromise) {
parent->notify.set_value();
setFuckingPromise=false;
//std::this_thread::yield();
2023-09-06 11:22:03 +00:00
}
2023-09-05 09:38:57 +00:00
if (terminate) {
break;
}
2023-09-06 21:39:35 +00:00
std::future<void> future=notify.get_future();
future.wait();
lock.lock();
notify=std::promise<void>();
promiseAlreadySet=false;
lock.unlock();
2023-09-05 09:38:57 +00:00
continue;
} else {
task=tasks.front();
tasks.pop();
lock.unlock();
task.func(task.funcArg);
2023-09-06 11:22:03 +00:00
int busyCount=--parent->busyCount;
if (busyCount<0) {
logE("oh no PROBLEM...");
}
2023-09-06 11:22:03 +00:00
if (busyCount==0) {
setFuckingPromise=true;
}
2023-09-05 09:38:57 +00:00
}
}
}
bool DivWorkThread::assign(void (*what)(void*), void* arg) {
2023-09-05 09:38:57 +00:00
lock.lock();
if (tasks.size()>=30) {
lock.unlock();
return false;
}
tasks.push(DivPendingTask(what,arg));
parent->busyCount++;
isBusy=true;
lock.unlock();
return true;
}
void DivWorkThread::wait() {
if (!isBusy) return;
}
bool DivWorkThread::busy() {
return isBusy;
}
void DivWorkThread::finish() {
lock.lock();
terminate=true;
2023-09-06 21:39:35 +00:00
notify.set_value();
2023-09-05 09:38:57 +00:00
lock.unlock();
thread->join();
}
2023-09-07 06:16:12 +00:00
bool DivWorkThread::init(DivWorkPool* p) {
2023-09-05 09:38:57 +00:00
parent=p;
2023-09-07 06:16:12 +00:00
try {
thread=new std::thread(_workThread,this);
} catch (std::system_error& e) {
logE("could not start thread! %s",e.what());
thread=NULL;
return false;
}
return true;
2023-09-05 09:38:57 +00:00
}
void DivWorkPool::push(void (*what)(void*), void* arg) {
2023-09-05 09:38:57 +00:00
// if no work threads, just execute
if (!threaded) {
what(arg);
return;
}
for (unsigned int tryCount=0; tryCount<count; tryCount++) {
2023-09-05 11:02:58 +00:00
if (pos>=count) pos=0;
2023-09-05 09:38:57 +00:00
if (workThreads[pos++].assign(what,arg)) return;
}
// all threads are busy
2023-09-05 11:02:58 +00:00
logW("DivWorkPool: all work threads busy!");
2023-09-05 09:38:57 +00:00
what(arg);
}
bool DivWorkPool::busy() {
if (!threaded) return false;
for (unsigned int i=0; i<count; i++) {
if (workThreads[i].busy()) return true;
}
return false;
}
void DivWorkPool::wait() {
if (!threaded) return;
2023-09-06 11:22:03 +00:00
if (busyCount==0) {
return;
}
std::future<void> future=notify.get_future();
2023-09-05 11:02:58 +00:00
// start running
for (unsigned int i=0; i<count; i++) {
2023-09-07 05:21:26 +00:00
if (!workThreads[i].promiseAlreadySet && !workThreads[i].tasks.empty()) {
2023-09-06 21:39:35 +00:00
try {
workThreads[i].lock.lock();
workThreads[i].promiseAlreadySet=true;
workThreads[i].notify.set_value();
workThreads[i].lock.unlock();
} catch (std::exception& e) {
2023-09-07 00:41:54 +00:00
logE("ERROR IN THREAD SYNC! %s",e.what());
2023-09-06 21:39:35 +00:00
abort();
}
}
2023-09-05 11:02:58 +00:00
}
//std::this_thread::yield();
2023-09-05 11:02:58 +00:00
// wait
2023-09-06 11:22:03 +00:00
future.wait();
notify=std::promise<void>();
2023-09-06 09:06:26 +00:00
pos=0;
2023-09-05 09:38:57 +00:00
}
DivWorkPool::DivWorkPool(unsigned int threads):
threaded(threads>0),
count(threads),
pos(0),
busyCount(0) {
if (threaded) {
workThreads=new DivWorkThread[threads];
for (unsigned int i=0; i<count; i++) {
2023-09-07 06:16:12 +00:00
if (!workThreads[i].init(this)) {
count=i;
break;
}
}
if (count<=0) {
logE("DivWorkPool: couldn't start any threads! falling back to non-threaded mode.");
delete[] workThreads;
threaded=false;
workThreads=NULL;
2023-09-05 09:38:57 +00:00
}
} else {
workThreads=NULL;
}
}
DivWorkPool::~DivWorkPool() {
if (threaded) {
for (unsigned int i=0; i<count; i++) {
workThreads[i].finish();
}
delete[] workThreads;
}
2023-09-05 11:02:58 +00:00
}