Shop OBEX P1 Docs P2 Docs Learn Events
How to use Locks? — Parallax Forums

How to use Locks?

KyeKye Posts: 2,200
edited 2009-12-30 23:14 in Propeller 1
I'm trying to get my head arround the locks mentioned in the prop manual and the description seems just a bit confusing it.

The manual uses this code:
PUB ReadResource | Idx 
repeat until not lockset(SemID) 'wait until we lock the resource 
repeat Idx from 0 to 9 'read all 10 longs of resource 
LocalData[noparse][[/noparse]Idx] := long[noparse][[/noparse]Idx] 

lockclr(SemID) 'unlock the resource 




However "lockset" "sets a semaphore to true and gets its previous state" which after one execution of the "repeat until not" (repeat while)·loop above the lock mechanism would be pointless.

So,·is this assumption correct... that the "lockset" command·only sets the lock to true if the previous state was false? This kind of·operation would make sense.



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,

Comments

  • JonnyMacJonnyMac Posts: 9,208
    edited 2009-12-30 22:14
    1 or 1 = 1 -- if the lock was already set then lockset(id) has no effect. The reason for returning the previous state is that we can determine that the lock was just set and we now have *safe* access to the target resources. I think lockset(id) was designed this way to eliminate the need for a getlock(id) method.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-12-30 22:15
    Kye said...
    So, is this assumption correct... that the "lockset" command only sets the lock to true if the previous state was false?
    Yes, sort of. Another way to look at it is this: the side effect of calling lockset is always a set lock. The return value tells you who "owns" the set lock (== 0: you, the caller; <> 0: someone else). That's why you have to wait until the return value is zero, which indicates that you now own the lock.

    -Phil

    Addendum: Come to think of it, lockset would have been much less confusing if its results were reversed: true if the lock was successfully obtained; false, otherwise.

    Post Edited (Phil Pilgrim (PhiPi)) : 12/30/2009 10:25:00 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2009-12-30 22:18
    LOCKSET sets the lock bit to true always. It also tests (and returns) the state of the lock bit before it sets it and the test and the set are indivisible.

    The REPEAT is correct. It requires that the lock be clear for one iteration of the REPEAT (at which point the REPEAT exits) and the lock has to have been set when the repeat exits.
  • KyeKye Posts: 2,200
    edited 2009-12-30 23:14
    Thanks,

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
Sign In or Register to comment.