Skip to content

Commit 2614c99

Browse files
author
Quan Anh Mai
committed
8343793: Test java/foreign/TestMemorySession.java is timing out
Reviewed-by: mcimadamore
1 parent 8a69893 commit 2614c99

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

test/jdk/java/foreign/TestMemorySession.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -326,52 +326,56 @@ public void testAcquireCloseRace() throws InterruptedException {
326326
int iteration = 1000;
327327
AtomicInteger lock = new AtomicInteger();
328328
boolean[] result = new boolean[1];
329-
lock.set(-2);
330329
MemorySessionImpl[] scopes = new MemorySessionImpl[iteration];
331330
for (int i = 0; i < iteration; i++) {
332331
scopes[i] = MemorySessionImpl.toMemorySession(Arena.ofShared());
333332
}
334333

334+
// These two threads proceed the scopes array in a lock-step manner, the first thread wait
335+
// for the second thread on the lock variable, while the second thread wait for the first
336+
// thread on the closing of the current scope
337+
335338
// This thread tries to close the scopes
336339
Thread t1 = new Thread(() -> {
337-
for (int i = 0; i < iteration; i++) {
340+
for (int i = 0; i < iteration;) {
338341
MemorySessionImpl scope = scopes[i];
339342
while (true) {
340343
try {
341344
scope.close();
345+
// Continue to the next iteration after a successful close
342346
break;
343-
} catch (IllegalStateException e) {}
347+
} catch (IllegalStateException e) {
348+
// Wait for the release and try again
349+
}
344350
}
345-
// Keep the 2 threads operating on the same scope
346-
int k = lock.getAndAdd(1) + 1;
347-
while (k != i * 2) {
351+
// Wait for the other thread to complete its iteration
352+
int prev = i;
353+
while (prev == i) {
354+
i = lock.get();
348355
Thread.onSpinWait();
349-
k = lock.get();
350356
}
351357
}
352358
});
353359

354360
// This thread tries to acquire the scopes, then check if it is alive after an acquire failure
355361
Thread t2 = new Thread(() -> {
356-
for (int i = 0; i < iteration; i++) {
362+
for (int i = 0; i < iteration;) {
357363
MemorySessionImpl scope = scopes[i];
358364
while (true) {
359365
try {
360366
scope.acquire0();
361367
} catch (IllegalStateException e) {
368+
// The scope has been closed, proceed to the next iteration
362369
if (scope.isAlive()) {
363370
result[0] = true;
364371
}
365372
break;
366373
}
374+
// Release and try again
367375
scope.release0();
368376
}
369-
// Keep the 2 threads operating on the same scope
370-
int k = lock.getAndAdd(1) + 1;
371-
while (k != i * 2) {
372-
Thread.onSpinWait();
373-
k = lock.get();
374-
}
377+
// Proceed to the next iteration
378+
i = lock.getAndAdd(1) + 1;
375379
}
376380
});
377381

0 commit comments

Comments
 (0)