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 / 449
448  |  450
Subject: 
Re: RCX Programming Questions and Sensor Ideas
Newsgroups: 
lugnet.robotics
Date: 
Wed, 11 Nov 1998 13:07:27 GMT
Original-From: 
Paul Haas <paulh@%SayNoToSpam%hamjudo.com>
Viewed: 
3190 times
  
On Wed, 11 Nov 1998, Ben Laurie wrote:

It isn't guaranteed to work, either, though it is likely to in the end.
This is because two processes can conspire to always have r31 == 2 when
you test for r31 == 1.

Oops.

I don't know how the scheduler in the RCX works.  It might execute 1
opcode from each task in order. If 2 tasks get in sync, then they will
never leave the semaphore code as written.

Here's a fixed version.  I added a random backoff.

Paul Haas wrote:
2. You can use a counter, because += 1 and -= 1 are atomic operations.

    int r31;
    int done;
    r31 = 0;

    // stuff happens.
    // Heres our semaphore stuff.
    done = 0;  // We're not done yet.

    while ( ! done ) {
         while ( r31 != 0 ) {
         } ; // busy wait.
        r31 += 1;  // This operation is atomic.
        if ( r31 == 1 ) {  // Were we the only process to touch the counter?
             // Do critical section stuff here.
             done = 1;
        }
            else {
                sleep(Random(10));
            }
        r31 -= 1;  // Remove our reservation.
   }

--
paulh@hamjudo.com  http://www.hamjudo.com
The April 97 WebSight magazine describes me as "(presumably) normal".



Message has 1 Reply:
  Re: RCX Programming Questions and Sensor Ideas
 
(...) Paul, Are you sure you want to sleep before decrementing r31? As written, r31 is always >= 1 during the sleep -- which prevents any other tasks from obtaining a lock on the resource during this sleep time. In other words, once any two tasks (...) (26 years ago, 11-Nov-98, to lugnet.robotics)

Message is in Reply To:
  Re: RCX Programming Questions and Sensor Ideas
 
(...) It isn't guaranteed to work, either, though it is likely to in the end. This is because two processes can conspire to always have r31 == 2 when you test for r31 == 1. Cheers, Ben. (26 years ago, 11-Nov-98, to lugnet.robotics)

17 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
    
Active threads in Robotics

 
Verified and Trusted Team of Hackers
27 minutes ago
Custom Search

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