mixed switch and select structure

Technical questions regarding the xTIMEcomposer, xSOFTip Explorer and Programming with XMOS.
omega7
Active Member
Posts: 32
Joined: Thu Jun 03, 2010 12:16 pm

mixed switch and select structure

Postby omega7 » Wed Jul 14, 2010 10:06 am

Hi!

A simple question for an XC expert.... What happens when in a single thread multiple select statements exist, depending on a switch state?

Like this:

Code: Select all

while(1)
{
  switch(state)
  {
  case INIT:
    select
    {
      case bla1:  break;
      case bla2:  break;
    } break;

  case BUSY:
    select
    {
      case bla3:  break;
      case bla4:  break;
    } break;
  }
}
User avatar
Woody
XCore Addict
Posts: 165
Joined: Wed Feb 10, 2010 2:32 pm

Postby Woody » Wed Jul 14, 2010 11:24 am

Each time the code repeats the while loop the switch is performed, so either the bla1/2 or the bla3/4 select statement will be issued that time around the loop. The code may pause at the select statement waiting for either the bla1 or bla2 (during IDLE) to occur. When one of them does its code is executed and the while loop repeats.

Note that if both of the select conditions become true at the same time then one or the other will execute (you do not know which)
omega7
Active Member
Posts: 32
Joined: Thu Jun 03, 2010 12:16 pm

Postby omega7 » Wed Jul 14, 2010 12:23 pm

Thanks Woody,
Do you mean that 'bla3' may be executed when in state INIT?
If so, what is your idea for preventing this?
And, what happens when I add a 'default' to the select?
Martin
User avatar
Berni
Respected Member
Posts: 363
Joined: Thu Dec 10, 2009 10:17 pm

Postby Berni » Wed Jul 14, 2010 3:14 pm

Adding a default makes it not wait for any of the conditions. So if none of the conditions are true it will simply go to default instead of wait. Default is often left empty so it simply keeps the code executing.

A very useful use for the default is waiting for data on a channel while still doing other stuff in a loop. In that case you just put a single case in the select along with the default and that makes it keep on going if no data is in the channel rather than stop and wait for data.
User avatar
Woody
XCore Addict
Posts: 165
Joined: Wed Feb 10, 2010 2:32 pm

Postby Woody » Thu Jul 15, 2010 8:49 am

omega7 wrote:Do you mean that 'bla3' may be executed when in state INIT?
No.
In state INIT only either bla1 or bla2 can execute.
In state BUSY only either bla3 or bla4 can execute.
omega7 wrote:And, what happens when I add a 'default' to the select?
As Berni says, if you add a default to a select then if neither of the cases are true when the select is first run, then the default code will execute. If there is no default the execution of code on the thread will pause until one of the cases occur.

Who is online

Users browsing this forum: No registered users and 5 guests