0

Each Java object (and its class) has an associated monitor. In pthread terms a Java monitor is equivalent to the combination of a reentrant mutex and a condition variable.

For locking, the Win32 API provides Mutex objects (which are reentrant but heavyweight) and Critical Sections (which are non-reentrant but lightweight). It also provides other synchronization constructs such as Semaphores and Events but has no explicit concept of a condition variable.

If I were writing a JVM, how could I use these Win32 concepts to implement Java monitors?

2 Answers 2

2

Windows has SignalObjectAndWait() which can be used very much like a wait on a condition variable in a monitor. You can use an Event (that is Reset) and a Mutex and then use PulseEvent() to do the equivalent of signalling the condition variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Both SignalObjectAndWait and PulseEvent are unreliable. Under certain conditions they don't work. SignalObjectAndWait is not guaranteed to be atomic. PulseEvent doesn't work if kernel APC calls occur in the meanwhile. Combining both SignalObjectAndWait and PulseEvent can be disastrous.
0

I suggest you take a look at the OpenJDK source to see how the class ReentrantLock was implemented.

(I haven't checked it myself so i'm not sure of the answer).

the util.concurrent locks are implemented using native API.

2 Comments

On windows it is implemented using native API and not Java monitors.
I'm struggling to find the relevant source code. Could you point me in the right direction?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.