Shop OBEX P1 Docs P2 Docs Learn Events
Lockset Question? — Parallax Forums

Lockset Question?

eldonb46eldonb46 Posts: 70
edited 2012-07-09 15:51 in General Discussion
If multiple COGs are executing:
repeat until not lockset(SemID)

Is the order of COGs successful getting the lock deterministic?

Or, does the execution of the "repeat" code effectively randomize the results?


Thanks,
Eldon

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-07-08 20:42
    It's deterministic, but maybe not predictable -- IOW, from a practical standpoint, a crapshoot if other Spin cogs are vying for the same lock.

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2012-07-08 21:44
    It's deterministic, but maybe not predictable ....
    Precise but not accurate?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-07-08 21:58
    Possibly even chaotic.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-07-09 08:54
    The loop execute 5 Spin bytecodes, which take a total of 884 cycles per loop. The cog that gets the lock will be the first one that executes the lockset after another cog has executed a lockclr. Assuming that each cog entered the loop at a random time, the winning cog will be randomly selected. However, if all the cogs entered the loop at the same time, and the cog that executes the lockclr was syncronized with that event, then the same cog would always win.
  • Mark_TMark_T Posts: 1,981
    edited 2012-07-09 13:44
    The minimal PASM loop would be 8 cycles min for the lockset, 4 cycles for the loop back, but after the first attempt would be hub-synchronized to 16 cycle repeat and thus the order of cogs precisely determines the winner. Perhaps Spin should have had a lockclaim primitive that did this busy-wait in PASM? (called waitlock?)
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-07-09 13:55
    Yes, if you have a mix of Spin and PASM cogs trying to acquire the lock, the PASM cogs will get it most of the time. In some situations a more complex scheme may be needed to ensure that all of the cogs get their turn to access a common resource.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-07-09 15:51
    A more "polite" way to code it would be that, if you don't get the lock, do a random timeout before retrying.

    -Phil
Sign In or Register to comment.