Skip to content

Commit cac12b8

Browse files
committed
https://github.com/peter-lawrey/Java-Thread-Affinity/issues/4
Improved errno handling
1 parent 145673f commit cac12b8

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

src/main/java/vanilla/java/affinity/JNAAffinity.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@
2626
public enum JNAAffinity implements AffinitySupport.IAffinity {
2727
INSTANCE;
2828
public static final Boolean LOADED;
29-
public static final String LIBRARY_NAME = Platform.isWindows() ? "msvcrt" : "c";
29+
private static final String LIBRARY_NAME = Platform.isWindows() ? "msvcrt" : "c";
3030

3131
/**
3232
* @author BegemoT
33+
* @author jbellis
3334
*/
3435
private interface CLibrary extends Library {
3536
public static final CLibrary INSTANCE = (CLibrary)
3637
Native.loadLibrary(LIBRARY_NAME, CLibrary.class);
3738

38-
public int sched_setaffinity(final int pid, final int cpusetsize, final PointerType cpuset);
39+
public int sched_setaffinity(final int pid, final int cpusetsize, final PointerType cpuset) throws LastErrorException;
3940

40-
public int sched_getaffinity(final int pid, final int cpusetsize, final PointerType cpuset);
41+
public int sched_getaffinity(final int pid, final int cpusetsize, final PointerType cpuset) throws LastErrorException;
4142

4243
// public int sched_getcpu();
4344
}
@@ -58,27 +59,15 @@ public long getAffinity() {
5859
final CLibrary lib = CLibrary.INSTANCE;
5960
final LongByReference cpuset = new LongByReference(0L);
6061
final int ret = lib.sched_getaffinity(0, Long.SIZE / 8, cpuset);
61-
if (ret < 0) {
62-
final int errNo = getErrorNo();
63-
throw new IllegalStateException("sched_getaffinity((" + Long.SIZE / 8 + ") , &(" + cpuset + ") ) return " + ret + ", errno() = " + errNo);
64-
}
62+
assert ret == 0;
6563
return cpuset.getValue();
6664
}
6765

6866
@Override
6967
public void setAffinity(long affinity) {
7068
final CLibrary lib = CLibrary.INSTANCE;
7169
final int ret = lib.sched_setaffinity(0, Long.SIZE / 8, new LongByReference(affinity));
72-
if (ret < 0) {
73-
final int errNo = getErrorNo();
74-
throw new IllegalStateException("sched_setaffinity((" + Long.SIZE / 8 + ") , &(" + affinity + ") ) return " + ret + ", errno() = " + errNo);
75-
}
76-
}
77-
78-
private static int getErrorNo() {
79-
final NativeLibrary nativeLib = NativeLibrary.getInstance(LIBRARY_NAME);
80-
final Pointer pErrNo = nativeLib.getFunction("errno");
81-
return pErrNo.getInt(0);
70+
assert ret == 0;
8271
}
8372

8473
@Override

src/test/java/vanilla/java/affinity/JNAAffinityTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616

1717
package vanilla.java.affinity;
1818

19+
import com.sun.jna.LastErrorException;
1920
import org.junit.Assume;
2021
import org.junit.BeforeClass;
2122
import org.junit.Test;
2223

24+
import static junit.framework.Assert.assertEquals;
25+
import static junit.framework.Assert.fail;
26+
2327
/**
2428
* @author peter.lawrey
2529
*/
@@ -29,6 +33,23 @@ public static void checkJniLibraryPresent() {
2933
Assume.assumeTrue(JNAAffinity.LOADED);
3034
}
3135

36+
@Test
37+
public void getSetAffinity() {
38+
long init = JNAAffinity.INSTANCE.getAffinity();
39+
JNAAffinity.INSTANCE.setAffinity(0x01);
40+
assertEquals(0x01, JNAAffinity.INSTANCE.getAffinity());
41+
42+
try {
43+
JNAAffinity.INSTANCE.setAffinity(0x00);
44+
fail("This should fail");
45+
} catch (LastErrorException expected) {
46+
assertEquals("errno was " + expected.getErrorCode(), expected.getMessage());
47+
}
48+
49+
JNAAffinity.INSTANCE.setAffinity(init);
50+
assertEquals(init, JNAAffinity.INSTANCE.getAffinity());
51+
}
52+
3253
@Test
3354
public void testNanoTimePerf() {
3455
final int runs = 10 * 1000 * 1000;

0 commit comments

Comments
 (0)