Cool... thank you for your help.

--jason

On 11-Jan-99 Juergen Kreileder wrote:
>>>>>> Jason Dillon writes:
> 
>     Jason> Does this apply to native methods as well?  The docs do not
>     Jason> really mention anything about native methods (not in that
>     Jason> section anyways).  I am wondering if it is safe to assume
>     Jason> that marking a native method as synchronized will
>     Jason> automagicly protect the object from access by other threads
>     Jason> or if I have to explicitly protect the method in the native
>     Jason> code.
> 
> Yes, this applies to native methods as well and at least the JDK 
> implements it correctly.
> 
> Here's a little test program:
> 
> C.java:
> class C
> {
>     static {
>         System.loadLibrary("synctest");
>     }
> 
>     native synchronized void ok(); 
>     native void bad(); 
> 
>     public static void main(String[] args)
>     {
>         C c = new C();
>         c.ok();
>         System.out.println("--------------");
>         c.bad();
>     }
> }
> 
> synctest.c:
>#include <stdlib.h>
>#include "C.h"
> 
> JNIEXPORT void JNICALL 
> Java_C_ok(JNIEnv* env, jobject this)
> {
>     jclass cls = (*env)->FindClass(env, "C");
>     jmethodID mid = (*env)->GetMethodID(env, cls, "notify", "()V");
>     (*env)->CallObjectMethod(env, this, mid);
>     if ((*env)->ExceptionOccurred(env)) {
>         (*env)->ExceptionDescribe(env);
>     }
> }
> 
> JNIEXPORT void JNICALL 
> Java_C_bad  (JNIEnv* env, jobject this)
> {
>     jclass cls = (*env)->FindClass(env, "C");
>     jmethodID mid = (*env)->GetMethodID(env, cls, "notify", "()V");
>     (*env)->CallObjectMethod(env, this, mid);
>     if ((*env)->ExceptionOccurred(env)) {
>         (*env)->ExceptionDescribe(env);
>     }
> }
> 
> The call to c.ok() should be ok but c.bad() should fail, i.e. the correct
> output is:
> $ LD_LIBRARY_PATH=. java C
> --------------
> java.lang.IllegalMonitorStateException: current thread not owner
>         at C.main(C.java:16)
> 
> 
>         Juergen
> 
>     Jason> On 11-Jan-99 Juergen Kreileder wrote:
>     >>>>>>> Jason Dillon writes:
>     >> 
>     Jason> This is not really java-linux specific, but this is the
>     Jason> only java related group that I am subscribed to.  I am
>     Jason> wondering if a native method is specified as synchronized
>     Jason> if the jvm will perform the proper MontiorEnter &
>     Jason> MonitorExit calls or if I should call them in the native
>     Jason> method.
>     >> 
>     Jason> So for example... does:
>     >> 
>     Jason> native public synchronized byte[] getBytes ();
>     >> 
>     Jason> imply:
>     >> 
>     Jason> JNIEXPORT jbyteArray JNICALL Java_<Package>_<Class>_getBytes
>     Jason> (JNIEnv *env, jobject jthis)
>     Jason> {
>     Jason> (*env)->MonitorEnter(env, jthis);
>     Jason> /* do something */
>     Jason> (*env)->MonitorExit(env, jthis);
>     >> 
>     Jason> return /* some jbyteArray */;
>     Jason> }
>     >> 
>     >> Not exactly, the method indeed is synchronized but the synchronization
>     >> is part of the method invocation and return. Section 7.14 of the
>     >> virtual machine specification has more details about that:
>     >> http://java.sun.com/docs/books/vmspec/html/Compiling.doc.html#6530
>     >> 
>     >> 
>     >> Juergen
>     >> 
>     >> -- 
>     >> Juergen Kreileder, Universitaet Dortmund, Lehrstuhl Informatik V
>     >> Baroper Strasse 301, D-44221 Dortmund, Germany
>     >> Phone: ++49 231/755-5806, Fax: ++49 231/755-5802

Reply via email to