[Mono-list] How to implement the monitor synchronization?

Rhys Weatherley rweather@zip.com.au
Wed, 14 Nov 2001 07:45:42 +1000


Morten Sylvest Olsen wrote:

> From what I could gather from the mcs classes the monitor Enter/Exit are
> to be implemented with a native call.

That's correct.  Although a clever engine could inline
the calls to those methods within the JIT, and essentially
"bring up" the implementation into the caller.  The final
effect would be the same as the monitorenter/exit opcodes.

> I guess the runtime shall automagically prevent scheduling during native
> calls?!?

Not that I'm aware of.  Native methods have the same
synchronisation issues as IL methods.

Behind the scenes, the Enter/Exit methods will eventually
call down to a primitive "acquire monitor" or "release
monitor" service API.  This service API performs all
necessary locking to preserve the atomic nature of
monitor operations.

The JVM calls this API directly in its monitorenter/exit
opcodes.  CLR is one step removed, in that it has an
extra method call layer.  Inlining can remove this layer,
so that a JIT for CLR would produce similar code to
a JIT for the JVM.

I hope this helps.

Cheers,

Rhys.