Mercurial > hg > beaglert
diff core/RTAudio.cpp @ 284:7bfb25a2e158 Doxy prerelease
Merge
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Tue, 17 May 2016 15:53:24 +0100 |
parents | 88cf310417cd |
children | c55c6f6c233c |
line wrap: on
line diff
--- a/core/RTAudio.cpp Tue May 17 15:40:16 2016 +0100 +++ b/core/RTAudio.cpp Tue May 17 15:53:24 2016 +0100 @@ -41,10 +41,14 @@ // can schedule typedef struct { RT_TASK task; + void (*argfunction)(void*); void (*function)(void); char *name; int priority; bool started; + bool hasArgs; + void* args; + bool autoSchedule; } InternalAuxiliaryTask; const char gRTAudioThreadName[] = "beaglert-audio"; @@ -291,7 +295,7 @@ // (equal or lower) priority. Audio priority is defined in BEAGLERT_AUDIO_PRIORITY; // priority should be generally be less than this. // Returns an (opaque) pointer to the created task on success; 0 on failure -AuxiliaryTask BeagleRT_createAuxiliaryTask(void (*functionToCall)(void), int priority, const char *name) +AuxiliaryTask BeagleRT_createAuxiliaryTask(void (*functionToCall)(void* args), int priority, const char *name, void* args, bool autoSchedule) { InternalAuxiliaryTask *newTask = (InternalAuxiliaryTask*)malloc(sizeof(InternalAuxiliaryTask)); @@ -303,15 +307,41 @@ } // Populate the rest of the data structure and store it in the vector - newTask->function = functionToCall; + newTask->argfunction = functionToCall; newTask->name = strdup(name); newTask->priority = priority; newTask->started = false; - + newTask->args = args; + newTask->hasArgs = true; + newTask->autoSchedule = autoSchedule; + getAuxTasks().push_back(newTask); return (AuxiliaryTask)newTask; } +AuxiliaryTask BeagleRT_createAuxiliaryTask(void (*functionToCall)(void), int priority, const char *name, bool autoSchedule) +{ + InternalAuxiliaryTask *newTask = (InternalAuxiliaryTask*)malloc(sizeof(InternalAuxiliaryTask)); + + // Attempt to create the task + if(rt_task_create(&(newTask->task), name, 0, priority, T_JOINABLE | T_FPU)) { + cout << "Error: unable to create auxiliary task " << name << endl; + free(newTask); + return 0; + } + + // Populate the rest of the data structure and store it in the vector + newTask->function = functionToCall; + newTask->name = strdup(name); + newTask->priority = priority; + newTask->started = false; + newTask->hasArgs = false; + newTask->autoSchedule = autoSchedule; + + getAuxTasks().push_back(newTask); + + return (AuxiliaryTask)newTask; +} // Schedule a previously created (and started) auxiliary task. It will run when the priority rules next // allow it to be scheduled. @@ -324,22 +354,38 @@ } rt_task_resume(&taskToSchedule->task); } +void BeagleRT_autoScheduleAuxiliaryTasks(){ + vector<InternalAuxiliaryTask*>::iterator it; + for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) { + if ((InternalAuxiliaryTask *)(*it)->autoSchedule){ + BeagleRT_scheduleAuxiliaryTask(*it); + } + } +} // Calculation loop that can be used for other tasks running at a lower // priority than the audio thread. Simple wrapper for Xenomai calls. // Treat the argument as containing the task structure void auxiliaryTaskLoop(void *taskStruct) { + InternalAuxiliaryTask *task = ((InternalAuxiliaryTask *)taskStruct); + // Get function to call from the argument - void (*auxiliary_function)(void) = ((InternalAuxiliaryTask *)taskStruct)->function; - const char *name = ((InternalAuxiliaryTask *)taskStruct)->name; + void (*auxiliary_argfunction)(void* args) = task->argfunction; + void (*auxiliary_function)(void) = task->function; + + // get the task's name + const char *name = task->name; // Wait for a notification rt_task_suspend(NULL); while(!gShouldStop) { // Then run the calculations - auxiliary_function(); + if (task->hasArgs) + auxiliary_argfunction(task->args); + else + auxiliary_function(); // Wait for a notification rt_task_suspend(NULL);