Skip to content

Commit f824225

Browse files
committed
When SetThreadAffinityMask silently fails, give a warning and retain the affinity mask. #76
1 parent b40ac66 commit f824225

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed

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

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.sun.jna.platform.win32.WinDef;
2626
import com.sun.jna.ptr.LongByReference;
2727
import net.openhft.affinity.IAffinity;
28+
import org.jetbrains.annotations.Nullable;
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031

@@ -42,6 +43,7 @@ public enum WindowsJNAAffinity implements IAffinity {
4243
INSTANCE;
4344
public static final boolean LOADED;
4445
private static final Logger LOGGER = LoggerFactory.getLogger(WindowsJNAAffinity.class);
46+
private static final ThreadLocal<BitSet> currentAffinity = new ThreadLocal<>();
4547

4648
static {
4749
boolean loaded = false;
@@ -58,26 +60,11 @@ public enum WindowsJNAAffinity implements IAffinity {
5860

5961
@Override
6062
public BitSet getAffinity() {
61-
final CLibrary lib = CLibrary.INSTANCE;
62-
final LongByReference cpuset1 = new LongByReference(0);
63-
final LongByReference cpuset2 = new LongByReference(0);
64-
try {
65-
66-
final int ret = lib.GetProcessAffinityMask(-1, cpuset1, cpuset2);
67-
// Successful result is positive, according to the docs
68-
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683213%28v=vs.85%29.aspx
69-
if (ret <= 0) {
70-
throw new IllegalStateException("GetProcessAffinityMask(( -1 ), &(" + cpuset1 + "), &(" + cpuset2 + ") ) return " + ret);
71-
}
72-
73-
long[] longs = new long[1];
74-
longs[0] = cpuset1.getValue();
75-
return BitSet.valueOf(longs);
76-
} catch (Exception e) {
77-
LOGGER.error(e.getMessage(), e);
78-
}
79-
80-
return new BitSet();
63+
BitSet bitSet = currentAffinity.get();
64+
if (bitSet != null)
65+
return bitSet;
66+
BitSet longs = getAffinity0();
67+
return longs != null ? longs : new BitSet();
8168
}
8269

8370
@Override
@@ -103,6 +90,34 @@ public void setAffinity(final BitSet affinity) {
10390
} catch (LastErrorException e) {
10491
throw new IllegalStateException("SetThreadAffinityMask((" + pid + ") , &(" + affinity + ") ) errorNo=" + e.getErrorCode(), e);
10592
}
93+
BitSet affinity2 = getAffinity0();
94+
if (!affinity2.equals(affinity)) {
95+
LoggerFactory.getLogger(WindowsJNAAffinity.class).warn("Tried to set affinity to " + affinity + " but was " + affinity2 + " you may have in sufficient access rights");
96+
}
97+
currentAffinity.set((BitSet) affinity.clone());
98+
}
99+
100+
@Nullable
101+
private BitSet getAffinity0() {
102+
final CLibrary lib = CLibrary.INSTANCE;
103+
final LongByReference cpuset1 = new LongByReference(0);
104+
final LongByReference cpuset2 = new LongByReference(0);
105+
try {
106+
107+
final int ret = lib.GetProcessAffinityMask(-1, cpuset1, cpuset2);
108+
// Successful result is positive, according to the docs
109+
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms683213%28v=vs.85%29.aspx
110+
if (ret <= 0) {
111+
throw new IllegalStateException("GetProcessAffinityMask(( -1 ), &(" + cpuset1 + "), &(" + cpuset2 + ") ) return " + ret);
112+
}
113+
114+
long[] longs = new long[1];
115+
longs[0] = cpuset1.getValue();
116+
return BitSet.valueOf(longs);
117+
} catch (Exception e) {
118+
LOGGER.error(e.getMessage(), e);
119+
}
120+
return null;
106121
}
107122

108123
public int getTid() {

0 commit comments

Comments
 (0)