When one thread hold a bassed-locking. The mark word is thread id. But for lightweight-locking, the mark word is stack pointer to the thread, why not store thread id still?
When one thread hold a bassed-locking. The mark word is thread id. But for lightweight-locking, the mark word is stack pointer to the thread, why not store thread id still?
Thin lock schema assumes that the mark word of a locked object points to a stack slot of the frame that has locked this object. This stack slot stores the original object header (aka displaced header).
Unlocked:
[ orig_header | 001 ] | Stack frame |
| |
Locked: | |
[ stack_ptr | 000 ] | |
| |-------------|
--------------------->| orig_header |
|-------------|
| |
| |
-------------
Obviously the stack slot carries more information than a thread ID, since you can derive thread ID from a stack slot, but not vice versa.
Unlike biased schema where unlock operation is effectively a no-op, thin locks need to restore the original header when an object is unlocked. This becomes very simple since the mark word already points to a stack slot with the original value.