prepare for threaded rendering?

one chip per thread.
This commit is contained in:
tildearrow 2023-09-06 04:03:53 -05:00
parent ec4063641a
commit 7d0f816d11
3 changed files with 29 additions and 9 deletions

View file

@ -39,6 +39,8 @@
#include <unordered_map> #include <unordered_map>
#include <deque> #include <deque>
class DivWorkPool;
#define addWarning(x) \ #define addWarning(x) \
if (warnings.empty()) { \ if (warnings.empty()) { \
warnings+=x; \ warnings+=x; \
@ -485,6 +487,8 @@ class DivEngine {
size_t totalProcessed; size_t totalProcessed;
DivWorkPool* renderPool;
// MIDI stuff // MIDI stuff
std::function<int(const TAMidiMessage&)> midiCallback=[](const TAMidiMessage&) -> int {return -2;}; std::function<int(const TAMidiMessage&)> midiCallback=[](const TAMidiMessage&) -> int {return -2;};

View file

@ -22,6 +22,7 @@
#define _USE_MATH_DEFINES #define _USE_MATH_DEFINES
#include "dispatch.h" #include "dispatch.h"
#include "engine.h" #include "engine.h"
#include "workPool.h"
#include "../ta-log.h" #include "../ta-log.h"
#include <math.h> #include <math.h>
@ -1788,6 +1789,11 @@ void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsi
std::chrono::steady_clock::time_point ts_processBegin=std::chrono::steady_clock::now(); std::chrono::steady_clock::time_point ts_processBegin=std::chrono::steady_clock::now();
if (renderPool==NULL) {
// TODO: test this
renderPool=new DivWorkPool(0);
}
// process MIDI events (TODO: everything) // process MIDI events (TODO: everything)
if (output) if (output->midiIn) while (!output->midiIn->queue.empty()) { if (output) if (output->midiIn) while (!output->midiIn->queue.empty()) {
TAMidiMessage& msg=output->midiIn->queue.front(); TAMidiMessage& msg=output->midiIn->queue.front();
@ -2061,20 +2067,28 @@ void DivEngine::nextBuf(float** in, float** out, int inChans, int outChans, unsi
// 5. tick the clock and fill buffers as needed // 5. tick the clock and fill buffers as needed
if (cycles<runLeftG) { if (cycles<runLeftG) {
for (int i=0; i<song.systemLen; i++) { for (int i=0; i<song.systemLen; i++) {
int total=(cycles*disCont[i].runtotal)/(size<<MASTER_CLOCK_PREC); renderPool->push([this,size](void* d) {
disCont[i].acquire(disCont[i].runPos,total); DivDispatchContainer* dc=(DivDispatchContainer*)d;
disCont[i].runLeft-=total; int total=(cycles*dc->runtotal)/(size<<MASTER_CLOCK_PREC);
disCont[i].runPos+=total; dc->acquire(dc->runPos,total);
dc->runLeft-=total;
dc->runPos+=total;
},&disCont[i]);
} }
renderPool->wait();
runLeftG-=cycles; runLeftG-=cycles;
cycles=0; cycles=0;
} else { } else {
cycles-=runLeftG; cycles-=runLeftG;
runLeftG=0; runLeftG=0;
for (int i=0; i<song.systemLen; i++) { for (int i=0; i<song.systemLen; i++) {
disCont[i].acquire(disCont[i].runPos,disCont[i].runLeft); renderPool->push([](void* d) {
disCont[i].runLeft=0; DivDispatchContainer* dc=(DivDispatchContainer*)d;
dc->acquire(dc->runPos,dc->runLeft);
dc->runLeft=0;
},&disCont[i]);
} }
renderPool->wait();
} }
} }
} }

View file

@ -50,7 +50,9 @@ void DivWorkThread::run() {
task.func(task.funcArg); task.func(task.funcArg);
parent->busyCount--; if (--parent->busyCount<0) {
logE("oh no PROBLEM...");
}
parent->notify.notify_one(); parent->notify.notify_one();
} }
} }
@ -125,8 +127,8 @@ void DivWorkPool::wait() {
} }
// wait // wait
while (busyCount!=0) { while (busyCount>0) {
if (notify.wait_for(unique,std::chrono::milliseconds(100))==std::cv_status::timeout) { if (notify.wait_for(unique,std::chrono::milliseconds(30))==std::cv_status::timeout) {
logW("DivWorkPool: wait() timed out!"); logW("DivWorkPool: wait() timed out!");
} }
} }