Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
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[Idx] := long[Idx]
  lockclr(SemID)                            'unlock the resource

PUB WriteResource | Idx 
  repeat until not lockset(SemID)           'wait until we lock the resource
  repeat Idx from 0 to 9                    'write all 10 longs to resource
    long[Idx] := LocalData[Idx]
  lockclr(SemID)                            'unlock the resource 

Both of these methods, ReadResource and WriteResource, follow the same rules before and after accessing the resource. First, they wait indefinitely at the first REPEAT loop until it has locked the resource; i.e., it has successfully "set" the associated lock. If LOCKSET returns TRUE, the condition "until not lockset…" is FALSE, meaning that some other cog is currently accessing the resource, so that first repeat loop tries again. If LOCKSET returns FALSE, the condition "until not lockset…" is true, meaning we have "locked the resource" and the first repeat loop ends. The second REPEAT loop in each method reads or writes the resource, via the long[Idx] and LocalData[Idx] statements. The last line of each method, lockclr(SemID), clears the resource's associated lock to FALSE, logically unlocking or releasing the resource for others to use.

...