Skip to content

Commit 4439b11

Browse files
authored
Merge pull request #94 from OpenHFT/issue/93-warn-dont-throw
log warning instead of throw exception for too high cpuId. Closes #93
2 parents 1e4bbc6 + 1933c59 commit 4439b11

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public final synchronized AffinityLock acquireLock(boolean bind, int cpuId, Affi
126126
final boolean specificCpuRequested = !isAnyCpu(cpuId);
127127
try {
128128
if (specificCpuRequested && cpuId != 0) {
129+
if (cpuId > logicalCoreLocks.length) {
130+
LOGGER.warn("Unable to acquire lock on CPU {} for thread {}, as not enough CPUs",
131+
cpuId, Thread.currentThread());
132+
return noLock();
133+
}
134+
129135
final AffinityLock required = logicalCoreLocks[cpuId];
130136
if (required.canReserve(true)
131137
&& anyStrategyMatches(cpuId, cpuId, strategies)
@@ -162,7 +168,8 @@ && updateLockForCurrentThread(bind, al, false)) {
162168
public final synchronized AffinityLock tryAcquireLock(boolean bind, int cpuId) {
163169
if (getAffinityImpl() instanceof NullAffinity)
164170
return null;
165-
171+
if (cpuId > logicalCoreLocks.length)
172+
return null;
166173
final AffinityLock required = logicalCoreLocks[cpuId];
167174
try {
168175
if (required.canReserve(true)

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,16 @@ public void testAffinityLockDescriptions() {
296296
assertFalse(lock.bound);
297297
}
298298
}
299+
300+
@Test
301+
public void testTooHighCpuId() {
302+
try (AffinityLock ignored = AffinityLock.acquireLock(123456)) {
303+
}
304+
}
305+
306+
@Test
307+
public void testTooHighCpuId2() {
308+
try (AffinityLock ignored = AffinityLock.acquireLock(new int[] {123456})) {
309+
}
310+
}
299311
}

0 commit comments

Comments
 (0)