select statement in a thread
-
- XCore Legend
- Posts: 1274
- Joined: Thu Dec 10, 2009 10:20 pm
Agreed this is good stuff, you should add a page to the wiki about select using your work above Bianco.
-
- Experienced Member
- Posts: 126
- Joined: Fri Feb 12, 2010 10:31 pm
Would someone be willing to check me on this? My servo library uses a couple of nested selects to iterate over the servo lines on the output port. Originally I'd thought that using a select with a "tmr when timerafter :> blah" would prevent it from blocking. However, it seems like in the second select statement a default case is needed to force it to be non-blocking (and otherwise a waiteu is issued).
In my for loop, if the default case doesn't exist in the select statement, would all the later loop iterations be waiting on the first timer?
Thanks!
In my for loop, if the default case doesn't exist in the select statement, would all the later loop iterations be waiting on the first timer?
Code: Select all
void servo_handler (out port servo_port, chanend cmd_line) {
timer tmr;
unsigned int time, durations[8];
unsigned char servo_state = 0;
unsigned int i;
servo_command instructions;
for (i = 0; i < 8; i++) {
durations[i] = SERVO_CENTRE_DURATION;
}
tmr :> time;
while (1) {
select {
case cmd_line :> instructions:
parse_instructions(instructions, durations);
break;
case tmr when timerafter(time + SERVO_REFRESH_PERIOD) :> void:
time += SERVO_REFRESH_PERIOD;
servo_state = 0xff;
servo_port <: servo_state;
break;
default:
for (i = 0; i < 8; i++) {
select {
case tmr when timerafter(time+durations[i]) :> void:
servo_state &= !(1 << i);
servo_port <: servo_state;
break;
default: // <-- This block is what's in question.
break; // <-- i.e. Whether or not to include the default case.
}
}
break;
}
}
-
- XCore Legend
- Posts: 1274
- Joined: Thu Dec 10, 2009 10:20 pm
It looks correct to me, the second default is required to avoid it waiting on whatever duration[0] is and hence blocking duration[1..7].
If you had the durations sorted lowest first you could use a blocking (no default) select perhaps..
P.S. I love your servo technique, its neat
regards
Al
If you had the durations sorted lowest first you could use a blocking (no default) select perhaps..
P.S. I love your servo technique, its neat
regards
Al
Last edited by Folknology on Fri Dec 03, 2010 8:07 pm, edited 1 time in total.
-
- Experienced Member
- Posts: 126
- Joined: Fri Feb 12, 2010 10:31 pm
Thanks Al,Folknology wrote:It looks correct to me, the second default is required to avoid it waiting on whatever duraction[0] is and hence blocking duration[1..7].
If you had the durations sorted lowest first you could use a blocking (no default) select perhaps..
P.S. I love your servo technique, its neat
regards
Al
Actually, I might have to try the sorting technique. I'd have to see how quickly I can do the math in the instruction parsing phase, so that none of the servos missed a timing signal, but it would significantly reduce the processing overhead from the infinite loop. Although, that would come with more latency on the command thread waiting to be acknowledged, perhaps as much as 2 ms I suppose.
Cheers,
Brendan