| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "workPool.h" |
| 21 |
#include "../ta-log.h" |
| 22 |
#include <thread> |
| 23 |
|
| 24 |
void* _workThread(void* inst) { |
| 25 |
((DivWorkThread*)inst)->run(); |
| 26 |
return NULL; |
| 27 |
} |
| 28 |
|
| 29 |
void DivWorkThread::run() { |
| 30 |
//std::unique_lock<std::mutex> unique(selfLock); |
| 31 |
DivPendingTask task; |
| 32 |
bool setFuckingPromise=false; |
| 33 |
|
| 34 |
logV("running work thread"); |
| 35 |
|
| 36 |
while (true) { |
| 37 |
lock.lock(); |
| 38 |
if (tasks.empty()) { |
| 39 |
lock.unlock(); |
| 40 |
isBusy=false; |
| 41 |
if (setFuckingPromise) { |
| 42 |
parent->notify.set_value(); |
| 43 |
setFuckingPromise=false; |
| 44 |
//std::this_thread::yield(); |
| 45 |
} |
| 46 |
if (terminate) { |
| 47 |
break; |
| 48 |
} |
| 49 |
std::future<void> future=notify.get_future(); |
| 50 |
future.wait(); |
| 51 |
lock.lock(); |
| 52 |
notify=std::promise<void>(); |
| 53 |
promiseAlreadySet=false; |
| 54 |
lock.unlock(); |
| 55 |
continue; |
| 56 |
} else { |
| 57 |
task=tasks.front(); |
| 58 |
tasks.pop(); |
| 59 |
lock.unlock(); |
| 60 |
|
| 61 |
task.func(task.funcArg); |
| 62 |
|
| 63 |
int busyCount=--parent->busyCount; |
| 64 |
if (busyCount<0) { |
| 65 |
logE("oh no PROBLEM..."); |
| 66 |
} |
| 67 |
if (busyCount==0) { |
| 68 |
setFuckingPromise=true; |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
bool DivWorkThread::assign(void (*what)(void*), void* arg) { |
| 75 |
lock.lock(); |
| 76 |
if (tasks.size()>=30) { |
| 77 |
lock.unlock(); |
| 78 |
return false; |
| 79 |
} |
| 80 |
tasks.push(DivPendingTask(what,arg)); |
| 81 |
parent->busyCount++; |
| 82 |
isBusy=true; |
| 83 |
lock.unlock(); |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
void DivWorkThread::wait() { |
| 88 |
if (!isBusy) return; |
| 89 |
} |
| 90 |
|
| 91 |
bool DivWorkThread::busy() { |
| 92 |
return isBusy; |
| 93 |
} |
| 94 |
|
| 95 |
void DivWorkThread::finish() { |
| 96 |
lock.lock(); |
| 97 |
terminate=true; |
| 98 |
try { |
| 99 |
notify.set_value(); |
| 100 |
} catch (std::future_error& e) { |
| 101 |
logE("future error! beware!"); |
| 102 |
} |
| 103 |
lock.unlock(); |
| 104 |
thread->join(); |
| 105 |
} |
| 106 |
|
| 107 |
bool DivWorkThread::init(DivWorkPool* p) { |
| 108 |
parent=p; |
| 109 |
try { |
| 110 |
thread=new std::thread(_workThread,this); |
| 111 |
} catch (std::system_error& e) { |
| 112 |
logE("could not start thread! %s",e.what()); |
| 113 |
thread=NULL; |
| 114 |
return false; |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
void DivWorkPool::push(void (*what)(void*), void* arg) { |
| 120 |
// if no work threads, just execute |
| 121 |
if (!threaded) { |
| 122 |
what(arg); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
for (unsigned int tryCount=0; tryCount<count; tryCount++) { |
| 127 |
if (pos>=count) pos=0; |
| 128 |
if (workThreads[pos++].assign(what,arg)) return; |
| 129 |
} |
| 130 |
|
| 131 |
// all threads are busy |
| 132 |
logW("DivWorkPool: all work threads busy!"); |
| 133 |
what(arg); |
| 134 |
} |
| 135 |
|
| 136 |
bool DivWorkPool::busy() { |
| 137 |
if (!threaded) return false; |
| 138 |
for (unsigned int i=0; i<count; i++) { |
| 139 |
if (workThreads[i].busy()) return true; |
| 140 |
} |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
void DivWorkPool::wait() { |
| 145 |
if (!threaded) return; |
| 146 |
|
| 147 |
if (busyCount==0) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
std::future<void> future=notify.get_future(); |
| 152 |
|
| 153 |
// start running |
| 154 |
for (unsigned int i=0; i<count; i++) { |
| 155 |
if (!workThreads[i].promiseAlreadySet && !workThreads[i].tasks.empty()) { |
| 156 |
try { |
| 157 |
workThreads[i].lock.lock(); |
| 158 |
workThreads[i].promiseAlreadySet=true; |
| 159 |
workThreads[i].notify.set_value(); |
| 160 |
workThreads[i].lock.unlock(); |
| 161 |
} catch (std::exception& e) { |
| 162 |
logE("ERROR IN THREAD SYNC! %s",e.what()); |
| 163 |
abort(); |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
//std::this_thread::yield(); |
| 168 |
|
| 169 |
// wait |
| 170 |
future.wait(); |
| 171 |
|
| 172 |
notify=std::promise<void>(); |
| 173 |
|
| 174 |
pos=0; |
| 175 |
} |
| 176 |
|
| 177 |
DivWorkPool::DivWorkPool(unsigned int threads): |
| 178 |
threaded(threads>0), |
| 179 |
count(threads), |
| 180 |
pos(0), |
| 181 |
busyCount(0) { |
| 182 |
if (threaded) { |
| 183 |
workThreads=new DivWorkThread[threads]; |
| 184 |
for (unsigned int i=0; i<count; i++) { |
| 185 |
if (!workThreads[i].init(this)) { |
| 186 |
count=i; |
| 187 |
break; |
| 188 |
} |
| 189 |
} |
| 190 |
if (count<=0) { |
| 191 |
logE("DivWorkPool: couldn't start any threads! falling back to non-threaded mode."); |
| 192 |
delete[] workThreads; |
| 193 |
threaded=false; |
| 194 |
workThreads=NULL; |
| 195 |
} |
| 196 |
} else { |
| 197 |
workThreads=NULL; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
DivWorkPool::~DivWorkPool() { |
| 202 |
if (threaded) { |
| 203 |
if (workThreads!=NULL) { |
| 204 |
for (unsigned int i=0; i<count; i++) { |
| 205 |
workThreads[i].finish(); |
| 206 |
} |
| 207 |
delete[] workThreads; |
| 208 |
} |
| 209 |
} |
| 210 |
} |