Shop OBEX P1 Docs P2 Docs Learn Events
Propeller Locks in PASM - what are LockNew and LockRet for? — Parallax Forums

Propeller Locks in PASM - what are LockNew and LockRet for?

WossnameWossname Posts: 174
edited 2015-03-11 09:54 in Propeller 1
What's the reasoning behind the LockNew and LockRet instructions? I guess you'd have to use them if you have more than 8 resources to guard, but is that the only reason?

If I only have 3 resources to guard, is it safe to omit LockNew and LockRet? I'd like to do away with them because I'm trying to free up a few lines of code space here and there in a particularly busy Cog running PASM code.

Comments

  • Heater.Heater. Posts: 21,230
    edited 2015-03-11 09:21
    You may well need a lock even if you only have one resource to guard. Having multiple parallel processes accessing a single variable can lead to incorrect results.

    It all depends on how you are sharing a resource. Have a look at FullDuplexSerial for a fine example of how it shares cyclic buffers between writer and reader processes. Because of the way it updates the head and tail pointers into those buffers data cannot become corrupted.

    You can guard shared resources between a single reader and a single writer with simple flags as well.
  • Mike GreenMike Green Posts: 23,101
    edited 2015-03-11 09:54
    This is somewhat like the difference between COGINIT and COGNEW. There are 8 locks available in the Propeller just as there are 8 cogs available. They're numbered and you're welcome to allocate them yourself to various parts of your program. In many cases, it's more convenient to let the hardware figure out what's available and tell you like with COGNEW. Similarly, LOCKNEW assigns a lock and returns the number of the lock. This can be used with the other LOCKxxx instructions and you use LOCKRET to put that lock number back into the pool.

    It's risky to try to use both techniques in the same program. If you manually assign lock numbers in your program, don't use LOCKNEW and LOCKRET. If you use LOCKNEW and LOCKRET, don't use specific lock numbers. Similarly, if you use COGNEW, don't use COGINIT with the possible exception of using COGINIT to reload and restart a cog that you originally started with COGNEW (something that doesn't apply to locks).
Sign In or Register to comment.