peter-lawrey / Java-Thread-Affinity

Control thread affinity for Java
379 stars 77 forks source link

Fix JNA errno support #4

Closed jbellis closed 12 years ago

jbellis commented 12 years ago

The modern way to handle errno from JNA is to declare your native methods as "throws LastErrorException." Then you can access getErrorCode on a caught LEE. For example, https://github.com/ryanking/apache-cassandra/blob/trunk/src/java/org/apache/cassandra/utils/CLibrary.java (which has some instanceof dancing that you may not care about if you only support versions of JNA new enough to define LEE).

Calling errno directly is problematic (it's often a macro), and even Native.getLastError (JNA's old way to expose it semi-portably) doesn't always work (apparently it can itself trigger syscalls that reset errno...). LastErrorException does work reliably and is the recommended way to handle errno now.

peter-lawrey commented 12 years ago

I have added the use of LastErrorException with this test case which passes

@Test
public void getSetAffinity() {
    long init = JNAAffinity.INSTANCE.getAffinity();
    JNAAffinity.INSTANCE.setAffinity(0x01);
    assertEquals(0x01, JNAAffinity.INSTANCE.getAffinity()) ;

    try {
        JNAAffinity.INSTANCE.setAffinity(0x00);
        fail("This should fail");
    } catch (LastErrorException expected) {
        assertEquals("errno was "+expected.getErrorCode(), expected.getMessage());
    }

    JNAAffinity.INSTANCE.setAffinity(init);
    assertEquals(init, JNAAffinity.INSTANCE.getAffinity()) ;
}

Thank you for your advice.