Re: compiling locks/ monitors

Ian Rogers <>
Fri, 13 Nov 2009 09:11:14 -0800 (PST)

          From comp.compilers

Related articles
compiling locks/ monitors (Arnab De) (2009-11-11)
Re: compiling locks/ monitors (Felipe Angriman) (2009-11-13)
Re: compiling locks/ monitors (Ian Rogers) (2009-11-13)
Re: compiling locks/ monitors (Andrew Tomazos) (2009-11-21)
| List of all articles for this month |

From: Ian Rogers <>
Newsgroups: comp.compilers
Date: Fri, 13 Nov 2009 09:11:14 -0800 (PST)
Organization: Compilers Central
References: 09-11-037
Keywords: parallel
Posted-Date: 15 Nov 2009 17:45:28 EST

On Nov 11, 6:08 am, Arnab De <> wrote:
> Many modern high level languages like Java/ C# have locks/ monitors as
> language level features. Hardwares typically have lower level
> synchronization primitives like CAS. One can implement a spinlock
> using CAS, but it is bad for performance as it busy-waits for the lock
> to be released. Can anyone tell me how these locks/monitors are
> typically and efficiently compiled? Can you cite any paper on this?

Hi Arnab,

for Java the papers you should look at are on thin locking [1] and
then on lock reservation/biasing [2][3]. A thin lock is a word in an
object that you assume will show that an object is not locked and then
you try to swap in your thread ID, if your compare and swap found a
non-zero value you have contention and then you need a fat lock (a
separate data structure). On releasing the thin lock you do a compare
and swap and if your thread ID has been changed to a fat lock
reference then you need to go to the fat lock to wake up a contending
thread. Lock reservation changes this so that on the release you do no
work (no CAS), instead contending threads add themselves to a list of
contending threads paired with objects that when crossing a safepoint
are processed. With reservation an object is often owned by your
thread (biased to it or reserved by it) and to check the thread still
owns the lock you don't need a full CAS, just a load (hence locking
with no atomic operations). In Java locks can be held recursively,
adding complexity. You also may not want lock reservation/biasing in
some performance scenarios, and you may want to busy wait in some
scenarios too.

Ian Rogers

[2] - Lock reservation: Java
locks can mostly do without atomic operations

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.