Locks ("lock bits" or semaphores) serve as a mechanism that provides for multiprocessor synchronization of a developer-defined mutually-exclusive resource. The Propeller has eight lock bits that can each be read and written atomically; read and written as a single operation that no other cog can interfere with. This allows them to be used as flags to prevent interference between cooperative parallel processes who are sharing a sensitive resource, such as main memory.
Proper use requires all the related code to use locks cooperatively.
A memory collision occurs when two or more cogs operate on a block of memory at the same time in a way that causes conflict between them.
A cog has exclusive access to its own Cog RAM, so no memory collision between cogs can occur there. However, all cogs share Main RAM; care must be taken to avoid collisions there.
A cog can read or write a byte, word, or long from Main RAM as an atomic operation—a single operation that no other cog can interfere with. In contrast, cogs can unknowingly interfere with each other's successive atomic operations (multiple reads or writes of bytes, words, or longs from Main RAM) since their respective operations are naturally interleaved when accessing a mutually-exclusive resource.
This situation can cause problems if two or more cogs simultaneously perform multiple opposing operations on the same logical block of memory. For example, two cogs may be tasked to cooperate on data contained in a block of memory 10 bytes in length. If one cog starts writing to the 10 bytes while another is reading, timing variations in their respective program loops can easily cause the "reading" cog to read a mixture of both new byte-sized data and old byte-sized data.
To prevent such misreads or miswrites, the cogs must coordinate their efforts through an agreed-upon synchronization mechanism. This mechanism serves as a "flag" to signal when a cog is performing a non-atomic operation on a mutually-exclusive resource. All cogs involved with that resource must check and set the "flag" before proceeding, and must clear the flag to indicate when they are done.
In many situations where memory is involved, a simple solution is to designate a byte, word, or long within the memory block to serve as this synchronization flag, but since that memory-based flag can not be read and written within the same atomic operation (it requires a read operation followed by a write operation) it may not work flawlessly for all situations. For this reason, a dedicated synchronization mechanism exists, called Locks, or semaphores.
The Propeller has 8 global lock bits which can be read and written simultaneously, as a single operation. When used properly, the lock bits can facilitate synchronization between cogs for any purpose desired; not just for Main RAM usage. The lock bits are managed through four commands: LOCKNEW, LOCKSET, LOCKCLR, and LOCKRET.
No. The Propeller chip's Locks (semaphores, or "lock bits") are free for use by any cog at any time and for any reason. They are not tied to any cog or any resource, such as memory, and serve as an atomic mechanism that provides for multiprocessor synchronization of a developer-defined mutually-exclusive resource. Proper use requires all the related code to use locks cooperatively.
These instructions provide an object the ability to request a unique lock ID bit; one that is not in-use by another part of the application. Without LOCKNEW and LOCKRET (and objects that faithfully used them) each object would have to use a static bit ID from the global semaphore resource which could unintentionally collide with the bit ID some other object happened to use within the same application.
LOCKNEW is similar in concept to using COGNEW rather than COGINIT; it requests a lowest-numbered, unused resource. LOCKRET is similar in concept to using COGSTOP; it returns a used resource back to a pool for later use by the next requestor.
No, it is required that the code use a LOCKRET command on any semaphore "lock bit" to release it back to the pool of available semaphores.
Propeller P8X32A Questions & Answers
Copyright © Parallax Inc., dba Parallax Semiconductor
Version 1.3.1
10/23/2013