Skip to content

Commit b91101c

Browse files
committed
Remove lock files after release. Fixes OpenHFT#42.
1 parent aa5f11e commit b91101c

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import org.slf4j.Logger;
2222
import org.slf4j.LoggerFactory;
2323

24+
import java.io.IOException;
25+
import java.nio.file.Files;
26+
import java.nio.file.Paths;
2427
import java.util.NavigableMap;
2528
import java.util.TreeMap;
2629

@@ -154,16 +157,9 @@ public final synchronized void release() {
154157
for (AffinityLock al : logicalCoreLocks) {
155158
Thread at = al.assignedThread;
156159
if (at == t) {
157-
LOGGER.info("Releasing cpu {} from {}", al.cpuId(), t);
158-
al.assignedThread = null;
159-
al.bound = false;
160-
al.boundHere = null;
161-
160+
releaseAffinityLock(t, al, "Releasing cpu {} from {}");
162161
} else if (at != null && !at.isAlive()) {
163-
LOGGER.warn("Releasing cpu {} from {} as it is not alive.", al.cpuId(), t);
164-
al.assignedThread = null;
165-
al.bound = false;
166-
al.boundHere = null;
162+
releaseAffinityLock(t, al, "Releasing cpu {} from {} as it is not alive.");
167163
}
168164
}
169165
Affinity.resetToBaseAffinity();
@@ -186,4 +182,18 @@ private void reset(CpuLayout cpuLayout) {
186182
private int toPhysicalCore(int layoutId) {
187183
return cpuLayout.socketId(layoutId) * cpuLayout.coresPerSocket() + cpuLayout.coreId(layoutId);
188184
}
185+
186+
private void releaseAffinityLock(final Thread t, final AffinityLock al, final String format) {
187+
LOGGER.info(format, al.cpuId(), t);
188+
al.assignedThread = null;
189+
al.bound = false;
190+
al.boundHere = null;
191+
192+
final String lockFilePath = LockCheck.toFile(al.cpuId()).getAbsolutePath();
193+
try {
194+
Files.delete(Paths.get(lockFilePath));
195+
} catch (IOException e) {
196+
LOGGER.warn("Failed to delete lock file at " + lockFilePath);
197+
}
198+
}
189199
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424

2525
import java.io.File;
2626
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Paths;
2729
import java.util.ArrayList;
2830
import java.util.List;
2931

32+
import static org.hamcrest.CoreMatchers.is;
3033
import static org.junit.Assert.assertEquals;
3134
import static org.junit.Assert.assertNotSame;
35+
import static org.junit.Assert.assertThat;
3236

3337
/**
3438
* @author peter.lawrey
@@ -218,6 +222,17 @@ public void run() {
218222
displayStatus();
219223
}
220224

225+
@Test
226+
public void lockFilesShouldBeRemovedOnRelease() {
227+
final AffinityLock lock = AffinityLock.acquireLock();
228+
229+
assertThat(Files.exists(Paths.get(LockCheck.toFile(lock.cpuId()).getAbsolutePath())), is(true));
230+
231+
lock.release();
232+
233+
assertThat(Files.exists(Paths.get(LockCheck.toFile(lock.cpuId()).getAbsolutePath())), is(false));
234+
}
235+
221236
private void displayStatus() {
222237
System.out.println(Thread.currentThread() + " on " + Affinity.getCpu() + "\n" + AffinityLock.dumpLocks());
223238
}

0 commit comments

Comments
 (0)