comparison core/RTAudio.cpp @ 176:fb2cf9c00024

It is now possible to create an auxiliary task in the constructor of a global object. Closes #1374
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 28 Dec 2015 16:22:54 +0100
parents 1e629f126322
children 3b364fb8c4ee
comparison
equal deleted inserted replaced
174:1e629f126322 176:fb2cf9c00024
56 RT_INTR gRTAudioInterrupt; 56 RT_INTR gRTAudioInterrupt;
57 #endif 57 #endif
58 PRU *gPRU = 0; 58 PRU *gPRU = 0;
59 I2c_Codec *gAudioCodec = 0; 59 I2c_Codec *gAudioCodec = 0;
60 60
61 vector<InternalAuxiliaryTask*> gAuxTasks; 61 vector<InternalAuxiliaryTask*> &getAuxTasks(){
62 static vector<InternalAuxiliaryTask*> auxTasks;
63 return auxTasks;
64 }
62 65
63 // Flag which tells the audio task to stop 66 // Flag which tells the audio task to stop
64 bool gShouldStop = false; 67 bool gShouldStop = false;
65 68
66 // general settings 69 // general settings
303 newTask->function = functionToCall; 306 newTask->function = functionToCall;
304 newTask->name = strdup(name); 307 newTask->name = strdup(name);
305 newTask->priority = priority; 308 newTask->priority = priority;
306 newTask->started = false; 309 newTask->started = false;
307 310
308 gAuxTasks.push_back(newTask); 311 getAuxTasks().push_back(newTask);
309 312
310 return (AuxiliaryTask)newTask; 313 return (AuxiliaryTask)newTask;
311 } 314 }
312 315
313 // Schedule a previously created (and started) auxiliary task. It will run when the priority rules next 316 // Schedule a previously created (and started) auxiliary task. It will run when the priority rules next
387 return -1; 390 return -1;
388 } 391 }
389 392
390 // The user may have created other tasks. Start those also. 393 // The user may have created other tasks. Start those also.
391 vector<InternalAuxiliaryTask*>::iterator it; 394 vector<InternalAuxiliaryTask*>::iterator it;
392 for(it = gAuxTasks.begin(); it != gAuxTasks.end(); it++) { 395 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
393 return BeagleRT_startAuxiliaryTask(*it); 396 return BeagleRT_startAuxiliaryTask(*it);
394 } 397 }
395 398
396 return 0; 399 return 0;
397 } 400 }
410 // Now wait for threads to respond and actually stop... 413 // Now wait for threads to respond and actually stop...
411 rt_task_join(&gRTAudioThread); 414 rt_task_join(&gRTAudioThread);
412 415
413 // Stop all the auxiliary threads too 416 // Stop all the auxiliary threads too
414 vector<InternalAuxiliaryTask*>::iterator it; 417 vector<InternalAuxiliaryTask*>::iterator it;
415 for(it = gAuxTasks.begin(); it != gAuxTasks.end(); it++) { 418 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
416 InternalAuxiliaryTask *taskStruct = *it; 419 InternalAuxiliaryTask *taskStruct = *it;
417 420
418 // Wake up each thread and join it 421 // Wake up each thread and join it
419 rt_task_resume(&(taskStruct->task)); 422 rt_task_resume(&(taskStruct->task));
420 rt_task_join(&(taskStruct->task)); 423 rt_task_join(&(taskStruct->task));
426 { 429 {
427 cleanup(&gContext, gUserData); 430 cleanup(&gContext, gUserData);
428 431
429 // Clean up the auxiliary tasks 432 // Clean up the auxiliary tasks
430 vector<InternalAuxiliaryTask*>::iterator it; 433 vector<InternalAuxiliaryTask*>::iterator it;
431 for(it = gAuxTasks.begin(); it != gAuxTasks.end(); it++) { 434 for(it = getAuxTasks().begin(); it != getAuxTasks().end(); it++) {
432 InternalAuxiliaryTask *taskStruct = *it; 435 InternalAuxiliaryTask *taskStruct = *it;
433 436
434 // Delete the task 437 // Delete the task
435 rt_task_delete(&taskStruct->task); 438 rt_task_delete(&taskStruct->task);
436 439
437 // Free the name string and the struct itself 440 // Free the name string and the struct itself
438 free(taskStruct->name); 441 free(taskStruct->name);
439 free(taskStruct); 442 free(taskStruct);
440 } 443 }
441 gAuxTasks.clear(); 444 getAuxTasks().clear();
442 445
443 // Delete the audio task and its interrupt 446 // Delete the audio task and its interrupt
444 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS 447 #ifdef BEAGLERT_USE_XENOMAI_INTERRUPTS
445 rt_intr_delete(&gRTAudioInterrupt); 448 rt_intr_delete(&gRTAudioInterrupt);
446 #endif 449 #endif