< prev index next >

src/java.base/share/classes/java/util/concurrent/locks/LockSupport.java

Print this page
@@ -33,10 +33,11 @@
   * http://creativecommons.org/publicdomain/zero/1.0/
   */
  
  package java.util.concurrent.locks;
  
+ import jdk.internal.misc.VirtualThreads;
  import jdk.internal.misc.Unsafe;
  
  /**
   * Basic thread blocking primitives for creating locks and other
   * synchronization classes.

@@ -171,12 +172,17 @@
       *
       * @param thread the thread to unpark, or {@code null}, in which case
       *        this operation has no effect
       */
      public static void unpark(Thread thread) {
-         if (thread != null)
-             U.unpark(thread);
+         if (thread != null) {
+             if (thread.isVirtual()) {
+                 VirtualThreads.unpark(thread); // can throw RejectedExecutionException
+             } else {
+                 U.unpark(thread);
+             }
+         }
      }
  
      /**
       * Disables the current thread for thread scheduling purposes unless the
       * permit is available.

@@ -206,12 +212,19 @@
       * @since 1.6
       */
      public static void park(Object blocker) {
          Thread t = Thread.currentThread();
          setBlocker(t, blocker);
-         U.park(false, 0L);
-         setBlocker(t, null);
+         try {
+             if (t.isVirtual()) {
+                 VirtualThreads.park();
+             } else {
+                 U.park(false, 0L);
+             }
+         } finally {
+             setBlocker(t, null);
+         }
      }
  
      /**
       * Disables the current thread for thread scheduling purposes, for up to
       * the specified waiting time, unless the permit is available.

@@ -247,12 +260,19 @@
       */
      public static void parkNanos(Object blocker, long nanos) {
          if (nanos > 0) {
              Thread t = Thread.currentThread();
              setBlocker(t, blocker);
-             U.park(false, nanos);
-             setBlocker(t, null);
+             try {
+                 if (t.isVirtual()) {
+                     VirtualThreads.park(nanos);
+                 } else {
+                     U.park(false, nanos);
+                 }
+             } finally {
+                 setBlocker(t, null);
+             }
          }
      }
  
      /**
       * Disables the current thread for thread scheduling purposes, until

@@ -288,12 +308,19 @@
       * @since 1.6
       */
      public static void parkUntil(Object blocker, long deadline) {
          Thread t = Thread.currentThread();
          setBlocker(t, blocker);
-         U.park(true, deadline);
-         setBlocker(t, null);
+         try {
+             if (t.isVirtual()) {
+                 VirtualThreads.parkUntil(deadline);
+             } else {
+                 U.park(true, deadline);
+             }
+         } finally {
+             setBlocker(t, null);
+         }
      }
  
      /**
       * Returns the blocker object supplied to the most recent
       * invocation of a park method that has not yet unblocked, or null

@@ -336,11 +363,15 @@
       * method to return. Callers should re-check the conditions which caused
       * the thread to park in the first place. Callers may also determine,
       * for example, the interrupt status of the thread upon return.
       */
      public static void park() {
-         U.park(false, 0L);
+         if (Thread.currentThread().isVirtual()) {
+             VirtualThreads.park();
+         } else {
+             U.park(false, 0L);
+         }
      }
  
      /**
       * Disables the current thread for thread scheduling purposes, for up to
       * the specified waiting time, unless the permit is available.

@@ -370,12 +401,17 @@
       * upon return.
       *
       * @param nanos the maximum number of nanoseconds to wait
       */
      public static void parkNanos(long nanos) {
-         if (nanos > 0)
-             U.park(false, nanos);
+         if (nanos > 0) {
+             if (Thread.currentThread().isVirtual()) {
+                 VirtualThreads.park(nanos);
+             } else {
+                 U.park(false, nanos);
+             }
+         }
      }
  
      /**
       * Disables the current thread for thread scheduling purposes, until
       * the specified deadline, unless the permit is available.

@@ -405,26 +441,32 @@
       *
       * @param deadline the absolute time, in milliseconds from the Epoch,
       *        to wait until
       */
      public static void parkUntil(long deadline) {
-         U.park(true, deadline);
+         if (Thread.currentThread().isVirtual()) {
+             VirtualThreads.parkUntil(deadline);
+         } else {
+             U.park(true, deadline);
+         }
      }
  
      /**
       * Returns the thread id for the given thread.  We must access
       * this directly rather than via method Thread.getId() because
       * getId() has been known to be overridden in ways that do not
       * preserve unique mappings.
       */
      static final long getThreadId(Thread thread) {
-         return U.getLong(thread, TID);
+         // need to keep in sync with Thread::getId
+         return U.getLong(thread, TID) & TID_MASK;
      }
  
      // Hotspot implementation via intrinsics API
      private static final Unsafe U = Unsafe.getUnsafe();
      private static final long PARKBLOCKER
          = U.objectFieldOffset(Thread.class, "parkBlocker");
      private static final long TID
          = U.objectFieldOffset(Thread.class, "tid");
+     private static final long TID_MASK = (1L << 48) - 1;
  
  }
< prev index next >