< prev index next >

src/java.base/share/classes/java/lang/VirtualThread.java

Print this page
*** 22,10 ***
--- 22,11 ---
   * or visit www.oracle.com if you need additional information or have any
   * questions.
   */
  package java.lang;
  
+ import java.lang.ref.Reference;
  import java.security.AccessController;
  import java.security.PrivilegedAction;
  import java.util.Locale;
  import java.util.Objects;
  import java.util.concurrent.CountDownLatch;

*** 51,10 ***
--- 52,12 ---
  import jdk.internal.vm.ContinuationScope;
  import jdk.internal.vm.StackableScope;
  import jdk.internal.vm.ThreadContainer;
  import jdk.internal.vm.ThreadContainers;
  import jdk.internal.vm.annotation.ChangesCurrentThread;
+ import jdk.internal.vm.annotation.ForceInline;
+ import jdk.internal.vm.annotation.Hidden;
  import jdk.internal.vm.annotation.JvmtiMountTransition;
  import sun.nio.ch.Interruptible;
  import sun.security.action.GetPropertyAction;
  import static java.util.concurrent.TimeUnit.*;
  

*** 268,30 ***
       * from its carrier thread when the task completes.
       */
      @ChangesCurrentThread
      private void run(Runnable task) {
          assert state == RUNNING;
-         boolean notifyJvmti = notifyJvmtiEvents;
  
          // first mount
          mount();
!         if (notifyJvmti) notifyJvmtiMountEnd(true);
  
          // emit JFR event if enabled
          if (VirtualThreadStartEvent.isTurnedOn()) {
              var event = new VirtualThreadStartEvent();
              event.javaThreadId = threadId();
              event.commit();
          }
  
          try {
!             task.run();
          } catch (Throwable exc) {
              dispatchUncaughtException(exc);
          } finally {
              try {
- 
                  // pop any remaining scopes from the stack, this may block
                  StackableScope.popAll();
  
                  // emit JFR event if enabled
                  if (VirtualThreadEndEvent.isTurnedOn()) {
--- 271,29 ---
       * from its carrier thread when the task completes.
       */
      @ChangesCurrentThread
      private void run(Runnable task) {
          assert state == RUNNING;
  
          // first mount
          mount();
!         if (notifyJvmtiEvents) notifyJvmtiMountEnd(true);
  
          // emit JFR event if enabled
          if (VirtualThreadStartEvent.isTurnedOn()) {
              var event = new VirtualThreadStartEvent();
              event.javaThreadId = threadId();
              event.commit();
          }
  
+         Object bindings = scopedValueBindings();
          try {
!             runWith(bindings, task);
          } catch (Throwable exc) {
              dispatchUncaughtException(exc);
          } finally {
              try {
                  // pop any remaining scopes from the stack, this may block
                  StackableScope.popAll();
  
                  // emit JFR event if enabled
                  if (VirtualThreadEndEvent.isTurnedOn()) {

*** 300,19 ***
                      event.commit();
                  }
  
              } finally {
                  // last unmount
!                 if (notifyJvmti) notifyJvmtiUnmountBegin(true);
                  unmount();
  
                  // final state
                  setState(TERMINATED);
              }
          }
      }
  
      /**
       * Mounts this virtual thread onto the current platform thread. On
       * return, the current thread is the virtual thread.
       */
      @ChangesCurrentThread
--- 302,27 ---
                      event.commit();
                  }
  
              } finally {
                  // last unmount
!                 if (notifyJvmtiEvents) notifyJvmtiUnmountBegin(true);
                  unmount();
  
                  // final state
                  setState(TERMINATED);
              }
          }
      }
  
+     @Hidden
+     @ForceInline
+     private void runWith(Object bindings, Runnable op) {
+         ensureMaterializedForStackWalk(bindings);
+         op.run();
+         Reference.reachabilityFence(bindings);
+     }
+ 
      /**
       * Mounts this virtual thread onto the current platform thread. On
       * return, the current thread is the virtual thread.
       */
      @ChangesCurrentThread

*** 392,21 ***
       * thread when continued. When enabled, JVMTI must be notified from this method.
       * @return true if the yield was successful
       */
      @ChangesCurrentThread
      private boolean yieldContinuation() {
-         boolean notifyJvmti = notifyJvmtiEvents;
- 
          // unmount
!         if (notifyJvmti) notifyJvmtiUnmountBegin(false);
          unmount();
          try {
              return Continuation.yield(VTHREAD_SCOPE);
          } finally {
              // re-mount
              mount();
!             if (notifyJvmti) notifyJvmtiMountEnd(false);
          }
      }
  
      /**
       * Invoked after the continuation yields. If parking then it sets the state
--- 402,19 ---
       * thread when continued. When enabled, JVMTI must be notified from this method.
       * @return true if the yield was successful
       */
      @ChangesCurrentThread
      private boolean yieldContinuation() {
          // unmount
!         if (notifyJvmtiEvents) notifyJvmtiUnmountBegin(false);
          unmount();
          try {
              return Continuation.yield(VTHREAD_SCOPE);
          } finally {
              // re-mount
              mount();
!             if (notifyJvmtiEvents) notifyJvmtiMountEnd(false);
          }
      }
  
      /**
       * Invoked after the continuation yields. If parking then it sets the state

*** 486,12 ***
  
          // start thread
          boolean started = false;
          container.onStart(this); // may throw
          try {
!             // extent locals may be inherited
!             inheritExtentLocalBindings(container);
  
              // submit task to run thread
              submitRunContinuation();
              started = true;
          } finally {
--- 494,12 ---
  
          // start thread
          boolean started = false;
          container.onStart(this); // may throw
          try {
!             // scoped values may be inherited
!             inheritScopedValueBindings(container);
  
              // submit task to run thread
              submitRunContinuation();
              started = true;
          } finally {
< prev index next >