wei-spring / codenameone

Automatically exported from code.google.com/p/codenameone
0 stars 0 forks source link

synchronized static methods not working correctly with new iOS vm #1274

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The problem is with the new iOS vm.

What steps will reproduce the problem?
1. Create a simple project like this:

a) add a test class:

public class SomeTestLogic {

    public static synchronized void syncedMethod() {
        System.out.println("executed!");
    }

}

b) to the StateMachine add the following code:

protected void initVars(Resources res) {
        SomeTestLogic.syncedMethod();

        new Thread() {

            public void run() {
                System.out.println("callSync from thread:");
                SomeTestLogic.syncedMethod();
            }

        }.start();
}

2. run 

What is the expected output? What do you see instead?

To the console should be printed:

executed!
callSync from thread:
executed!

But on new iOS is printed (2nd call to syncedMethod() is not executed):

executed!
callSync from thread:

What version of the product are you using? On what operating system?

the latest buildserver build

Please provide any additional information below.

From generated code it seams, that the problem raises from calling monitorEnter 
twice, but a simple test to remove the second call failed, so unfortunately I 
don't have a quick fix.

JAVA_VOID userclasses_SomeTestLogic_syncedMethod__(CODENAME_ONE_THREAD_STATE) {
    __STATIC_INITIALIZER_userclasses_SomeTestLogic(threadStateData);
    DEFINE_METHOD_STACK(2, 0, 0, 7423, 7424);
    monitorEnter(threadStateData, (JAVA_OBJECT)&class__userclasses_SomeTestLogic);
    __CN1_DEBUG_INFO(11);
    PUSH_POINTER(get_static_java_lang_System_out(threadStateData));
    /* LDC: 'synced print'*/
    PUSH_POINTER(STRING_FROM_CONSTANT_POOL_OFFSET(3338));
    virtual_java_io_PrintStream_println___java_lang_String(threadStateData, stack[stackPointer - 2].data.o, stack[stackPointer - 1].data.o);     releaseObj(threadStateData, stack[stackPointer - 1].data.o);
    releaseObj(threadStateData, stack[stackPointer - 2].data.o);
    stackPointer -= 2;
    __CN1_DEBUG_INFO(12);
    monitorEnter(threadStateData, (JAVA_OBJECT)&class__userclasses_SomeTestLogic);
    releaseForReturn(threadStateData, cn1LocalsBeginInThread, stackPointer - 1, 0, stack, locals); 
    return;
}

Original issue reported on code.google.com by jaanus.h...@gmail.com on 9 Jan 2015 at 2:34

GoogleCodeExporter commented 9 years ago
sorry for missing the obvious, the issue seams to be easily fixable be 
replacing the second "monitorEnter" with "monitorExit".

Original comment by jaanus.h...@gmail.com on 12 Jan 2015 at 7:06

GoogleCodeExporter commented 9 years ago
Thanks, should be fixed now.

Original comment by shai.almog on 12 Jan 2015 at 11:42