How do you properly synchronize threads on the native side of a JNI environment?
If both threads are attached to the JVM, then you can access the JNI's synchronization via JNIEnv
's MonitorEnter(jobject)
and MonitorExit(jobject)
functions. Just as it sounds, MonitorEnter
aquires a lock on the provided jobject
, and MonitorExit
releases the lock on the provided jobject
.
NOTE: There are some pitfalls to be aware of! Notice the second to last paragraph of MonitorEnter
's description and the last paragraph of MonitorExit
's description about mixing and matching MonitorEnter
/MonitorExit
with other similar mechanisms which you might otherwise think are compatible.
See here
MonitorEnter
jint MonitorEnter(JNIEnv *env, jobject obj);
Enters the monitor associated with the underlying Java object referred to by obj. Enters the monitor associated with the object referred to by obj. The obj reference must not be NULL. Each Java object has a monitor associated with it. If the current thread already owns the monitor associated with obj, it increments a counter in the monitor indicating the number of times this thread has entered the monitor. If the monitor associated with obj is not owned by any thread, the current thread becomes the owner of the monitor, setting the entry count of this monitor to 1. If another thread already owns the monitor associated with obj, the current thread waits until the monitor is released, then tries again to gain ownership.
A monitor entered through a MonitorEnter JNI function call cannot be exited using the monitorexit Java virtual machine instruction or a synchronized method return. A MonitorEnter JNI function call and a monitorenter Java virtual machine instruction may race to enter the monitor associated with the same object.
To avoid deadlocks, a monitor entered through a MonitorEnter JNI function call must be exited using the MonitorExit JNI call, unless the DetachCurrentThread call is used to implicitly release JNI monitors.
LINKAGE:
Index 217 in the JNIEnv interface function table.
PARAMETERS:
env: the JNI interface pointer.
obj: a normal Java object or class object.
RETURNS:
Returns “0” on success; returns a negative value on failure.
and
MonitorExit
jint MonitorExit(JNIEnv *env, jobject obj);
The current thread must be the owner of the monitor associated with the underlying Java object referred to by obj. The thread decrements the counter indicating the number of times it has entered this monitor. If the value of the counter becomes zero, the current thread releases the monitor.
Native code must not use MonitorExit to exit a monitor entered through a synchronized method or a monitorenter Java virtual machine instruction.
LINKAGE:
Index 218 in the JNIEnv interface function table.
PARAMETERS:
env: the JNI interface pointer.
obj: a normal Java object or class object.
RETURNS:
Returns “0” on success; returns a negative value on failure.
EXCEPTIONS:
IllegalMonitorStateException: if the current thread does not own the monitor.
So the C++ code in the question which attempted to use pthreads should be changed as following (code assumes the JNIEnv*
pointer was acquired somehow beforehand in typical JNI fashion):
class objectA
{
jobject dataMutex;
... // everything else mentioned before
}
// called on c++ thread
void objectA :: poll()
{
// You will need to aquire jniEnv pointer somehow just as usual for JNI
jniEnv->MonitorEnter(dataMutex);
... // all the poll stuff from before
jniEnv->MonitorExit(dataMutex);
}
// called on java thread
void objectA :: supplyData(JNIEnv* jni, jobject jthis, jobject data)
{
// You will need to aquire jniEnv pointer somehow just as usual for JNI
jniEnv->MonitorEnter(dataMutex);
... // all the supplyData stuff from before
jniEnv->MonitorExit(dataMutex);
}
Kudos to @Radiodef who provided the answer. Unfortunately it was as a comment. I waited until afternoon next day to allow time for Radiodef to make it an answer, so now I'm doing it. Thank you Radiodef for providing the nudge I needed to fix this.