Skip to content

Commit

Permalink
Synchronize using the AtomicInteger instead of a synchronized block
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Sep 13, 2023
1 parent 544b6f8 commit 74869ef
Showing 1 changed file with 26 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,16 @@ default boolean hasSize() {
}

private static final CountDownLatch CACHED_SIZE_AVAIL_LATCH = new CountDownLatch(1);
private static final AtomicInteger INITIALIZING = new AtomicInteger(0);
private static final AtomicInteger INITIALIZATION_STATE = new AtomicInteger(0);
private static final UnsignedWord UNSET_SENTINEL = UnsignedUtils.MAX_VALUE;
private static UnsignedWord cachedSize = UNSET_SENTINEL;

public static boolean isInitialized() {
return INITIALIZING.get() > 1;
}
private static final int NOT_INITIALIZED = 0;
private static final int INITIALIZING = 1;
private static final int INITIALIZED = 2;

/**
*
* @return {@code true} when PhycialMemory.size() is still initializing
*/
private static boolean isInitializing() {
return INITIALIZING.get() == 1;
public static boolean isInitialized() {
return INITIALIZATION_STATE.get() == INITIALIZED;
}

/**
Expand All @@ -90,26 +86,25 @@ public static UnsignedWord size() {
throw VMError.shouldNotReachHere("Accessing the physical memory size may require allocation and synchronization");
}

synchronized (INITIALIZING) {
if (!isInitialized()) {
VMError.guarantee(INITIALIZING.get() <= 1, "Must not initialize twice");
if (isInitializing()) {
/*
* Recursive initializations need to wait for the one initializing thread to
* finish so as to get correct reads of the cachedSize value.
*/
try {
boolean expired = !CACHED_SIZE_AVAIL_LATCH.await(1L, TimeUnit.SECONDS);
if (expired) {
throw new InternalError("expired latch!");
}
VMError.guarantee(cachedSize != UNSET_SENTINEL, "Expected chached size to be set");
return cachedSize;
} catch (InterruptedException e) {
throw VMError.shouldNotReachHere("Interrupt on countdown latch!");
int initState = INITIALIZATION_STATE.get();
if (initState != INITIALIZED) {
if (initState == INITIALIZING) {
/*
* Recursive initializations need to wait for the one initializing thread to
* finish so as to get correct reads of the cachedSize value.
*/
try {
boolean expired = !CACHED_SIZE_AVAIL_LATCH.await(1L, TimeUnit.SECONDS);
if (expired) {
throw new InternalError("expired latch!");
}
VMError.guarantee(cachedSize != UNSET_SENTINEL, "Expected chached size to be set");
return cachedSize;
} catch (InterruptedException e) {
throw VMError.shouldNotReachHere("Interrupt on countdown latch!");
}
INITIALIZING.incrementAndGet();
}
if (INITIALIZATION_STATE.compareAndSet(NOT_INITIALIZED, INITIALIZING)) {
try {
long memoryLimit = SubstrateOptions.MaxRAM.getValue();
if (memoryLimit > 0) {
Expand All @@ -123,8 +118,9 @@ public static UnsignedWord size() {
// Now that we have set the cachedSize let other threads know it's
// available to use.
CACHED_SIZE_AVAIL_LATCH.countDown();
} finally {
INITIALIZING.incrementAndGet();
INITIALIZATION_STATE.set(INITIALIZED);
} catch (Throwable t) {
INITIALIZATION_STATE.set(NOT_INITIALIZED);
}
}
}
Expand Down

0 comments on commit 74869ef

Please sign in to comment.