Skip to content

Commit efda204

Browse files
committed
Merge remote-tracking branch 'origin/mc_1215' into mc_1215
2 parents 0c5882a + 56b75e3 commit efda204

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/main/java/me/cortex/voxy/common/util/ThreadUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.lwjgl.system.MemoryUtil;
66
import org.lwjgl.system.Platform;
77
import org.lwjgl.system.windows.Kernel32;
8+
import org.lwjgl.system.APIUtil;
89

910
//Platform specific code to assist in thread utilities
1011
public class ThreadUtils {
@@ -15,6 +16,7 @@ public class ThreadUtils {
1516
private static final boolean isWindows = Platform.get() == Platform.WINDOWS;
1617
private static final long SetThreadPriority;
1718
private static final long SetThreadSelectedCpuSetMasks;
19+
private static final long schedSetaffinity;
1820
static {
1921
if (isWindows) {
2022
SetThreadPriority = Kernel32.getLibrary().getFunctionAddress("SetThreadPriority");
@@ -23,6 +25,13 @@ public class ThreadUtils {
2325
SetThreadPriority = 0;
2426
SetThreadSelectedCpuSetMasks = 0;
2527
}
28+
29+
if (Platform.get() == Platform.LINUX) {
30+
var libc = APIUtil.apiCreateLibrary("libc.so.6");
31+
schedSetaffinity = APIUtil.apiGetFunctionAddress(libc, "sched_setaffinity");
32+
} else {
33+
schedSetaffinity = 0;
34+
}
2635
}
2736

2837
public static boolean SetThreadSelectedCpuSetMasksWin32(long mask) {
@@ -70,4 +79,22 @@ public static boolean SetSelfThreadPriorityWin32(int priority) {
7079
}
7180
return true;
7281
}
82+
83+
public static boolean schedSetaffinityLinux(long masks[]) {
84+
if (schedSetaffinity == 0 || isWindows) {
85+
return false;
86+
}
87+
try (var stack = MemoryStack.stackPush()) {
88+
long ptr = stack.ncalloc(8, masks.length, 8);
89+
for (int i=0; i<masks.length; i++) {
90+
MemoryUtil.memPutLong(ptr+i*8L, masks[i]);
91+
}
92+
93+
int retVal = JNI.invokePPI(0, (long)masks.length*8, ptr, schedSetaffinity);
94+
if (retVal != 0) {
95+
throw new IllegalStateException();
96+
}
97+
return true;
98+
}
99+
}
73100
}

src/main/java/me/cortex/voxy/common/util/cpu/CpuLayout.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public static void setThreadAffinity(Core... cores) {
2525
}
2626

2727
public static void setThreadAffinity(Affinity... affinities) {
28-
if (Platform.get() == Platform.WINDOWS) {
28+
var platform = Platform.get();
29+
if (platform == Platform.WINDOWS) {
2930
long[] msks = new long[affinities.length];
3031
short[] groups = new short[affinities.length];Arrays.fill(groups, (short) -1);
3132
int i = 0;
@@ -36,8 +37,15 @@ public static void setThreadAffinity(Affinity... affinities) {
3637
msks[idx] |= a.msk;
3738
}
3839
ThreadUtils.SetThreadSelectedCpuSetMasksWin32(Arrays.copyOf(msks, i), Arrays.copyOf(groups, i));
40+
} else if (platform == Platform.LINUX) {
41+
Arrays.sort(affinities, (a, b) -> a.group - b.group);
42+
long[] msks = new long[affinities.length];
43+
for (int i=0; i<affinities.length; i++) {
44+
msks[i] = affinities[i].msk;
45+
}
46+
ThreadUtils.schedSetaffinityLinux(msks);
3947
} else {
40-
Logger.error("TODO: THIS");
48+
Logger.error("Don't know how to set thread affinity on this platform.");
4149
}
4250
}
4351

0 commit comments

Comments
 (0)