Atomic Operations and Syncronization

This implementation is based on "Thin Locks: Featherweight Syncronization for Java" developed at IBM T.J. Watson Research Centre.

Object Layout

All objects contain the following header layout:

  --- Locking Structure (24 bits)
  --- Metadata Token (16 bit integer)

Thin Locks

  --- 0 (1 bit)
  --- Thread Identifier (15 bits) - Index in table which maps Thread Ids to Thread Pointers
  --- Count (8 bits) - Number of locks minus 1

Locking

  1. Lock Interrupts
  2. Old Value = 0x000
  3. Count = 0
  4. New Value = Old Value OR (Thread Identifier << 16)
  5. Compare and swap (Old Value, New Value) ==> Lock Word
  6. If equal jump to end
  7. Owned = (Thread Identifier << 16)
  8. Owned > 255 need fat lock
  9. (|Lock Word| XOR |Thread Index|) << 8
  10. Less Than 255
  11. Add 255 to |New Value|
  12. Store in |Lock Word|
  13. Unlock Interrupts

Fat Locks

  --- 1 (1 bit)
  --- Lock Index (23 bits) - Index in table which maps Fat Locks to Fat Lock Structures.

Also available in: HTML TXT