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

View file

@ -22,6 +22,7 @@
#define _USE_MATH_DEFINES
#include "dispatch.h"
#include "engine.h"
#include "workPool.h"
#include "../ta-log.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();
if (renderPool==NULL) {
// TODO: test this
renderPool=new DivWorkPool(0);
}
// process MIDI events (TODO: everything)
if (output) if (output->midiIn) while (!output->midiIn->queue.empty()) {
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
if (cycles<runLeftG) {
for (int i=0; i<song.systemLen; i++) {
int total=(cycles*disCont[i].runtotal)/(size<<MASTER_CLOCK_PREC);
disCont[i].acquire(disCont[i].runPos,total);
disCont[i].runLeft-=total;
disCont[i].runPos+=total;
renderPool->push([this,size](void* d) {
DivDispatchContainer* dc=(DivDispatchContainer*)d;
int total=(cycles*dc->runtotal)/(size<<MASTER_CLOCK_PREC);
dc->acquire(dc->runPos,total);
dc->runLeft-=total;
dc->runPos+=total;
},&disCont[i]);
}
renderPool->wait();
runLeftG-=cycles;
cycles=0;
} else {
cycles-=runLeftG;
runLeftG=0;
for (int i=0; i<song.systemLen; i++) {
disCont[i].acquire(disCont[i].runPos,disCont[i].runLeft);
disCont[i].runLeft=0;
renderPool->push([](void* d) {
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);
parent->busyCount--;
if (--parent->busyCount<0) {
logE("oh no PROBLEM...");
}
parent->notify.notify_one();
}
}
@ -125,8 +127,8 @@ void DivWorkPool::wait() {
}
// wait
while (busyCount!=0) {
if (notify.wait_for(unique,std::chrono::milliseconds(100))==std::cv_status::timeout) {
while (busyCount>0) {
if (notify.wait_for(unique,std::chrono::milliseconds(30))==std::cv_status::timeout) {
logW("DivWorkPool: wait() timed out!");
}
}