55import org .lwjgl .system .MemoryUtil ;
66import org .lwjgl .system .Platform ;
77import org .lwjgl .system .windows .Kernel32 ;
8+ import org .lwjgl .system .APIUtil ;
89
910//Platform specific code to assist in thread utilities
1011public 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}
0 commit comments