here's my problem: I'm developing a multithreaded application composed by:
I have implemented a button widget, that only receives signals from the helper and RT thread, that modify the gui in its callback function.
So, my question is: who is modifying the GUI? the helper/RT thread or the gui thread in which i used gtk_main() ?
Thanks for your collaboration!
edit: i added the code /** @file JPLowPassFilter.c * * @brief this is a simple client that implements a numeric low Frequency Pass Filter */
#include "JPLowPassFilter.h"
jack_port_t *input_port;
jack_port_t *output_port;
jack_default_audio_sample_t tmp;
int first=1;
appData* mainData;
jack_default_audio_sample_t tmp;
/*Code for port_registration_callback */
void registrationPort(jack_port_id_t port, int reg, void *arg)
{
return;
}
/*Code for client_registration_callback */
void registrationClient(const char* name, int reg, void *arg)
{
return;
}
/**
* The process callback for this JACK application is called in a
* special realtime thread once for each audio cycle.
* Must not block!
*/
int process (jack_nframes_t nframes, void *arg)
{
int i;
float alfa=mainData->alfa;
jack_default_audio_sample_t *in, *out;
in = jack_port_get_buffer (input_port, nframes);
out = jack_port_get_buffer (output_port, nframes);
for( i=0; i<nframes; i++) {
if(first==1){
tmp=in[i];
first=0;
}
else{
tmp=tmp*alfa+(1.0f-alfa)*in[i];
//tmp=tmp*(1.0f-alfa)+alfa*in[i];
}
out[i]=tmp;
}
//fprintf (stderr, ".");
return 0;
}
/**
* JACK calls this shutdow_callback if the server ever shuts down or
* decides to disconnect the client
*/
void jack_shutdown (void *arg)
{
mainData->state=NOT_WORKING;
jack_port_unregister(mainData->client,input_port);
jack_port_unregister(mainData->client, output_port);
g_signal_emit_by_name (GTK_BUTTON(mainData->init),"clicked");
}
/* JACK calls this function whenever there is an xrun */
int xrun_function(void *arg)
{
fprintf (stderr, "--XRUN OCCURRED--\n");
}
void* threadCode(void* val)
{
const char *client_name = CLIENT_NAME;
const char *server_name = NULL;
mainData=(appData*) val;
/* if server isn't present, don't start it!*/
jack_options_t options =JackNoStartServer;
jack_status_t status;
do{
/* try to open a client connection to the JACK server */
mainData->client = jack_client_open (client_name, options, &status, server_name);
if (mainData->client == NULL)
{
fprintf (stderr, "jack_client_open() failed, "
"status = 0x%2.0x\n", status);
if (status & JackServerFailed) {
fprintf (stderr, "Unable to connect to JACK server\n");
}
sleep(3);
}
else
{
fprintf (stderr, "Connected to JACK server\n");
mainData->state=INIT;
/* CALLBACKS*/
jack_set_process_callback (mainData->client, process, 0);
jack_on_shutdown (mainData->client, jack_shutdown, mainData);
jack_set_xrun_callback(mainData->client,xrun_function, 0);
jack_set_port_registration_callback (mainData->client, registrationPort,NULL);
jack_set_client_registration_callback(mainData->client, registrationClient,NULL);
/* PORTS */
input_port = jack_port_register (mainData->client, "input", JACK_DEFAULT_AUDIO_TYPE,JackPortIsInput, 0);
output_port = jack_port_register (mainData->client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
mainData->state=INIT;
if ((input_port == NULL) || (output_port == NULL))
{
fprintf(stderr, "no more JACK ports available\n");
mainData->state=NOT_WORKING;
}
/* STARTS */
else if (jack_activate (mainData->client)) {
fprintf (stderr, "cannot activate client");
jack_port_unregister(mainData->client,input_port);
jack_port_unregister(mainData->client, output_port);
mainData->state=NOT_WORKING;
}
else
{
fprintf (stderr, "Client ready to Run\n");
mainData->state=WORKING;
mainData->portsName[0]=jack_port_name(input_port);
mainData->portsName[1]=jack_port_name(output_port);
}
//can be written to
mainData->inputList=jack_get_ports(mainData->client,NULL, NULL,JackPortIsInput);
//can be read from
mainData->outputList=jack_get_ports(mainData->client,NULL, NULL,JackPortIsOutput);
g_signal_emit_by_name (GTK_BUTTON(mainData->init),"clicked");
while(mainData->state==WORKING)
{
sleep(5);
fprintf (stderr, ".");
}
fprintf (stderr, "\n");
jack_free(mainData->inputList);
jack_free(mainData->outputList);
mainData->outputList=NULL;
mainData->inputList=NULL;
jack_client_close (mainData->client);
}
fprintf (stderr, "---RECONNECT---\n");
}while(mainData->state==NOT_WORKING);
fprintf (stderr, "Ended!\n");
pthread_exit(NULL);
}
Following the comments, a suggestion would be to exchange the g_signal_emit_by_name
functions for g_async_queue_*
functions.
Let suppose that mainData->init
points to a GAsyncQueue created in the main thread instead of the actual button.
Then you could use on your thread:
g_async_queue_push(G_ASYNC_QUEUE(mainData->init), data);
Data can contain a simple flag to indicate the status and/or status change
Then on your main thread, when you setup the UI, you could add the idle handler with:
my_queue = g_async_queue_new();
...
g_idle_add ((GSourceFunc) check_async_queue, my_queue);
and your check_async_queue
could be something like this:
gboolean check_async_queue (gpointer user_data) {
gpointer queue_data;
queue_data = g_async_queue_try_pop (G_ASYNC_QUEUE(user_data));
if (queue_data != NULL) {
// We have data, do something with 'queue_data'
// and update GUI
} else {
// no data, probably do nothing
}
return TRUE; // can be G_SOURCE_CONTINUE instead of TRUE
}
The return value will indicate if the check_async_queue
function should keep running or not, so you can have a condition to remove the function.
This will allow you to have a simple one way message queue that you can use to pass information from the worker thread to the main thread.