Event handling with channels

All technical discussions and projects around startKIT
Post Reply
rebb
Member
Posts: 9
Joined: Thu Jun 25, 2015 12:14 pm

Event handling with channels

Post by rebb »

What is the best practice to write event based system using channels?
I now have UIThread that handles all physical buttons and handling screen updates.
I listen button and if that is pressed it sends 1 to channel that is in listened in another thread that calculates audio.

Problem is that if i listen same channel in UIThread, with case (like case: 2 { do something}) it stalls until i send something back on that channel. If i send random number back, UIThread keeps running on while loop, but not reacting to that anymore.

So to clarify:

I have setup my channels like:
uiHandler(uiTrigger);
trigger(uiTrigger,c_synth_audio);

When I have send data to channel from UIHandler it should check when it gets value 2 back from that channel and then print something to screen. Once i have send value to channel, trigger starts playing audio as it should, but uiHandler is stuck until it get data back from channel.


peter
XCore Addict
Posts: 230
Joined: Wed Mar 10, 2010 12:46 pm

Post by peter »

Could do with a bit more information, but let's say you have the following:

Code: Select all

void uiThread(chanend c)
{
  while (1) {
    if (button) {
      c <: 2;
    }
    ....
  }
}

void trigger(chanend c, streaming chanend audio) {
  while (1) {
    select {
      case c :> int cmd:
        // Process sample
        ...
        break;
      case audio :> int sample:
        // Process sample
        ...
        break;
    }
  }
}

int main() {
  chan c;
  streaming chan audio;
  par {
    audioProducer(audio);
    uiThread(c);
    trigger(c, audio)
  }
}
There are two things to note, the control channel (c) will be handshaken, so that the uiThread and trigger thread will continue simultaneously only once the uiThread has input the token. There is no need to pass anything back to the uiThread.

The audio channel is marked as streaming, so there is no such synchronisation (handshake) and the audio data will have simply been posted to the channel.

If that example doesn't help them maybe give a more complete listing of your code to help us understand what you are are trying to achieve.
rebb
Member
Posts: 9
Joined: Thu Jun 25, 2015 12:14 pm

Post by rebb »

Found a bug that was stalling my code, which caused strange behaviours like could not update screen while calculating samples.

I copied whole buffer at once thru channel in c code rather than just copy one sample at time. After i changed this behaviour everything seems to work like a charm again.

If i'm calling c function from thread, is that c code run in that calling thread or does it make up a thread of it's own? As it seemed to "steal" cpu time from other cores, or otherwise stalling the whole system.
Post Reply