Adding enterMonitor/exitMonitor to JObject

Alec Bau
2003-05-29
2013-04-29
  • Alec Bau

    Alec Bau - 2003-05-29

    Toby,

    I found that adding those methods to JObject is pretty handy and allows to use portable Java threading primitives likw wait, notify, etc. in C++.

    Will you consider adding this to the Jace?

    Here are sample implementations:

    /**
    *    Acquires a monitor on this JObject.
    *
    *    Equivalent to Java "synchronized(this)" block entrance.
    *
    *    Throws if operation didn't succeed.
    */
    void JObject::monitorEnter( void ) throw ( JNIException )
    {
        jobject jthis = getJavaObject();
        if (jthis == NULL)
        {
            throw JNIException ("JObject::monitorEnter - Can't enter monitor on isNull object!");
        }

        JNIEnv* jenv = helper::attach ();
        if (jenv -> MonitorEnter (jthis) != JNI_OK)
        {
            throw JNIException ("JObject::monitorEnter - Failed to enter the monitor!");
        }
    }

    /**
    *    Releases a monitor on this JObject.
    *
    *    Equivalent to Java "synchronized(this)" block exit.
    *
    *    Throws if operation didn't succeed.
    */
    void JObject::monitorExit( void ) throw ( JNIException )
    {
        jobject jthis = getJavaObject();
        if (jthis == NULL)
        {
            throw JNIException ("JObject::monitorExit - Can't exit monitor on isNull object!");
        }

        JNIEnv* jenv = helper::attach ();
        if (jenv -> MonitorExit (jthis) != JNI_OK)
        {
            throw JNIException ("JObject::monitorExit - Failed to exit the monitor!");
        }
    }

     
    • Gili Tzabari

      Gili Tzabari - 2008-08-18

      I'm curious, what prevents you from locking in Java before you enter the native code?

       

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks