This is the body of the Iterator. This method is executed as a 96 | * {@link Coroutine} to {@link #produce} the values of the Iterator.
97 | * 98 | *Note that this method is suspended each time it calls produce. And if 99 | * the consumer does not consume all values of the Iterator then this 100 | * method does not get the change to finish it's execution. This also 101 | * includes the finally blocks.
102 | * 103 | *This method must only suspend by calling produce. Any other reason 104 | * for suspension will cause a busy loop in the Iterator.
105 | * 106 | * @throws de.matthiasmann.continuations.SuspendExecution 107 | */ 108 | protected abstract void run() throws SuspendExecution; 109 | 110 | private class DelegateExecute implements CoroutineProto, Serializable { 111 | private static final long serialVersionUID = 12784529515412L; 112 | 113 | public void coExecute() throws SuspendExecution { 114 | CoIterator.this.run(); 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/CoroutineProto.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008, Matthias Mann 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of Matthias Mann nor the names of its 14 | * contributors may be used to endorse or promote products derived from 15 | * this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package de.matthiasmann.continuations; 30 | 31 | /** 32 | * A class that implements this interface can be run as a Coroutine. 33 | * 34 | * @see Coroutine 35 | * @author Matthias Mann 36 | */ 37 | public interface CoroutineProto { 38 | 39 | /** 40 | * Entry point for Coroutine execution. 41 | * 42 | * This method should never be called directly. 43 | * 44 | * @see Coroutine#Coroutine(de.matthiasmann.continuations.CoroutineProto) 45 | * @see Coroutine#run() 46 | * @see SuspendExecution 47 | * @throws de.matthiasmann.continuations.SuspendExecution This exception should never be cought 48 | */ 49 | public void coExecute() throws SuspendExecution; 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/SomeInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this template, choose Tools | Templates 3 | * and open the template in the editor. 4 | */ 5 | 6 | package de.matthiasmann.continuations; 7 | 8 | /** 9 | * A dummy interface used for the InterfaceTest 10 | * 11 | * @author Elias Naur 12 | */ 13 | public interface SomeInterface { 14 | 15 | void doStuff() throws SuspendExecution; 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/SuspendExecution.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008, Matthias Mann 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of Matthias Mann nor the names of its 14 | * contributors may be used to endorse or promote products derived from 15 | * this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package de.matthiasmann.continuations; 30 | 31 | /** 32 | *An exception used to initiate the control transfer.
33 | *It does not support stack traces.
34 | * 35 | *Methods which are declared to throw this exception are "suspendable". This 36 | * exception must always be propagated and never be caught.
37 | * 38 | *Generic try/catch handlers are allowed:
39 | * {@code try{ doSomething(); } catch(Throwable ex) { handleException(ex); } }
The instrumentation ANT task will enhance the bytecode of these methods to 42 | * support suspension and continuation of their execution.
43 | * 44 | * @author Matthias Mann 45 | */ 46 | @SuppressWarnings("serial") 47 | public final class SuspendExecution extends Exception { 48 | 49 | static final SuspendExecution instance = new SuspendExecution(); 50 | 51 | private SuspendExecution() { 52 | } 53 | 54 | @Override 55 | public Throwable fillInStackTrace() { 56 | return this; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/Util.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008, Matthias Mann 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of Matthias Mann nor the names of its 14 | * contributors may be used to endorse or promote products derived from 15 | * this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package de.matthiasmann.continuations; 30 | 31 | /** 32 | * 33 | * @author Matthias Mann 34 | */ 35 | public class Util { 36 | 37 | public static int[] copyOf(int[] src, int size) { 38 | int[] dst = new int[size]; 39 | System.arraycopy(src, 0, dst, 0, Math.min(src.length, size)); 40 | return dst; 41 | } 42 | 43 | public static long[] copyOf(long[] src, int size) { 44 | long[] dst = new long[size]; 45 | System.arraycopy(src, 0, dst, 0, Math.min(src.length, size)); 46 | return dst; 47 | } 48 | 49 | public static Object[] copyOf(Object[] src, int size) { 50 | Object[] dst = new Object[size]; 51 | System.arraycopy(src, 0, dst, 0, Math.min(src.length, size)); 52 | return dst; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/instrument/AlreadyInstrumented.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008, Matthias Mann 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of Matthias Mann nor the names of its 14 | * contributors may be used to endorse or promote products derived from 15 | * this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package de.matthiasmann.continuations.instrument; 30 | 31 | import java.lang.annotation.ElementType; 32 | import java.lang.annotation.Retention; 33 | import java.lang.annotation.RetentionPolicy; 34 | import java.lang.annotation.Target; 35 | 36 | /** 37 | * An annotation used to mark a class as instrumented. 38 | * It must never be used in Java source code. 39 | * 40 | * @author Matthias Mann 41 | */ 42 | @Target(ElementType.TYPE) 43 | @Retention(RetentionPolicy.RUNTIME) 44 | public @interface AlreadyInstrumented { 45 | } 46 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/instrument/CheckInstrumentationVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013, Matthias Mann 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of Matthias Mann nor the names of its 14 | * contributors may be used to endorse or promote products derived from 15 | * this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package de.matthiasmann.continuations.instrument; 30 | 31 | import de.matthiasmann.continuations.SuspendExecution; 32 | import de.matthiasmann.continuations.instrument.MethodDatabase.ClassEntry; 33 | import org.objectweb.asm.AnnotationVisitor; 34 | import org.objectweb.asm.ClassVisitor; 35 | import org.objectweb.asm.MethodVisitor; 36 | import org.objectweb.asm.Opcodes; 37 | import org.objectweb.asm.Type; 38 | 39 | /** 40 | * Check if a class contains suspendable methods. 41 | * Basicly this class checks if a method is declared to throw {@link SuspendExecution}. 42 | * 43 | * @author Matthias Mann 44 | */ 45 | public class CheckInstrumentationVisitor extends ClassVisitor { 46 | 47 | static final String EXCEPTION_NAME = Type.getInternalName(SuspendExecution.class); 48 | static final String EXCEPTION_DESC = Type.getDescriptor(SuspendExecution.class); 49 | 50 | private String className; 51 | private ClassEntry classEntry; 52 | private boolean hasSuspendable; 53 | private boolean alreadyInstrumented; 54 | 55 | public CheckInstrumentationVisitor() { 56 | super(Opcodes.ASM4); 57 | } 58 | 59 | public boolean needsInstrumentation() { 60 | return hasSuspendable; 61 | } 62 | 63 | ClassEntry getClassEntry() { 64 | return classEntry; 65 | } 66 | 67 | public String getName() { 68 | return className; 69 | } 70 | 71 | public boolean isAlreadyInstrumented() { 72 | return alreadyInstrumented; 73 | } 74 | 75 | @Override 76 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { 77 | this.className = name; 78 | this.classEntry = new ClassEntry(superName); 79 | } 80 | 81 | @Override 82 | public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 83 | if(desc.equals(InstrumentClass.ALREADY_INSTRUMENTED_NAME)) { 84 | alreadyInstrumented = true; 85 | } 86 | return null; 87 | } 88 | 89 | @Override 90 | public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { 91 | boolean suspendable = checkExceptions(exceptions); 92 | if(suspendable) { 93 | hasSuspendable = true; 94 | // synchronized methods can't be made suspendable 95 | if((access & Opcodes.ACC_SYNCHRONIZED) == Opcodes.ACC_SYNCHRONIZED) { 96 | throw new UnableToInstrumentException("synchronized method", className, name, desc); 97 | } 98 | } 99 | classEntry.set(name, desc, suspendable); 100 | return null; 101 | } 102 | 103 | public static boolean checkExceptions(String[] exceptions) { 104 | if(exceptions != null) { 105 | for(String ex : exceptions) { 106 | if(ex.equals(EXCEPTION_NAME)) { 107 | return true; 108 | } 109 | } 110 | } 111 | return false; 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/instrument/DBClassWriter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013, Matthias Mann 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * * Neither the name of Matthias Mann nor the names of its contributors may 15 | * be used to endorse or promote products derived from this software 16 | * without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package de.matthiasmann.continuations.instrument; 31 | 32 | import org.objectweb.asm.ClassReader; 33 | import org.objectweb.asm.ClassWriter; 34 | 35 | /** 36 | * 37 | * @author Matthias Mann 38 | */ 39 | public class DBClassWriter extends ClassWriter { 40 | 41 | private final MethodDatabase db; 42 | 43 | public DBClassWriter(MethodDatabase db, ClassReader classReader) { 44 | super(classReader, COMPUTE_FRAMES | COMPUTE_MAXS); 45 | this.db = db; 46 | } 47 | 48 | @Override 49 | protected String getCommonSuperClass(String type1, String type2) { 50 | return db.getCommonSuperClass(type1, type2); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/instrument/ExtractSuperClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2012, Matthias Mann 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * * Neither the name of Matthias Mann nor the names of its contributors may 15 | * be used to endorse or promote products derived from this software 16 | * without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package de.matthiasmann.continuations.instrument; 31 | 32 | import org.objectweb.asm.ClassVisitor; 33 | import org.objectweb.asm.Opcodes; 34 | 35 | /** 36 | * 37 | * @author Matthias Mann 38 | */ 39 | public class ExtractSuperClass extends ClassVisitor { 40 | 41 | String superClass; 42 | 43 | public ExtractSuperClass() { 44 | super(Opcodes.ASM4); 45 | } 46 | 47 | @Override 48 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { 49 | this.superClass = superName; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/instrument/InstrumentClass.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2013, Matthias Mann 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * * Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * * Redistributions in binary form must reproduce the above copyright 11 | * notice, this list of conditions and the following disclaimer in the 12 | * documentation and/or other materials provided with the distribution. 13 | * * Neither the name of Matthias Mann nor the names of its 14 | * contributors may be used to endorse or promote products derived from 15 | * this software without specific prior written permission. 16 | * 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 | * POSSIBILITY OF SUCH DAMAGE. 28 | */ 29 | package de.matthiasmann.continuations.instrument; 30 | 31 | import de.matthiasmann.continuations.Coroutine; 32 | import de.matthiasmann.continuations.instrument.MethodDatabase.ClassEntry; 33 | 34 | import java.util.ArrayList; 35 | import java.util.List; 36 | 37 | import org.objectweb.asm.AnnotationVisitor; 38 | import org.objectweb.asm.ClassVisitor; 39 | import org.objectweb.asm.MethodVisitor; 40 | import org.objectweb.asm.Opcodes; 41 | import org.objectweb.asm.Type; 42 | import org.objectweb.asm.tree.MethodNode; 43 | import org.objectweb.asm.tree.analysis.AnalyzerException; 44 | 45 | /** 46 | * Instrument a class by instrumenting all suspendable methods and copying the 47 | * others. 48 | * 49 | * @author Matthias Mann 50 | */ 51 | @SuppressWarnings("all") 52 | public class InstrumentClass extends ClassVisitor { 53 | 54 | static final String COROUTINE_NAME = Type.getInternalName(Coroutine.class); 55 | static final String ALREADY_INSTRUMENTED_NAME = Type.getDescriptor(AlreadyInstrumented.class); 56 | 57 | private final MethodDatabase db; 58 | private final boolean forceInstrumentation; 59 | private String className; 60 | private ClassEntry classEntry; 61 | private boolean alreadyInstrumented; 62 | private ArrayListThis exception is thrown when an unsupported construct was found in a class 33 | * that must be instrumented for suspension.
34 | * 35 | *Note: this needs to be a RuntimeException - otherwise it can't be thrown 36 | * from {@link CheckInstrumentationVisitor}.
37 | * 38 | * @author Matthias Mann 39 | */ 40 | @SuppressWarnings("serial") 41 | public class UnableToInstrumentException extends RuntimeException { 42 | 43 | private final String reason; 44 | private final String className; 45 | private final String methodName; 46 | private final String methodDesc; 47 | 48 | public UnableToInstrumentException(String reason, String className, String methodName, String methodDesc) { 49 | super(String.format("Unable to instrument class %s#%s%s because of %s", className, methodName, methodDesc, reason)); 50 | this.reason = reason; 51 | this.className = className; 52 | this.methodName = methodName; 53 | this.methodDesc = methodDesc; 54 | } 55 | 56 | public String getClassName() { 57 | return className; 58 | } 59 | 60 | public String getMethodName() { 61 | return methodName; 62 | } 63 | 64 | public String getMethodDesc() { 65 | return methodDesc; 66 | } 67 | 68 | public String getReason() { 69 | return reason; 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/instrument/UnresolvedValue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2008-2012, Matthias Mann 3 | * 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * * Redistributions of source code must retain the above copyright notice, 10 | * this list of conditions and the following disclaimer. 11 | * * Redistributions in binary form must reproduce the above copyright 12 | * notice, this list of conditions and the following disclaimer in the 13 | * documentation and/or other materials provided with the distribution. 14 | * * Neither the name of Matthias Mann nor the names of its contributors may 15 | * be used to endorse or promote products derived from this software 16 | * without specific prior written permission. 17 | * 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 22 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 26 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | package de.matthiasmann.continuations.instrument; 31 | 32 | import org.objectweb.asm.Type; 33 | import org.objectweb.asm.tree.analysis.BasicValue; 34 | 35 | /** 36 | * 37 | * @author Matthias Mann 38 | */ 39 | public class UnresolvedValue extends BasicValue { 40 | 41 | final BasicValue v; 42 | final BasicValue w; 43 | 44 | public UnresolvedValue(BasicValue v, BasicValue w) { 45 | super(Type.VOID_TYPE); 46 | this.v = v; 47 | this.w = w; 48 | } 49 | 50 | @Override 51 | public int getSize() { 52 | return 0; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/instrument/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | *4 | * Copyright (c) 2008, Matthias Mann 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * * Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of Matthias Mann nor the names of its 16 | * contributors may be used to endorse or promote products derived from 17 | * this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | *31 | * 32 | * @see de.matthiasmann.continuations.instrument.InstrumentationTask 33 | */ 34 | package de.matthiasmann.continuations.instrument; 35 | -------------------------------------------------------------------------------- /src/de/matthiasmann/continuations/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | *
4 | * Copyright (c) 2008, Matthias Mann 5 | * All rights reserved. 6 | * 7 | * Redistribution and use in source and binary forms, with or without 8 | * modification, are permitted provided that the following conditions are met: 9 | * 10 | * * Redistributions of source code must retain the above copyright notice, 11 | * this list of conditions and the following disclaimer. 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * * Neither the name of Matthias Mann nor the names of its 16 | * contributors may be used to endorse or promote products derived from 17 | * this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | * POSSIBILITY OF SUCH DAMAGE. 30 | *31 | * 32 | *
This package contains the runtime of the Continuations library.
33 | * 34 | * @see de.matthiasmann.continuations.Coroutine 35 | * @author Matthias Mann 36 | */ 37 | package de.matthiasmann.continuations; 38 | -------------------------------------------------------------------------------- /src/net/indiespot/continuations/VirtualRunnable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, Enhanced Four 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are 7 | * met: 8 | * 9 | * * Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of 'Enhanced Four' nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | package net.indiespot.continuations; 34 | 35 | import java.io.Serializable; 36 | 37 | import de.matthiasmann.continuations.SuspendExecution; 38 | 39 | public interface VirtualRunnable extends Serializable { 40 | 41 | public void run() throws SuspendExecution; 42 | } 43 | -------------------------------------------------------------------------------- /src/net/indiespot/continuations/VirtualThreadLocal.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2012, Enhanced Four 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are 7 | * met: 8 | * 9 | * * Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * * Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * * Neither the name of 'Enhanced Four' nor the names of its contributors 17 | * may be used to endorse or promote products derived from this software 18 | * without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 | */ 32 | 33 | package net.indiespot.continuations; 34 | 35 | import java.io.Serializable; 36 | import java.util.IdentityHashMap; 37 | import java.util.Map; 38 | 39 | 40 | @SuppressWarnings("serial") 41 | public class VirtualThreadLocal