src/java.base/share/classes/sun/nio/fs/Cancellable.java

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import sun.misc.ManagedLocalsThread;
  29 import jdk.internal.misc.Unsafe;
  30 import java.util.concurrent.ExecutionException;
  31 
  32 /**
  33  * Base implementation of a task (typically native) that polls a memory location
  34  * during execution so that it may be aborted/cancelled before completion. The
  35  * task is executed by invoking the {@link runInterruptibly} method defined
  36  * here and cancelled by invoking Thread.interrupt.
  37  */
  38 
  39 abstract class Cancellable implements Runnable {
  40     private static final Unsafe unsafe = Unsafe.getUnsafe();
  41 
  42     private final long pollingAddress;
  43     private final Object lock = new Object();
  44 
  45     // the following require lock when examining or changing
  46     private boolean completed;
  47     private Throwable exception;
  48 


 101         } finally {
 102             synchronized (lock) {
 103                 completed = true;
 104                 unsafe.freeMemory(pollingAddress);
 105             }
 106         }
 107     }
 108 
 109     /**
 110      * The task body. This should periodically poll the memory location
 111      * to check for cancellation.
 112      */
 113     abstract void implRun() throws Throwable;
 114 
 115     /**
 116      * Invokes the given task in its own thread. If this (meaning the current)
 117      * thread is interrupted then an attempt is make to cancel the background
 118      * thread by writing into the memory location that it polls cooperatively.
 119      */
 120     static void runInterruptibly(Cancellable task) throws ExecutionException {
 121         Thread t = new ManagedLocalsThread(task);
 122         t.start();
 123         boolean cancelledByInterrupt = false;
 124         while (t.isAlive()) {
 125             try {
 126                 t.join();
 127             } catch (InterruptedException e) {
 128                 cancelledByInterrupt = true;
 129                 task.cancel();
 130             }
 131         }
 132         if (cancelledByInterrupt)
 133             Thread.currentThread().interrupt();
 134         Throwable exc = task.exception();
 135         if (exc != null)
 136             throw new ExecutionException(exc);
 137     }
 138 }


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 

  28 import jdk.internal.misc.Unsafe;
  29 import java.util.concurrent.ExecutionException;
  30 
  31 /**
  32  * Base implementation of a task (typically native) that polls a memory location
  33  * during execution so that it may be aborted/cancelled before completion. The
  34  * task is executed by invoking the {@link runInterruptibly} method defined
  35  * here and cancelled by invoking Thread.interrupt.
  36  */
  37 
  38 abstract class Cancellable implements Runnable {
  39     private static final Unsafe unsafe = Unsafe.getUnsafe();
  40 
  41     private final long pollingAddress;
  42     private final Object lock = new Object();
  43 
  44     // the following require lock when examining or changing
  45     private boolean completed;
  46     private Throwable exception;
  47 


 100         } finally {
 101             synchronized (lock) {
 102                 completed = true;
 103                 unsafe.freeMemory(pollingAddress);
 104             }
 105         }
 106     }
 107 
 108     /**
 109      * The task body. This should periodically poll the memory location
 110      * to check for cancellation.
 111      */
 112     abstract void implRun() throws Throwable;
 113 
 114     /**
 115      * Invokes the given task in its own thread. If this (meaning the current)
 116      * thread is interrupted then an attempt is make to cancel the background
 117      * thread by writing into the memory location that it polls cooperatively.
 118      */
 119     static void runInterruptibly(Cancellable task) throws ExecutionException {
 120         Thread t = new Thread(null, task, "NIO-Task", 0, false);
 121         t.start();
 122         boolean cancelledByInterrupt = false;
 123         while (t.isAlive()) {
 124             try {
 125                 t.join();
 126             } catch (InterruptedException e) {
 127                 cancelledByInterrupt = true;
 128                 task.cancel();
 129             }
 130         }
 131         if (cancelledByInterrupt)
 132             Thread.currentThread().interrupt();
 133         Throwable exc = task.exception();
 134         if (exc != null)
 135             throw new ExecutionException(exc);
 136     }
 137 }