8343793: Test java/foreign/TestMemorySession.java is timing out

Reviewed-by: mcimadamore
This commit is contained in:
Quan Anh Mai 2024-11-09 09:39:23 +00:00
parent 8a69893e1d
commit 2614c991d8

View File

@ -326,52 +326,56 @@ public class TestMemorySession {
int iteration = 1000; int iteration = 1000;
AtomicInteger lock = new AtomicInteger(); AtomicInteger lock = new AtomicInteger();
boolean[] result = new boolean[1]; boolean[] result = new boolean[1];
lock.set(-2);
MemorySessionImpl[] scopes = new MemorySessionImpl[iteration]; MemorySessionImpl[] scopes = new MemorySessionImpl[iteration];
for (int i = 0; i < iteration; i++) { for (int i = 0; i < iteration; i++) {
scopes[i] = MemorySessionImpl.toMemorySession(Arena.ofShared()); scopes[i] = MemorySessionImpl.toMemorySession(Arena.ofShared());
} }
// These two threads proceed the scopes array in a lock-step manner, the first thread wait
// for the second thread on the lock variable, while the second thread wait for the first
// thread on the closing of the current scope
// This thread tries to close the scopes // This thread tries to close the scopes
Thread t1 = new Thread(() -> { Thread t1 = new Thread(() -> {
for (int i = 0; i < iteration; i++) { for (int i = 0; i < iteration;) {
MemorySessionImpl scope = scopes[i]; MemorySessionImpl scope = scopes[i];
while (true) { while (true) {
try { try {
scope.close(); scope.close();
// Continue to the next iteration after a successful close
break; break;
} catch (IllegalStateException e) {} } catch (IllegalStateException e) {
// Wait for the release and try again
} }
// Keep the 2 threads operating on the same scope }
int k = lock.getAndAdd(1) + 1; // Wait for the other thread to complete its iteration
while (k != i * 2) { int prev = i;
while (prev == i) {
i = lock.get();
Thread.onSpinWait(); Thread.onSpinWait();
k = lock.get();
} }
} }
}); });
// This thread tries to acquire the scopes, then check if it is alive after an acquire failure // This thread tries to acquire the scopes, then check if it is alive after an acquire failure
Thread t2 = new Thread(() -> { Thread t2 = new Thread(() -> {
for (int i = 0; i < iteration; i++) { for (int i = 0; i < iteration;) {
MemorySessionImpl scope = scopes[i]; MemorySessionImpl scope = scopes[i];
while (true) { while (true) {
try { try {
scope.acquire0(); scope.acquire0();
} catch (IllegalStateException e) { } catch (IllegalStateException e) {
// The scope has been closed, proceed to the next iteration
if (scope.isAlive()) { if (scope.isAlive()) {
result[0] = true; result[0] = true;
} }
break; break;
} }
// Release and try again
scope.release0(); scope.release0();
} }
// Keep the 2 threads operating on the same scope // Proceed to the next iteration
int k = lock.getAndAdd(1) + 1; i = lock.getAndAdd(1) + 1;
while (k != i * 2) {
Thread.onSpinWait();
k = lock.get();
}
} }
}); });