Skip to content

Commit

Permalink
Store function pointer for parallel thread in class
Browse files Browse the repository at this point in the history
  • Loading branch information
brummer10 committed Aug 5, 2024
1 parent 6879ae2 commit 938fbb2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 23 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@
[submodule "FFTConvolver"]
path = FFTConvolver
url = https://github.com/HiFi-LoFi/FFTConvolver.git
[submodule "delegate"]
path = delegate
url = https://github.com/rosbacke/delegate.git
62 changes: 47 additions & 15 deletions Ratatouille/ParallelThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
* requires minimum c++17
* works best with c++20 (std::atomic::wait)
*
* ParallelThread using delegate to replace std::function<void>
* see:
* https://github.com/rosbacke/delegate
*
* ParallelThread aims to be suitable in real-time processes
* to provide a parallel processor.
*
Expand All @@ -39,7 +35,7 @@
* proc.setTimeOut(std::max(100,static_cast<int>((bufferSize/(sampleRate*0.000001))*0.1)));
* // set the function to run in the parallel thread
* function should be defined in YourClass as void YourFunction();
* proc.process.set<&YourClass::YourFunction>(*this);
* proc.set<YourClass, &YourClass::YourFunction>(*this);
* // now anything is setup to run the thread,
* so try to get the processing pointer by getProcess()
* getProcess() check if the thread is in waiting state, if not,
Expand Down Expand Up @@ -79,14 +75,57 @@

#include <pthread.h>

#include "delegate.hpp"

#pragma once

#ifndef PARALLEL_THREAD_H_
#define PARALLEL_THREAD_H_

class ParallelThread
class ProcessPtr
{
public:
ProcessPtr() {
set<0, ProcessPtr, &ProcessPtr::dummyFunc>(this);
set<1, ProcessPtr, &ProcessPtr::dummyFunc>(this);
i = 0;
}

template <class C, void (C::*Function)()>
void set(C* instance) {
instPtr[i] = instance;
memberFunc[i] = &wrap<C, Function>;
}

template <uint32_t s, class C, void (C::*Function)()>
void set(C* instance) {
instPtr[s] = instance;
memberFunc[s] = &wrap<C, Function>;
}

void setProcessor(uint32_t i_) {
i = i_;
}

void process() const {
return memberFunc[i](instPtr[i]);
}

private:
typedef void* InstancePtr;
typedef void (*MemberFunc)(InstancePtr);

template <class C, void (C::*Function)()>
static inline void wrap(InstancePtr instance) {
return (static_cast<C*>(instance)->*Function)();
}

void dummyFunc() {}

InstancePtr instPtr[2];
MemberFunc memberFunc[2];
uint32_t i;
};

class ParallelThread: public ProcessPtr
{
public:
//Constructor
Expand All @@ -99,7 +138,6 @@ class ParallelThread
#endif
{
timeoutPeriod = 400;
process.set<&ParallelThread::runDummyFunction>(*this);
threadName = "anonymous";
init();
}
Expand Down Expand Up @@ -208,9 +246,6 @@ class ParallelThread
}
}

// the function to run by thread
// could be a std::function, but delegate is more lightweight
delegate<void ()>process;

private:
std::atomic<bool> pRun;
Expand Down Expand Up @@ -322,9 +357,6 @@ class ParallelThread
}
#endif

// default dummy function running by delegate
void runDummyFunction() {}

};

#endif
8 changes: 5 additions & 3 deletions Ratatouille/Ratatouille.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Xratatouille::Xratatouille() :
_normA(0),
_normB(0) {
xrworker.start();
xrworker.process.set<&Xratatouille::do_work_mono>(*this);
xrworker.set<Xratatouille, &Xratatouille::do_work_mono>(this);
//xrworker.process = [=] () {do_work_mono();};
pro.start();
};
Expand Down Expand Up @@ -300,6 +300,8 @@ void Xratatouille::init_dsp_(uint32_t rate)
if (!rt_policy) rt_policy = 1; //SCHED_FIFO;
pro.setThreadName("RT");
pro.setPriority(rt_prio, rt_policy);
pro.set<0, Xratatouille, &Xratatouille::processSlotB>(this);
pro.set<1, Xratatouille, &Xratatouille::processConv1>(this);

model_file = "None";
model_file1 = "None";
Expand Down Expand Up @@ -692,7 +694,7 @@ void Xratatouille::run_dsp_(uint32_t n_samples)
// process slot B in parallel
_bufb = bufb;
if (_neuralB.load(std::memory_order_acquire) && pro.getProcess()) {
pro.process.set<&Xratatouille::processSlotB>(*this);
pro.setProcessor(0);
pro.runProcess();
} else {
processSlotB();
Expand Down Expand Up @@ -743,7 +745,7 @@ void Xratatouille::run_dsp_(uint32_t n_samples)
_bufb = bufb;
if (!_execute.load(std::memory_order_acquire) && conv1.is_runnable()) {
if (pro.getProcess()) {
pro.process.set<&Xratatouille::processConv1>(*this);
pro.setProcessor(1);
pro.runProcess();
} else {
processConv1();
Expand Down
3 changes: 2 additions & 1 deletion Ratatouille/fftconvolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class DoubleThreadConvolver: public fftconvolver::TwoStageFFTConvolver
DoubleThreadConvolver()
: resamp(), ready(false), samplerate(0), pro() {
pro.setTimeOut(200);
pro.process.set<&DoubleThreadConvolver::doBackgroundProcessing>(*this);
pro.set<DoubleThreadConvolver, &DoubleThreadConvolver::backgroundProcessing>(this);
pro.setThreadName("Convolver");
norm = 0;}

Expand All @@ -137,6 +137,7 @@ class DoubleThreadConvolver: public fftconvolver::TwoStageFFTConvolver
private:
friend class ParallelThread;
gx_resample::BufferResampler resamp;
void backgroundProcessing() { return doBackgroundProcessing();}
volatile bool ready;
uint32_t buffersize;
uint32_t samplerate;
Expand Down
1 change: 0 additions & 1 deletion delegate
Submodule delegate deleted from 64185e

0 comments on commit 938fbb2

Please sign in to comment.