Skip to content

Commit

Permalink
Moved classes AtomicUnsigned and AtomicWord to UninterruptibleUtils.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhaeubl committed Aug 20, 2019
1 parent 7be8d4c commit 700fcaa
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
import com.oracle.svm.core.genscavenge.HeapChunk.Header;
import com.oracle.svm.core.genscavenge.UnalignedHeapChunk.UnalignedHeader;
import com.oracle.svm.core.jdk.UninterruptibleUtils;
import com.oracle.svm.core.jdk.UninterruptibleUtils.AtomicUnsigned;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.os.CommittedMemoryProvider;
import com.oracle.svm.core.thread.VMThreads;
import com.oracle.svm.core.util.AtomicUnsigned;

/**
* Allocates and frees the memory for aligned and unaligned heap chunks. The methods are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,19 @@

import org.graalvm.compiler.api.replacements.Fold;
import org.graalvm.compiler.word.Word;
import org.graalvm.nativeimage.hosted.Feature.FeatureAccess;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature.FeatureAccess;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.SubstrateUtil;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.heap.GCCause;
import com.oracle.svm.core.heap.PhysicalMemory;
import com.oracle.svm.core.jdk.UninterruptibleUtils.AtomicUnsigned;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.option.XOptions;
import com.oracle.svm.core.util.AtomicUnsigned;
import com.oracle.svm.core.util.UnsignedUtils;
import com.oracle.svm.core.util.UserError;
import com.oracle.svm.core.util.VMError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import org.graalvm.compiler.serviceprovider.GraalUnsafeAccess;
import org.graalvm.word.PointerBase;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordBase;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.annotate.Uninterruptible;
Expand Down Expand Up @@ -144,6 +146,127 @@ public boolean compareAndSet(long expected, long update) {
}
}

/**
* A {@link WordBase word} value that may be updated atomically. See the
* {@link java.util.concurrent.atomic} package specification for description of the properties
* of atomic variables.
*
* Similar to {@link AtomicReference}, but for {@link WordBase word} types. A dedicated
* implementation is necessary because Object and word types cannot be mixed.
*/
public static class AtomicWord<T extends WordBase> {

/**
* For simplicity, we convert the word value to a long and delegate to existing atomic
* operations.
*/
protected final AtomicLong value;

/**
* Creates a new AtomicWord with initial value {@link WordFactory#zero}.
*/
public AtomicWord() {
value = new AtomicLong(0L);
}

/**
* Gets the current value.
*
* @return the current value
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final T get() {
return WordFactory.unsigned(value.get());
}

/**
* Sets to the given value.
*
* @param newValue the new value
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final void set(T newValue) {
value.set(newValue.rawValue());
}

/**
* Atomically sets to the given value and returns the old value.
*
* @param newValue the new value
* @return the previous value
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final T getAndSet(T newValue) {
return WordFactory.unsigned(value.getAndSet(newValue.rawValue()));
}

/**
* Atomically sets the value to the given updated value if the current value {@code ==} the
* expected value.
*
* @param expect the expected value
* @param update the new value
* @return {@code true} if successful. False return indicates that the actual value was not
* equal to the expected value.
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final boolean compareAndSet(T expect, T update) {
return value.compareAndSet(expect.rawValue(), update.rawValue());
}
}

/**
* A {@link UnsignedWord} value that may be updated atomically. See the
* {@link java.util.concurrent.atomic} package specification for description of the properties
* of atomic variables.
*/
public static class AtomicUnsigned extends AtomicWord<UnsignedWord> {

/**
* Atomically adds the given value to the current value.
*
* @param delta the value to add
* @return the previous value
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final UnsignedWord getAndAdd(UnsignedWord delta) {
return WordFactory.unsigned(value.getAndAdd(delta.rawValue()));
}

/**
* Atomically adds the given value to the current value.
*
* @param delta the value to add
* @return the updated value
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final UnsignedWord addAndGet(UnsignedWord delta) {
return WordFactory.unsigned(value.addAndGet(delta.rawValue()));
}

/**
* Atomically subtracts the given value from the current value.
*
* @param delta the value to add
* @return the previous value
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final UnsignedWord getAndSubtract(UnsignedWord delta) {
return WordFactory.unsigned(value.getAndAdd(-delta.rawValue()));
}

/**
* Atomically subtracts the given value from the current value.
*
* @param delta the value to add
* @return the updated value
*/
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
public final UnsignedWord subtractAndGet(UnsignedWord delta) {
return WordFactory.unsigned(value.addAndGet(-delta.rawValue()));
}
}

public static class AtomicPointer<T extends PointerBase> {

private static final Unsafe UNSAFE = GraalUnsafeAccess.getUnsafe();
Expand Down Expand Up @@ -248,7 +371,7 @@ public static class Long {
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
// Checkstyle: stop
public static int numberOfLeadingZeros(long i) {
// @formatter:off
// @formatter:off
// HD, Figure 5-6
if (i == 0)
return 64;
Expand All @@ -272,7 +395,7 @@ public static class Integer {
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
@SuppressWarnings("all")
public static int numberOfLeadingZeros(int i) {
// @formatter:off
// @formatter:off
// HD, Figure 5-6
if (i == 0)
return 32;
Expand All @@ -290,7 +413,7 @@ public static int numberOfLeadingZeros(int i) {
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
@SuppressWarnings("all")
public static int highestOneBit(int i) {
// @formatter:off
// @formatter:off
// HD, Figure 3-1
i |= (i >> 1);
i |= (i >> 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.oracle.svm.core.annotate.ForceFixedRegisterReads;
import com.oracle.svm.core.annotate.Uninterruptible;
import com.oracle.svm.core.jdk.UninterruptibleUtils;
import com.oracle.svm.core.jdk.UninterruptibleUtils.AtomicWord;
import com.oracle.svm.core.locks.VMCondition;
import com.oracle.svm.core.locks.VMMutex;
import com.oracle.svm.core.threadlocal.FastThreadLocalFactory;
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 700fcaa

Please sign in to comment.