Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: CPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Common Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/cpl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the CPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the CPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.ext.timeout;
 
 import org.jruby.Ruby;
 import static org.jruby.runtime.Visibility.*;
 
 public class Timeout implements Library {
     public void load(Ruby runtimeboolean wrapthrows IOException {
         RubyModule timeout = runtime.defineModule("Timeout");
         RubyClass superclass = runtime.is1_9() ? runtime.getRuntimeError() : runtime.getInterrupt();
         RubyClass timeoutError = runtime.defineClassUnder("Error"superclasssuperclass.getAllocator(), timeout);
         runtime.defineClassUnder("ExitException"runtime.getException(), runtime.getException().getAllocator(), timeout);
 
         // Here we create an "anonymous" exception type used for unrolling the stack.
         // MRI creates a new one for *every call* to timeout, which can be costly.
         // We opt to use a single exception type for all cases to avoid this overhead.
         RubyClass anonEx = runtime.defineClassUnder("AnonymousException"runtime.getException(), runtime.getException().getAllocator(), timeout);
         anonEx.setBaseName(null); // clear basename so it's anonymous when raising
 
         // These are not really used by timeout, but exposed for compatibility
         timeout.defineConstant("THIS_FILE", RubyRegexp.newRegexp(runtime"timeout\\.rb"new RegexpOptions()));
         timeout.defineConstant("CALLER_OFFSET", RubyFixnum.newFixnum(runtime, 0));
 
         // Timeout module methods
         timeout.defineAnnotatedMethods(Timeout.class);
 
         // Toplevel defines
         runtime.getObject().defineConstant("TimeoutError"timeoutError);
         runtime.getObject().defineAnnotatedMethods(TimeoutToplevel.class);
     }
 
 
     public static class TimeoutToplevel {
         @JRubyMethod(required = 1, optional = 1, visibility = )
         public static IRubyObject timeout(ThreadContext contextIRubyObject selfIRubyObject[] argsBlock block) {
             RubyModule timeout = context.runtime.getModule("Timeout");
             
             switch (args.length) {
             case 1:
                 return Timeout.timeout(contexttimeoutargs[0], block);
             case 2:
                 return Timeout.timeout(contexttimeoutargs[0], args[1], block);
             default:
                 Arity.raiseArgumentError(context.runtimeargs.length, 1, 2);
                 return context.runtime.getNil();
             }
         }
     }
    @JRubyMethod(module = true)
    public static IRubyObject timeout(final ThreadContext contextIRubyObject timeoutIRubyObject secondsBlock block) {
        // No seconds, just yield
        if (seconds.isNil() || RuntimeHelpers.invoke(contextseconds"zero?").isTrue()) {
            return block.yieldSpecific(context);
        }
        final Ruby runtime = context.runtime;
        // No timeout in critical section
        if (runtime.getThreadService().getCritical()) {
            return raiseBecauseCritical(context);
        }
        final RubyThread currentThread = context.getThread();
        final AtomicBoolean latch = new AtomicBoolean(false);
        IRubyObject id = new RubyObject(runtimeruntime.getObject());
        Runnable timeoutRunnable = prepareRunnable(currentThreadruntimelatchid);
        Future timeoutFuture = null;
        try {
            try {
                timeoutFuture = .schedule(timeoutRunnable,
                        (long)(seconds.convertToFloat().getDoubleValue() * 1000000), .);
                return block.yield(contextseconds);
            } finally {
                killTimeoutThread(contexttimeoutFuturelatch);
            }
        } catch (RaiseException re) {
            if (re.getException().getInternalVariable("__identifier__") == id) {
                return raiseTimeoutError(contextre);
            } else {
                throw re;
            }
        }
    }
    @JRubyMethod(module = true)
    public static IRubyObject timeout(final ThreadContext contextIRubyObject timeoutIRubyObject secondsIRubyObject exceptionTypeBlock block) {
        // No seconds, just yield
        if (seconds.isNil() || RuntimeHelpers.invoke(contextseconds"zero?").isTrue()) {
            return block.yieldSpecific(context);
        }
        final Ruby runtime = context.runtime;
        // No timeout in critical section
        if (runtime.getThreadService().getCritical()) {
            return raiseBecauseCritical(context);
        }
        final RubyThread currentThread = context.getThread();
        final AtomicBoolean latch = new AtomicBoolean(false);
        IRubyObject id = new RubyObject(runtimeruntime.getObject());
        RubyClass anonException = (RubyClass)runtime.getClassFromPath("Timeout::AnonymousException");
        Runnable timeoutRunnable = exceptionType.isNil() ?
                prepareRunnable(currentThreadruntimelatchid) :
                prepareRunnableWithException(currentThreadexceptionTyperuntimelatch);
        Future timeoutFuture = null;
        try {
            try {
                timeoutFuture = .schedule(timeoutRunnable,
                        (long)(seconds.convertToFloat().getDoubleValue() * 1000000), .);
                return block.yield(contextseconds);
            } finally {
                killTimeoutThread(contexttimeoutFuturelatch);
            }
        } catch (RaiseException re) {
            // if it's the exception we're expecting
            if (re.getException().getMetaClass() == anonException) {
                // and we were not given a specific exception
                if (exceptionType.isNil()) {
                    // and it's the exception intended for us
                    if (re.getException().getInternalVariable("__identifier__") == id) {
                        return raiseTimeoutError(contextre);
                    }
                }
            }
            // otherwise, rethrow
            throw re;
        }
    }
    private static Runnable prepareRunnable(final RubyThread currentThreadfinal Ruby runtimefinal AtomicBoolean latchfinal IRubyObject id) {
        Runnable timeoutRunnable = new Runnable() {
            public void run() {
                if (latch.compareAndSet(falsetrue)) {
                    if (currentThread.alive_p().isTrue()) {
                        RubyClass anonException = (RubyClass)runtime.getClassFromPath("Timeout::AnonymousException");
                        IRubyObject anonExceptionObj = anonException.newInstance(runtime.getCurrentContext(), runtime.newString("execution expired"), .);
                        anonExceptionObj.getInternalVariables().setInternalVariable("__identifier__"id);
                        currentThread.internalRaise(new IRubyObject[] {anonExceptionObj});
                    }
                }
            }
        };
        return timeoutRunnable;
    }
    private static Runnable prepareRunnableWithException(final RubyThread currentThreadfinal IRubyObject exceptionfinal Ruby runtimefinal AtomicBoolean latch) {
        Runnable timeoutRunnable = new Runnable() {
            public void run() {
                if (latch.compareAndSet(falsetrue)) {
                    if (currentThread.alive_p().isTrue()) {
                        currentThread.internalRaise(new IRubyObject[]{exceptionruntime.newString("execution expired")});
                    }
                }
            }
        };
        return timeoutRunnable;
    }
    private static void killTimeoutThread(ThreadContext contextFuture timeoutFutureAtomicBoolean latch) {
        if (latch.compareAndSet(falsetrue)) {
            // ok, exception will not fire
            timeoutFuture.cancel(false);
            if ( instanceof ScheduledThreadPoolExecutor && timeoutFuture instanceof Runnable) {
                ((ScheduledThreadPoolExecutor).remove((RunnabletimeoutFuture);
            }
        } else {
            // future is already in progress, wait for it to run and then poll
            try {
                timeoutFuture.get();
            } catch (ExecutionException ex) {
            } catch (InterruptedException ex) {
            }
            // poll to propagate exception from child thread
            context.pollThreadEvents();
        }
    }
    private static IRubyObject raiseBecauseCritical(ThreadContext context) {
        Ruby runtime = context.runtime;
        return RubyKernel.raise(contextruntime.getKernel(), new IRubyObject[]{runtime.getThreadError(), runtime.newString("timeout within critical section")}, .);
    }
    private static IRubyObject raiseTimeoutError(ThreadContext contextRaiseException re) {
        Ruby runtime = context.runtime;
        return RubyKernel.raise(
                context,
                runtime.getKernel(),
                new IRubyObject[]{
                    runtime.getClassFromPath("Timeout::Error"),
                    re.getException().callMethod(context"message"),
                    re.getException().callMethod(context"backtrace")},
                .);
    }
New to GrepCode? Check out our FAQ X