To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 7799
7798  |  7800
Subject: 
bug in semaphore code (was Re: Announce: NQCIPC update
Newsgroups: 
lugnet.robotics
Date: 
Fri, 29 Oct 1999 23:49:53 GMT
Viewed: 
1513 times
  
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



Message has 1 Reply:
  Re: bug in semaphore code (was Re: Announce: NQCIPC update
 
(...) Robert, Cool stuff. I just added it here: (URL) (25 years ago, 30-Oct-99, to lugnet.robotics)

Message is in Reply To:
  Announce: NQCIPC update
 
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 (...) (25 years ago, 29-Oct-99, to lugnet.robotics)

6 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