Skip to content

Commit 7cb6427

Browse files
committed
Allow an AffinityLock to be release without resetting the current thread. #65
1 parent 469f388 commit 7cb6427

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class AffinityLock implements Closeable {
8585
@Nullable
8686
Thread assignedThread;
8787
Throwable boundHere;
88+
private boolean resetAffinity = true;
8889

8990
AffinityLock(int cpuId, boolean base, boolean reservable, LockInventory lockInventory) {
9091
this.lockInventory = lockInventory;
@@ -274,6 +275,22 @@ private static boolean areAssertionsEnabled() {
274275
return debug;
275276
}
276277

278+
/**
279+
* @return Whether to reset the affinity, false indicates the thread is about to die anyway.
280+
*/
281+
public boolean resetAffinity() {
282+
return resetAffinity;
283+
}
284+
285+
/**
286+
* @param resetAffinity Whether to reset the affinity, false indicates the thread is about to die anyway.
287+
* @return this
288+
*/
289+
public AffinityLock resetAffinity(boolean resetAffinity) {
290+
this.resetAffinity = resetAffinity;
291+
return this;
292+
}
293+
277294
/**
278295
* Assigning the current thread has a side effect of preventing the lock being used again until
279296
* it is released.
@@ -358,7 +375,9 @@ public void release() {
358375
if (cpuId == ANY_CPU)
359376
return;
360377
// expensive if not actually used.
361-
lockInventory.release();
378+
boolean resetAffinity = this.resetAffinity;
379+
this.resetAffinity = true;
380+
lockInventory.release(resetAffinity);
362381
}
363382

364383
@Override

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public final synchronized void bindWholeCore(int logicalCoreID) {
179179
}
180180
}
181181

182-
public final synchronized void release() {
182+
public final synchronized void release(boolean resetAffinity) {
183183
Thread t = Thread.currentThread();
184184
for (AffinityLock al : logicalCoreLocks) {
185185
Thread at = al.assignedThread;
@@ -189,7 +189,8 @@ public final synchronized void release() {
189189
releaseAffinityLock(t, al, "Releasing cpu {} from {} as it is not alive.");
190190
}
191191
}
192-
Affinity.resetToBaseAffinity();
192+
if (resetAffinity)
193+
Affinity.resetToBaseAffinity();
193194
}
194195

195196
public final synchronized String dumpLocks() {

affinity/src/test/java/net/openhft/affinity/AffinityLockTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ public void assignReleaseThread() throws IOException {
149149
assertEquals(AffinityLock.BASE_AFFINITY, Affinity.getAffinity());
150150
}
151151

152+
@Test
153+
public void resetAffinity() {
154+
assertTrue(Affinity.getAffinity().cardinality() > 1);
155+
try (AffinityLock lock = AffinityLock.acquireLock()) {
156+
assertEquals(1, Affinity.getAffinity().cardinality());
157+
assertTrue(lock.resetAffinity());
158+
lock.resetAffinity(false);
159+
}
160+
assertEquals(1, Affinity.getAffinity().cardinality());
161+
try (AffinityLock lock = AffinityLock.acquireLock()) {
162+
}
163+
assertTrue(Affinity.getAffinity().cardinality() > 1);
164+
}
165+
152166
@Test
153167
public void testIssue21() throws IOException {
154168
if (!new File("/proc/cpuinfo").exists()) {

0 commit comments

Comments
 (0)