Skip to content

Commit 525cf4e

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 4758ecf + f824225 commit 525cf4e

File tree

6 files changed

+44
-28
lines changed

6 files changed

+44
-28
lines changed

README.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,21 @@ try (final AffinityLock al = AffinityLock.acquireLock()) {
129129
----
130130
In this example, the library will prefer a free CPU on the same Socket as the first thread, otherwise it will pick any free CPU.
131131

132-
=== Getting the thread id.
132+
=== Getting the thread id
133133
You can get the current thread id using
134134
[source, java]
135135
----
136136
137137
int threadId = AffinitySupport.getThreadId();
138138
----
139-
=== Determining which CPU you are running on.
139+
=== Determining which CPU you are running on
140140
You can get the current CPU being used by
141141
[source, java]
142142
----
143143
144144
int cpuId = AffinitySupport.getCpu();
145145
----
146-
=== Controlling the affinity more directly.
146+
=== Controlling the affinity more directly
147147
The affinity of the process on start up is
148148
[source, java]
149149
----
@@ -223,7 +223,7 @@ try (AffinityLock lock = AffinityLock.acquireLockLastMinus(n)) {
223223

224224
I have the cpuId in a configuration file, how can I set it using a string?
225225

226-
=== Answer: use one of the following.
226+
=== Answer: use one of the following
227227

228228
[source,java]
229229
----

affinity-test/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</parent>
2727

2828
<artifactId>affinity-test</artifactId>
29-
<version>3.21ea3-SNAPSHOT</version>
29+
<version>3.21ea4-SNAPSHOT</version>
3030
<packaging>bundle</packaging>
3131

3232
<name>OpenHFT/Java-Thread-Affinity/affinity-test</name>

affinity/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</parent>
2727

2828
<artifactId>affinity</artifactId>
29-
<version>3.21ea3-SNAPSHOT</version>
29+
<version>3.21ea4-SNAPSHOT</version>
3030
<packaging>bundle</packaging>
3131

3232
<name>OpenHFT/Java-Thread-Affinity/affinity</name>

affinity/src/main/java/net/openhft/affinity/AffinityThreadFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public void run() {
6666
}
6767

6868
private synchronized AffinityLock acquireLockBasedOnLast() {
69-
AffinityLock al = lastAffinityLock == null ? AffinityLock.acquireLock() : lastAffinityLock.acquireLock(strategies);
69+
AffinityLock al = lastAffinityLock == null ? AffinityLock.acquireLock(false) : lastAffinityLock.acquireLock(strategies);
70+
al.bind();
7071
if (al.cpuId() >= 0)
7172
lastAffinityLock = al;
7273
return al;

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() {

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
</parent>
2727

2828
<artifactId>Java-Thread-Affinity</artifactId>
29-
<version>3.21ea3-SNAPSHOT</version>
29+
<version>3.21ea4-SNAPSHOT</version>
3030
<packaging>pom</packaging>
3131

3232
<name>OpenHFT/Java-Thread-Affinity Parent</name>

0 commit comments

Comments
 (0)