Shop OBEX P1 Docs P2 Docs Learn Events
Cogs and locks — Parallax Forums

Cogs and locks

rtarbellrtarbell Posts: 7
edited 2007-10-06 15:10 in Propeller 1
Hello all,

I wanted to make sure that I understand this concept of locks before I move on. My apologies if my question doesn't exactly make sense:

Are locks used to lock up variables, or methods, or objects, or all three? As in, if I have a cog, and I want that cog to operate on a long variable, can I have the cog check out a lock on that variable so that no other cog can operate on it?
Same question with a method?
Same question with an object?

The manual says that locks can lock up resources, but what exactly is a resource?

Comments

  • deSilvadeSilva Posts: 2,967
    edited 2007-10-05 19:32
    "Locks" can't do anything of all the things you mention... It will take some time to explain, and it will depend on your pre-knowledge. I spent a whole chapter of my Tutorial on Machine Language on it, though it basically has nothing to do with machine code....
    But I have an example in that chapter and some SPIN code as well.....

    Post Edited (deSilva) : 10/5/2007 7:39:57 PM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-05 19:54
    All right, to make it simple: To add "one" to a variable means:
    - reading it
    - adding one
    - storing it away
    When you have two parallel processes, A and B, incrementing the same variable, their accesses might conflict:

    - A reading it
    - A adding one
    - B reading it
    - B adding one
    - A storing it away
    - B storing it away

    Now it will be incremented only once rather than twice...

    To synchronize such situations, "semaphores" have been invented. A semaphore (which is a technical term in computer science) is called "lock" with the propeller.

    There are eight locks only so you have to be very picky as to which accesses to synchronize.

    What you have to do is:

    (a) One of the involved processes must reserve a lock (locknmb := LOCKNEW)
    (b) When any process wants to modifiy the bespoken variable, it has to wait until a nothr process has completed an concurrent access and stop any one cooperating in that scheme to make a modifying access:
    REPEAT WHILE LOCKSET(locknmb)
    


    (c) When the modification is completed, the program has to call
    LOCKCLR(locknmb)
    



    Note that the "locking" applies only to "co-operating" processes; there is no enforecement of anything , as the connection between the locknmb and the variable to be "locked" or whatever action, is arbitrary and volatile...

    Post Edited (deSilva) : 10/5/2007 8:01:45 PM GMT
  • TookingsTookings Posts: 18
    edited 2007-10-05 20:18
    Right -- the "semaphore" name comes from the the visual signal of the same name. www.carrtracks.com/sspicndx.htm ...but goes back to the 1600's I think.

    ...and you could ignore them just the same. If all processes accessing the variable don't check the lock before accessing the variable, nothing is stopping the process from ignoring the "lock" and munging the data. It's really just special use bit flag, I think (?).
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-05 20:23
    Tookings said...
    It's really just special use bit flag, I think (?).

    You are basically right, but it's not "just", it's quite complex smile.gif
    The "read and modify" must be an inseparable action, needing special HUB logic - as you nowadays can even see in the photograph of th chip smile.gif
  • TookingsTookings Posts: 18
    edited 2007-10-05 20:32
    You're right...or else, I suppose, the semaphore would have the same locking problem as the variable you were trying to protect in the first place. smile.gif
  • JoeCreateJoeCreate Posts: 36
    edited 2007-10-05 21:40
    So, you can acquire a lock to update some variables, but if any other processes that update that variable don't adhere to the locking, then you might as well not have locked it in the first place because the variable can still be updated by any process, locked or not. Correct? (I'm just talking Propeller, not any other OS)

    How differently does this behave from settings a variable (bit flag or whatever) in Hub ram that the other cogs would check to make sure it's okay or not to update a specific Hub ram variable? It sounds like the LOCKSET just allows a Cog to Check and Set a "lock flag" in one step, as opposed to a Cog reading a Hub ram variable(flag) to determine a lock, then Setting it if not locked, but in-between the read and write some other Cog could have been doing the same thing.

    eh?
  • JoeCreateJoeCreate Posts: 36
    edited 2007-10-05 21:45
    If two separate Cogs are updating a shared variable in Hub ram using Spin "MyVariable++", should Locks be used by the cogs before doing this?
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-05 21:50
    JoeCreate said...
    If two separate Cogs are updating a shared variable in Hub ram using Spin "MyVariable++", should Locks be used by the cogs before doing this?
    Of course! That's what it's all about!
    Don't let you be fooled by the simple syntax. "myVar++" will take around 10 mys which means that around 200 machine instructions will be spent for it smile.gif

    But even in machine code you would need two turns of the HUB wheel: one for reading the variable, and one to store it. Ample opportunities for ALL other COGs to cross your best designs smile.gif :-

    Post Edited (deSilva) : 10/5/2007 10:02:59 PM GMT
  • JoeCreateJoeCreate Posts: 36
    edited 2007-10-05 22:02
    deSilva: That's pretty much the point I was trying to make (or get You to make). You can't update a Hub variable from a Cog to use as a Lock because the process of Checking and Setting are many instructions apart. Hence LOCKSET, so you know no other Cogs were reading or writing the LOCK "variable" at the same time.
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-05 22:11
    You might enjoy reading this: greenteapress.com/semaphores/
    It contains the most important information about semaphores on 200+ pages smile.gif
  • RicardoPRicardoP Posts: 13
    edited 2007-10-05 22:12
    Do I need a Lock for this? I have a cog doing a mov into main ram and another cog doing a mov from main ram (same address) I figure this is an atomic instruction so I didnt bother with locks, seems to work correctly.
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-05 22:21
    No, you don't need semaphores, as it's not both writing!

    But you need some caution IF IT IS NOT JUST WORD (=LONG) you are reading and writing!

    The critical scenario would be as follows:
    The writer updates word A and B, for some reason having a wait after having written A.
    The reader reads A and THE OLD VERSION of B.

    This however is not a typical semaphore problem; you could use a "system lock", but you can easily work with a "private lock" C: Whenever the writer updates A and B it previously sets C and resets C after A AND B have been updated.
    The reader waits, it it finds the "private lock" C set.

    This is a very common use by many COG drivers - see FLOAT32 as a simple example.

    Why does it work? Simply because the writer is THE ONLY ONE setting and resetting C - no conflict possible.

    Post Edited (deSilva) : 10/5/2007 10:27:16 PM GMT
  • rtarbellrtarbell Posts: 7
    edited 2007-10-06 14:58
    Okay,

    Let me ask this (assuming I am programming in SPIN language):

    Let's say I have a variable I call AAA, and a stack space for another cog called newstack[noparse][[/noparse]20]
    And a PUB method called arithmetic_add (takes in one variable, adds 3 to it, and returns a result)

    PUB arithmetic_add(input)
    output := input + 3


    Then, in my main method, I call a function on a new cog:

    cognew(arithmetic_add(AAA), @newstack)

    1) If I have lots of other cog process running (let's assume I plan on using all cogs), what code for a lock would I write to protect my variable AAA such that no other function or method can use AAA until arithmetic_add is done with it?

    2) If I have another method called arithmetic_subtract, what code would I write to test whether variable AAA is locked or not?

    3) What does the @ symbol mean when referencing variables?
  • deSilvadeSilva Posts: 2,967
    edited 2007-10-06 15:10
    All your three questions have nothing to do with locks or semaphores...What is a pitty as deSilva was so happy having explained semaphores :-(

    (1) As long as you use routine (method) parameters a copy of them is made when calling the routine. It might become more complex when this parameter is a pointer and you are using the memory vector pointed to. Then you can use one of the methods discussed above, depending on whether only one writes to it, or many do.

    (2) See above, AAA is "read only"

    (3) @ means just what you say: It gives you the address of that variable, so you can access it vie
    BYTE[noparse][[/noparse]pointer]
    or
    WORD[noparse][[/noparse]pointer]
    or
    LONG[noparse][[/noparse]pointer]
    or many more variations...
    Just edited some most extraordinary typos smile.gif.

    Post Edited (deSilva) : 10/6/2007 3:19:24 PM GMT
Sign In or Register to comment.