Locks

There are eight lock bits (also known as semaphores) available to facilitate exclusive access to user-defined resources among multiple cogs. If a block of memory is to be used by two or more cogs at once and that block consists of more than one long (four bytes), the cogs will each have to perform multiple reads and writes to retrieve or update that memory block. This leads to the likely possibility of read/write contention on that memory block where one cog may be writing while another is reading, resulting in misreads and/or miswrites.
The lock bits exist inside the Hub (outside of Main RAM and Cog RAM) but are globally accessible by every cog through the hub instructions: LOCKNEW, LOCKRET, LOCKSET, and LOCKCLR. These special instructions provide an effective control mechanism because only one cog at a time can execute them and the LOCKSET and LOCKCLR instructions each perform a read and write as one atomic operation. The LOCKNEW and LOCKRET instructions provide a centralized way for cogs to check out a lock for a new purpose, or return a lock that is no longer in use. See LOCKNEW, ; LOCKRET, ; LOCKSET, ; and LOCKCLR, for more information.

Unless otherwise noted, content on this site is licensed under the
Creative Commons Attribution-ShareAlike 4.0 International License.