PREV UP NEXT StackThreads/MP: version 0.77 User's Guide

3.4: Spin-Locks

StackThreads/MP provides a simple set of spin-lock primitives. A location can be atomically read and locked by read-and-lock primitives. They wait for the location to become unlocked and then atomically lock the location. They return the value previously written in the location. A locked location can be unlocked with write-and-unlock primitives. They take a value and a locked location as the arguments, write the value to the location, and unlock the location. The location must be locked or the behavior is undefined. These primitives are useful for performing atomic read-modify-write on a single location without a separate ``lock'' word. In addition a value can be read even when the location is currently locked.

CAUTION: All locking primitives described below (st_read_and_lock_xxx) fail after certain number of locking attempts. When they fail, the program terminates after printing stack trace information. This is typically a good thing, as it makes debugging easier (a simple error that forgets to unlock a location is detected at the next lock attempt). On the other hand, it is in theory possible to kill a program that is otherwise correct. However, in most cases, when you fail to obtain a lock, it indicates that you lock a location too long or you spin-lock a highly-contended location, both of which are considered wrong. In most cases, you should change the locking strategy for performance anyway. See Initialization Lock and Unlock for more about this.

  • Initialization Lock and Unlock
  • Reading and Checking Locations
  • Try Lock and Lock Any
  • Fetch and Add