Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8277090: jsr166 refresh for jdk19
Reviewed-by: alanb, psandoz
  • Loading branch information
Doug Lea committed May 4, 2022
1 parent c5a0687 commit 00e6c63
Show file tree
Hide file tree
Showing 13 changed files with 2,722 additions and 1,636 deletions.
Expand Up @@ -438,7 +438,7 @@ public static interface AsynchronousCompletionTask {
ForkJoinPool.commonPool() : new ThreadPerTaskExecutor();

/** Fallback if ForkJoinPool.commonPool() cannot support parallelism */
static final class ThreadPerTaskExecutor implements Executor {
private static final class ThreadPerTaskExecutor implements Executor {
public void execute(Runnable r) {
Objects.requireNonNull(r);
new Thread(r).start();
Expand Down Expand Up @@ -2134,6 +2134,38 @@ public T getNow(T valueIfAbsent) {
return ((r = result) == null) ? valueIfAbsent : (T) reportJoin(r);
}

@Override
public T resultNow() {
Object r = result;
if (r != null) {
if (r instanceof AltResult alt) {
if (alt.ex == null) return null;
} else {
@SuppressWarnings("unchecked")
T t = (T) r;
return t;
}
}
throw new IllegalStateException();
}

@Override
public Throwable exceptionNow() {
Object r = result;
Throwable x;
if (r instanceof AltResult alt
&& ((x = alt.ex) != null)
&& !(x instanceof CancellationException)) {
if (x instanceof CompletionException) {
Throwable cause = x.getCause();
if (cause != null)
x = cause;
}
return x;
}
throw new IllegalStateException();
}

/**
* If not already completed, sets the value returned by {@link
* #get()} and related methods to the given value.
Expand Down Expand Up @@ -2509,6 +2541,20 @@ public boolean isCompletedExceptionally() {
return ((r = result) instanceof AltResult) && r != NIL;
}

@Override
public State state() {
Object r = result;
if (r == null)
return State.RUNNING;
if (r != NIL && r instanceof AltResult alt) {
if (alt.ex instanceof CancellationException)
return State.CANCELLED;
else
return State.FAILED;
}
return State.SUCCESS;
}

/**
* Forcibly sets or resets the value subsequently returned by
* method {@link #get()} and related methods, whether or not
Expand Down Expand Up @@ -2912,6 +2958,10 @@ static final class MinimalStage<T> extends CompletableFuture<T> {
throw new UnsupportedOperationException(); }
@Override public T join() {
throw new UnsupportedOperationException(); }
@Override public T resultNow() {
throw new UnsupportedOperationException(); }
@Override public Throwable exceptionNow() {
throw new UnsupportedOperationException(); }
@Override public boolean complete(T value) {
throw new UnsupportedOperationException(); }
@Override public boolean completeExceptionally(Throwable ex) {
Expand All @@ -2928,6 +2978,8 @@ static final class MinimalStage<T> extends CompletableFuture<T> {
throw new UnsupportedOperationException(); }
@Override public boolean isCompletedExceptionally() {
throw new UnsupportedOperationException(); }
@Override public State state() {
throw new UnsupportedOperationException(); }
@Override public int getNumberOfDependents() {
throw new UnsupportedOperationException(); }
@Override public CompletableFuture<T> completeAsync
Expand Down
Expand Up @@ -35,8 +35,7 @@

package java.util.concurrent;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
import jdk.internal.misc.Unsafe;

/**
* A {@link ForkJoinTask} with a completion action performed when
Expand Down Expand Up @@ -540,7 +539,7 @@ public final void setPendingCount(int count) {
* @param delta the value to add
*/
public final void addToPendingCount(int delta) {
PENDING.getAndAdd(this, delta);
U.getAndAddInt(this, PENDING, delta);
}

/**
Expand All @@ -552,12 +551,12 @@ public final void addToPendingCount(int delta) {
* @return {@code true} if successful
*/
public final boolean compareAndSetPendingCount(int expected, int count) {
return PENDING.compareAndSet(this, expected, count);
return U.compareAndSetInt(this, PENDING, expected, count);
}

// internal-only weak version
final boolean weakCompareAndSetPendingCount(int expected, int count) {
return PENDING.weakCompareAndSet(this, expected, count);
return U.weakCompareAndSetInt(this, PENDING, expected, count);
}

/**
Expand Down Expand Up @@ -733,7 +732,6 @@ public final void helpComplete(int maxTasks) {
if (q != null && maxTasks > 0)
q.helpComplete(this, owned, maxTasks);
}

// ForkJoinTask overrides

/**
Expand Down Expand Up @@ -779,15 +777,16 @@ protected final boolean exec() {
@Override
protected void setRawResult(T t) { }

// VarHandle mechanics
private static final VarHandle PENDING;
/*
* This class uses jdk-internal Unsafe for atomics and special
* memory modes, rather than VarHandles, to avoid initialization
* dependencies in other jdk components that require early
* parallelism.
*/
private static final Unsafe U;
private static final long PENDING;
static {
try {
MethodHandles.Lookup l = MethodHandles.lookup();
PENDING = l.findVarHandle(CountedCompleter.class, "pending", int.class);

} catch (ReflectiveOperationException e) {
throw new ExceptionInInitializerError(e);
}
U = Unsafe.getUnsafe();
PENDING = U.objectFieldOffset(CountedCompleter.class, "pending");
}
}
Expand Up @@ -102,9 +102,20 @@
* }
* }}</pre>
*
* The following method shuts down an {@code ExecutorService} in two phases,
* first by calling {@code shutdown} to reject incoming tasks, and then
* calling {@code shutdownNow}, if necessary, to cancel any lingering tasks:
* An {@code ExecutorService} may also be established and closed
* (shutdown, blocking until terminated) as follows; illustrating with
* a different {@code Executors} factory method:
*
* <pre> {@code
* try (ExecutorService e = Executors.newWorkStealingPool()) {
* // submit or execute many tasks with e ...
* }}</pre>
*
* Further customization is also possible. For example, the following
* method shuts down an {@code ExecutorService} in two phases, first
* by calling {@code shutdown} to reject incoming tasks, and then
* calling {@code shutdownNow}, if necessary, to cancel any lingering
* tasks:
*
* <pre> {@code
* void shutdownAndAwaitTermination(ExecutorService pool) {
Expand Down Expand Up @@ -135,7 +146,7 @@
* @since 1.5
* @author Doug Lea
*/
public interface ExecutorService extends Executor {
public interface ExecutorService extends Executor, AutoCloseable {

/**
* Initiates an orderly shutdown in which previously submitted
Expand Down Expand Up @@ -368,4 +379,52 @@ <T> T invokeAny(Collection<? extends Callable<T>> tasks)
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout, TimeUnit unit)
throws InterruptedException, ExecutionException, TimeoutException;

/**
* Initiates an orderly shutdown in which previously submitted tasks are
* executed, but no new tasks will be accepted. This method waits until all
* tasks have completed execution and the executor has terminated.
*
* <p> If interrupted while waiting, this method stops all executing tasks as
* if by invoking {@link #shutdownNow()}. It then continues to wait until all
* actively executing tasks have completed. Tasks that were awaiting
* execution are not executed. The interrupt status will be re-asserted
* before this method returns.
*
* <p> If already terminated, invoking this method has no effect.
*
* @implSpec
* The default implementation invokes {@code shutdown()} and waits for tasks
* to complete execution with {@code awaitTermination}.
*
* @throws SecurityException if a security manager exists and
* shutting down this ExecutorService may manipulate
* threads that the caller is not permitted to modify
* because it does not hold {@link
* java.lang.RuntimePermission}{@code ("modifyThread")},
* or the security manager's {@code checkAccess} method
* denies access.
* @since 19
*/
@Override
default void close() {
boolean terminated = isTerminated();
if (!terminated) {
shutdown();
boolean interrupted = false;
while (!terminated) {
try {
terminated = awaitTermination(1L, TimeUnit.DAYS);
} catch (InterruptedException e) {
if (!interrupted) {
shutdownNow();
interrupted = true;
}
}
}
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
}

3 comments on commit 00e6c63

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@varada1110
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on 00e6c63 Dec 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@varada1110 Could not automatically backport 00e6c63c to openjdk/jdk17u-dev due to conflicts in the following files:

  • src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java
  • src/java.base/share/classes/java/util/concurrent/RecursiveTask.java

Please fetch the appropriate branch/commit and manually resolve these conflicts by using the following commands in your personal fork of openjdk/jdk17u-dev. Note: these commands are just some suggestions and you can use other equivalent commands you know.

# Fetch the up-to-date version of the target branch
$ git fetch --no-tags https://git.openjdk.org/jdk17u-dev.git master:master

# Check out the target branch and create your own branch to backport
$ git checkout master
$ git checkout -b backport-varada1110-00e6c63c

# Fetch the commit you want to backport
$ git fetch --no-tags https://git.openjdk.org/jdk.git 00e6c63cd12e3f92d0c1d007aab4f74915616ffb

# Backport the commit
$ git cherry-pick --no-commit 00e6c63cd12e3f92d0c1d007aab4f74915616ffb
# Resolve conflicts now

# Commit the files you have modified
$ git add files/with/resolved/conflicts
$ git commit -m 'Backport 00e6c63cd12e3f92d0c1d007aab4f74915616ffb'

Once you have resolved the conflicts as explained above continue with creating a pull request towards the openjdk/jdk17u-dev with the title Backport 00e6c63cd12e3f92d0c1d007aab4f74915616ffb.

Please sign in to comment.