Skip to content

Commit

Permalink
Replace WindowsJNAAffinity.SetThreadAffinityMask with SetProcessAffin…
Browse files Browse the repository at this point in the history
…ityMask.
  • Loading branch information
yevgenp committed Apr 9, 2024
1 parent 3cebc6f commit 2229ce6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public void setAffinity(final BitSet affinity) {

int pid = getTid();
try {
lib.SetThreadAffinityMask(handle(pid), aff);
lib.SetProcessAffinityMask(Kernel32.INSTANCE.GetCurrentProcess(), aff);
} catch (LastErrorException e) {
throw new IllegalStateException("SetThreadAffinityMask((" + pid + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
throw new IllegalStateException("SetProcessAffinityMask((" + pid + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
}
BitSet affinity2 = getAffinity0();
if (!affinity2.equals(affinity)) {
Expand Down Expand Up @@ -158,7 +158,7 @@ private interface CLibrary extends Library {

int GetProcessAffinityMask(final WinNT.HANDLE pid, final PointerType lpProcessAffinityMask, final PointerType lpSystemAffinityMask) throws LastErrorException;

void SetThreadAffinityMask(final WinNT.HANDLE pid, final WinDef.DWORD lpProcessAffinityMask) throws LastErrorException;
int SetProcessAffinityMask(final WinNT.HANDLE pid, final WinDef.DWORD lpProcessAffinityMask) throws LastErrorException;

int GetCurrentThread() throws LastErrorException;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package net.openhft.affinity.impl;

import net.openhft.affinity.IAffinity;
import org.junit.BeforeClass;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.BitSet;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

public class WindowsJNAAffinityTest extends AbstractAffinityImplTest {
@BeforeClass
public static void checkJniLibraryPresent() {
assumeTrue(System.getProperty("os.name").contains("Win"));
assumeTrue(WindowsJNAAffinity.LOADED);
}

@Override
public IAffinity getImpl() {
return WindowsJNAAffinity.INSTANCE;
}


@Test
public void testSetAffinity() throws Exception {
BitSet affinity = new BitSet(CORES);
affinity.set(0);
getImpl().setAffinity(affinity);

Method getAffinity0 = WindowsJNAAffinity.class.getDeclaredMethod("getAffinity0");
getAffinity0.setAccessible(true);

assertEquals(affinity, getAffinity0.invoke(getImpl()));
}
}

0 comments on commit 2229ce6

Please sign in to comment.