Subject:
|
Re: NQC 2.0 and some math questions
|
Newsgroups:
|
lugnet.robotics.rcx.nqc
|
Date:
|
Sat, 2 Oct 1999 02:46:04 GMT
|
Viewed:
|
2610 times
|
| |
| |
In article <37F47D52.204EC5C6@algroup.co.uk>, Ben Laurie
<ben@algroup.co.uk> wrote:
> Dave Baum wrote:
> > There isn't an atomic test-and-set bytecode, so the only way I know of
> > implementing sempaphore would be to use bitflags and assign one bit per
> > task.
>
> There is a way to do it even if you have no test and set, though I can't
> remember it off the top of my head, and it is slow. Don't see how using
> bitflags helps?
I'm going from memory here, so I may miss a detail...
int lock;
#define TASK_BIT(task_num) (1 << task_num)
void acquire_lock(int task_num)
{
while(1)
{
// 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);
}
}
-----
You try to acquire the lock by setting your task's bit. The lock is only
successful if after setting the bit, your bit is the only one set. If
another task started to lock it in between your set and test, then your
test would fail. Conversely, if the other task sets after you have
completely locked, then their test will fail.
You release the lock by setting it to 0 (or perhaps just clearing your bit).
Dave Baum
--
reply to: dbaum at enteract dot com
|
|
Message has 2 Replies: | | Re: NQC 2.0 and some math questions
|
| Your proposed solution is susceptible to the "lockstep starvation" problem. Lockstep starvation happens when two tasks try to get the lock at the same time and execute the same code in lock step, like so: task 1: lock |= 2; task 2: lock |= 4; task (...) (25 years ago, 2-Oct-99, to lugnet.robotics.rcx.nqc)
| | | Re: NQC 2.0 and some math questions
|
| (...) This only works if |= is atomic, though. Is it? Cheers, Ben. -- (URL) grandfather once told me that there are two kinds of people: those who work and those who take the credit. He told me to try to be in the first group; there was less (...) (25 years ago, 2-Oct-99, to lugnet.robotics.rcx.nqc)
|
Message is in Reply To:
| | Re: NQC 2.0 and some math questions
|
| (...) There is a way to do it even if you have no test and set, though I can't remember it off the top of my head, and it is slow. Don't see how using bitflags helps? Cheers, Ben. -- (URL) grandfather once told me that there are two kinds of people: (...) (25 years ago, 1-Oct-99, to lugnet.robotics.rcx.nqc)
|
16 Messages in This Thread:
- Entire Thread on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
This Message and its Replies on One Page:
- Nested:
All | Brief | Compact | Dots
Linear:
All | Brief | Compact
|
|
|
|