| | | | |
| |
| Your semaphore acquire algorithm isn't too good. Here is your code:
#define sem_acquire(s) until (s == 0); s = 1;
If two tasks are waiting for the semaphore at the same time, it is quite likely
that both will get it:
// (s is currently nonzero)
task 1: until (s == 0);
task 2: until (s == 0);
task 3: s = 0;
task 1: until (s == 0);
task 2: until (s == 0);
task 3: // whatever
task 1: s = 1;
task 2: s = 1;
// at this point, task 1 and task 2 both think they have the semaphore
Dave Baum and I and a few others talked about this issue 4 weeks ago in this
thread:
http://www.lugnet.com/robotics/rcx/nqc/?n=172
The result was code that properly handles the lockstep starvation problem as
well as making sure that only one task uses the semaphore at any given time.
The solution requires the tasks to have task ID numbers.
I will repeat the proper code here:
int lock; // the semaphore
#define TASK_BIT(task_num) (1 << task_num)
// this is written as if subroutines were legal in NQC, actually
// you would implement it as an inline function or a macro
void acquire_lock(int task_num)
{
while(true)
{
// wait for lock to be clear
while(lock);
// try to own it
lock |= TASK_BIT(task_num);
// see if we own it
if (lock == TASK_BIT(task_num)) {
return;
} else {
lock &= ~TASK_BIT(task_num);
Sleep(task_num);
}
}
}
- Robert Munafo
In lugnet.robotics, Brian Connors writes:
> As some of you may know, about three weeks ago I
> released a package called NQCIPC that provides a basic
> API for semaphores and intertask messaging on NQC.
> Originally written for NQC 1.x, it's been updated for
> 2.x and I'd be interested in seeing what all of you
> think of it. I'd especially be interested in feature
> suggestions (if practical); I'd also like a better
> name than NQCIPC if someone has an idea (anyone like
> Spectre (by analogy with spirit.ocx)?).
>
> The home page is
>
> http://www.geocities.com/ResearchTriangle/Station/2266/nqcipc/nqcipcdoc.html
>
> and it contains everything relevant to the package.
> Check it out and let me know what y'all think.
>
> Brian Connors
| | | | | | | | | | | | | In lugnet.robotics, Robert Munafo writes:
> [...]
> Dave Baum and I and a few others talked about this issue 4 weeks ago in this
> thread:
>
> http://www.lugnet.com/robotics/rcx/nqc/?n=172
>
> The result was code that properly handles the lockstep starvation problem as
> well as making sure that only one task uses the semaphore at any given time.
> The solution requires the tasks to have task ID numbers.
>
> I will repeat the proper code here:
> [...]
Robert,
Cool stuff. I just added it here:
http://www.lugnet.com/robotics/rcx/nqc/?p=code
--Todd
| | | | | | | | | | | | | | | | | | Todd, this is CoolBut: It does not appear to appear
from NNTP. Is it your intention to have, um..
lugnet.robotics.rcx.nqc.code ??
I like the idea a lot of having a code repository...
--
Regards,
Terry King ...In The Woods In Vermont
| | | | | | | | | | | | | | | | | | What is
http://www.lugnet.com/robotics/rcx/nqc/?p=code
and why are there no links connecting the rest of
http://www.lugnet.com/robotics/rcx/nqc/ to it?
In lugnet.robotics, Todd Lehman writes:
> Robert,
> Cool stuff. I just added it here:
> http://www.lugnet.com/robotics/rcx/nqc/?p=code
> --Todd
| | | | | | |