To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.nqcOpen lugnet.robotics.rcx.nqc in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / NQC / 180
179  |  181
Subject: 
Re: NQC 2.0 and some math questions
Newsgroups: 
lugnet.robotics.rcx.nqc
Date: 
Sat, 2 Oct 1999 10:42:32 GMT
Viewed: 
2638 times
  
Dave Baum wrote:

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).

This only works if |= is atomic, though. Is it?

Cheers,

Ben.

--
http://www.apache-ssl.org/ben.html

"My 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 competition there."
     - Indira Gandhi



Message has 1 Reply:
  Re: NQC 2.0 and some math questions
 
(...) Yes, |= is atomic. At the bytecode level you always OR from some source into a destination variable. Dave (25 years ago, 2-Oct-99, to lugnet.robotics.rcx.nqc)

Message is in Reply To:
  Re: NQC 2.0 and some math questions
 
(...) 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); // (...) (25 years ago, 2-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
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR