From aec14a264cdb19ccbd9e53596c58da9b60308909 Mon Sep 17 00:00:00 2001 From: Florian Jung Date: Fri, 6 Apr 2012 17:27:15 +0200 Subject: improved inter-thread communication. the in-synth-CLI still partially uses the non-synced communication. TODO! --- synth/communication.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++---- synth/communication.h | 35 ++++++++++++++++++------- synth/in_synth_cli.cpp | 52 +++++++++--------------------------- synth/jack.cpp | 60 +++++++++++++++++++++++++++++++----------- 4 files changed, 146 insertions(+), 71 deletions(-) (limited to 'synth') diff --git a/synth/communication.cpp b/synth/communication.cpp index f21e46f..4f9ce75 100644 --- a/synth/communication.cpp +++ b/synth/communication.cpp @@ -1,16 +1,76 @@ #include "communication.h" -pthread_mutex_t suspend_request_mutex; -suspend_request_t suspend_request; +#include +#include + +using namespace std; + +jack_ringbuffer_t* request_ringbuf; +jack_ringbuffer_t* result_ringbuf; +pthread_mutex_t request_mutex; + void init_communication() { - pthread_mutex_init(&suspend_request_mutex, NULL); + pthread_mutex_init(&request_mutex, NULL); - suspend_request.done=true; + request_ringbuf=jack_ringbuffer_create(sizeof(request_t)+1); + result_ringbuf=jack_ringbuffer_create(sizeof(int)+1); } void uninit_communication() { - pthread_mutex_destroy(&suspend_request_mutex); + jack_ringbuffer_free(request_ringbuf); + jack_ringbuffer_free(result_ringbuf); + + pthread_mutex_destroy(&request_mutex); +} + +int do_request(request_t request) +{ + pthread_mutex_lock(&request_mutex); // only one request at a time + + jack_ringbuffer_write(request_ringbuf, (char*)(&request), sizeof(request_t)); + + do // wait for the answer + { + usleep(10000); + } while (jack_ringbuffer_read_space(result_ringbuf)=sizeof(request_t)); +} + +request_t get_request() +{ + request_t request; + + int len=jack_ringbuffer_read(request_ringbuf, (char*)(&request), sizeof(request_t)); + if (len==0) + cout << "ERROR: no request on the ringbuffer! nothing read, continuing..." << endl; + else if (len!=sizeof(request_t)) + { + cout << "possibly FATAL: short read from the request ringbuffer, expect breakage!" << endl; + request.type=request_t::NONE; + } + + return request; +} + +void request_finished(int result) +{ + jack_ringbuffer_write(result_ringbuf, (char*)(&result), sizeof(int)); } diff --git a/synth/communication.h b/synth/communication.h index 27f7186..8d11cd4 100644 --- a/synth/communication.h +++ b/synth/communication.h @@ -2,21 +2,36 @@ #define __COMMUNICATION_H__ #include +#include -struct suspend_request_t +struct request_t { - int prog; //if negative, all programs are affected - bool suspend; //true->suspend, false->use them again - bool done; //must be set to false by the requester, - //must be set to true after processing by the requestee + enum request_type_t { NONE, SUSPEND_PROGRAM, RESUME_PROGRAM, PANIC, RELEASE_ALL }; + request_type_t type; + int prog_or_chan; //if negative, all programs/channels are affected + + request_t() + { + type=NONE; + } + + request_t(request_type_t type_, int poc) + { + type=type_; + prog_or_chan=poc; + } }; +// init/uninit +void init_communication(); +void uninit_communication(); -extern pthread_mutex_t suspend_request_mutex; -extern suspend_request_t suspend_request; - +// for non-audio-threads. mutex-protected +int do_request(request_t request); +// for the audio-thread. NOT mutex-protected +bool request_available(); +request_t get_request(); +void request_finished(int); -void init_communication(); -void uninit_communication(); #endif diff --git a/synth/in_synth_cli.cpp b/synth/in_synth_cli.cpp index 4d4ef76..069e7bd 100644 --- a/synth/in_synth_cli.cpp +++ b/synth/in_synth_cli.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include "in_synth_cli.h" @@ -26,33 +25,6 @@ void signal_handler(int sig) cout << endl << PROMPT << flush; } -void do_request(int prg_no, bool susp) -{ - pthread_mutex_lock(&suspend_request_mutex); - - suspend_request.prog=prg_no; - suspend_request.suspend=susp; - suspend_request.done=false; - - pthread_mutex_unlock(&suspend_request_mutex); - - - - while (true) - { - usleep(100000); - - pthread_mutex_lock(&suspend_request_mutex); - if (suspend_request.done) - { - pthread_mutex_unlock(&suspend_request_mutex); - break; - } - else - pthread_mutex_unlock(&suspend_request_mutex); - } -} - void lock_and_load_program(int prg_no, string file) { pthread_mutex_lock(&prog_load_mutex); @@ -61,7 +33,7 @@ void lock_and_load_program(int prg_no, string file) remove_watch(prg_no); #endif - do_request(prg_no, true); + do_request(request_t(request_t::SUSPEND_PROGRAM, prg_no)); if (load_program(file,program_settings[prg_no])) { @@ -78,7 +50,7 @@ void lock_and_load_program(int prg_no, string file) for (int i=0;imaybe_reload_program(prg_no); - do_request(prg_no, false); + do_request(request_t(request_t::RESUME_PROGRAM, prg_no)); pthread_mutex_unlock(&prog_load_mutex); } @@ -89,7 +61,7 @@ void lock_and_load_program_no_watch_updates(int prg_no, string file) { pthread_mutex_lock(&prog_load_mutex); - do_request(prg_no, true); + do_request(request_t(request_t::SUSPEND_PROGRAM, prg_no)); if (load_program(file,program_settings[prg_no])) { @@ -102,20 +74,20 @@ void lock_and_load_program_no_watch_updates(int prg_no, string file) for (int i=0;imaybe_reload_program(prg_no); - do_request(prg_no, false); + do_request(request_t(request_t::RESUME_PROGRAM, prg_no)); pthread_mutex_unlock(&prog_load_mutex); } void lock_and_change_lfo(int lfo_no, float freq) { - do_request(-1, true); + do_request(request_t(request_t::SUSPEND_PROGRAM, -1)); uninit_lfo(lfo_no); lfo_freq_hz[lfo_no]=freq; init_lfo(lfo_no); - do_request(-1, false); + do_request(request_t(request_t::RESUME_PROGRAM, -1)); } @@ -183,13 +155,12 @@ void do_in_synth_cli() else if (command=="panic") { if ((params=="") || (params=="all")) - for (int i=0;ipanic(); + do_request(request_t(request_t::PANIC, -1)); else if (isnum(params)) { num=atoi(params.c_str()); if ((num>=0) && (numpanic(); + do_request(request_t(request_t::PANIC, num)); else cout << "error: channel-number must be one of 0.."<release_all(); + do_request(request_t(request_t::RELEASE_ALL, -1)); else if (isnum(params)) { num=atoi(params.c_str()); if ((num>=0) && (numrelease_all(); + do_request(request_t(request_t::RELEASE_ALL, num)); else cout << "error: channel-number must be one of 0.."<kill_program(prog); } -void process_request() +void process_request(request_t request) { - if (suspend_request.prog==-1) - for (int i=0;i<128;++i) - manage_program_lock(i,suspend_request.suspend); - else - manage_program_lock(suspend_request.prog,suspend_request.suspend); + switch (request.type) + { + case request_t::NONE: break; + + case request_t::SUSPEND_PROGRAM: + case request_t::RESUME_PROGRAM: + if (request.prog_or_chan==-1) + for (int i=0;i<128;++i) + manage_program_lock(i,request.type==request_t::SUSPEND_PROGRAM); + else + manage_program_lock(request.prog_or_chan,request.type==request_t::SUSPEND_PROGRAM); + + break; + + case request_t::PANIC: + if (request.prog_or_chan==-1) + for (int i=0;ipanic(); + else + channel[request.prog_or_chan]->panic(); + + break; + + case request_t::RELEASE_ALL: + if (request.prog_or_chan==-1) + for (int i=0;irelease_all(); + else + channel[request.prog_or_chan]->release_all(); + + break; + + default: + cout << "ERROR: bad request, ignoring it." << endl; + } - suspend_request.done=true; + request_finished(0); } @@ -217,10 +247,10 @@ int xrun_callback(void *notused) if (history.size() >= xrun_n) { - cout << "PANIC -- TOO MANY XRUNs! killing all voices" << endl<panic(); - + cout << "PANIC -- TOO MANY XRUNs! killing all voices" << flush; + do_request(request_t(request_t::PANIC, -1)); + cout << " (done)" << endl << endl; + history.clear(); } @@ -254,11 +284,9 @@ int process_callback(jack_nframes_t nframes, void *notused) } - - pthread_mutex_lock(&suspend_request_mutex); - if (suspend_request.done==false) - process_request(); - pthread_mutex_unlock(&suspend_request_mutex); + if (request_available()) + process_request(get_request()); + -- cgit v1.2.3