Skip to content

Commit b9e4a40

Browse files
authored
Allow thread pinning by pid
It is not always possible to access the thread to be pinned. This method allows any thread to be pinned. Tested on RHEL 7.
1 parent e0987f6 commit b9e4a40

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

affinity/src/main/java/net/openhft/affinity/impl/LinuxHelper.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ cpu_set_t sched_getaffinity() {
6666
return cpuset;
6767
}
6868

69-
public static void sched_setaffinity(final BitSet affinity) {
69+
public static void sched_setaffinity(final BitSet affinity) {
70+
sched_setaffinity(0, affinity);
71+
}
72+
73+
public static void sched_setaffinity(final int pid, final BitSet affinity) {
7074
final CLibrary lib = CLibrary.INSTANCE;
7175
final cpu_set_t cpuset = new cpu_set_t();
7276
final int size = version.isSameOrNewer(VERSION_2_6) ? cpu_set_t.SIZE_OF_CPU_SET_T : NativeLong.SIZE;
@@ -80,12 +84,12 @@ public static void sched_setaffinity(final BitSet affinity) {
8084
}
8185
}
8286
try {
83-
if (lib.sched_setaffinity(0, size, cpuset) != 0) {
84-
throw new IllegalStateException("sched_setaffinity(0, " + size +
87+
if (lib.sched_setaffinity(pid, size, cpuset) != 0) {
88+
throw new IllegalStateException("sched_setaffinity(" + pid + ", " + size +
8589
", 0x" + Utilities.toHexString(affinity) + ") failed; errno=" + Native.getLastError());
8690
}
8791
} catch (LastErrorException e) {
88-
throw new IllegalStateException("sched_setaffinity(0, " + size +
92+
throw new IllegalStateException("sched_setaffinity(" + pid + ", " + size +
8993
", 0x" + Utilities.toHexString(affinity) + ") failed; errno=" + e.getErrorCode(), e);
9094
}
9195
}

0 commit comments

Comments
 (0)