Package org.jruby

Examples of org.jruby.RubyThread$Unblocker


        getCurrentContext().setThread(null);
        localContext.set(null);
    }
   
    private RubyThread getRubyThreadFromThread(Thread activeThread) {
        RubyThread rubyThread = rubyThreadMap.get(activeThread);
       
        return rubyThread;
    }
View Full Code Here


        }
    }

    public static long nativeBlockingRegion(Ruby runtime, long blocking_func, long blocking_data,
            long unblocking_func, long unblocking_data) {
        RubyThread thread = runtime.getCurrentContext().getThread();
        NativeFunctionTask task = new NativeFunctionTask(Native.getInstance(runtime), blocking_func,
                blocking_data, unblocking_func, unblocking_data);
        int lockCount = GIL.releaseAllLocks();
        try {
            thread.executeBlockingTask(task);
        } catch (InterruptedException e) {}
        GIL.acquire(lockCount);
        return task.retval;
    }
View Full Code Here

        // No timeout in critical section
        if (runtime.getThreadService().getCritical()) {
            return raiseBecauseCritical(context);
        }

        final RubyThread currentThread = context.getThread();
        final AtomicBoolean latch = new AtomicBoolean(false);
       
        Runnable timeoutRunnable = prepareRunnable(currentThread, runtime, latch);
        Future timeoutFuture = null;
View Full Code Here

        if (runtime.getThreadService().getCritical()) {
            return raiseBecauseCritical(context);
        }

        final IRubyObject exception = exceptionType.isNil() ? runtime.getClassFromPath("Timeout::AnonymousException") : exceptionType;
        final RubyThread currentThread = context.getThread();
        final AtomicBoolean latch = new AtomicBoolean(false);
       
        Runnable timeoutRunnable = prepareRunnableWithException(currentThread, exception, runtime, latch);
        Future timeoutFuture = null;
View Full Code Here

    private boolean waitSelect(final int operations, final boolean blocking) throws IOException {
        if (!(io.getChannel() instanceof SelectableChannel)) {
            return true;
        }
        final Ruby runtime = getRuntime();
        RubyThread thread = runtime.getCurrentContext().getThread();

        SelectableChannel selectable = (SelectableChannel)io.getChannel();
        selectable.configureBlocking(false);
        final Selector selector = runtime.getSelectorPool().get();
        final SelectionKey key = selectable.register(selector, operations);

        try {
            io.addBlockingThread(thread);

            final int[] result = new int[1];

            thread.executeBlockingTask(new RubyThread.BlockingTask() {
                public void run() throws InterruptedException {
                    try {
                        if (!blocking) {
                            result[0] = selector.selectNow();
                            if (result[0] == 0) {
                                if ((operations & SelectionKey.OP_READ) != 0 && (operations & SelectionKey.OP_WRITE) != 0) {
                                    if (key.isReadable()) {
                                        writeWouldBlock();
                                    } else if (key.isWritable()) {
                                        readWouldBlock();
                                    } else { //neither, pick one
                                        readWouldBlock();
                                    }
                                } else if ((operations & SelectionKey.OP_READ) != 0) {
                                    readWouldBlock();
                                } else if ((operations & SelectionKey.OP_WRITE) != 0) {
                                    writeWouldBlock();
                                }
                            }
                        } else {
                            result[0] = selector.select();
                        }
                    } catch (IOException ioe) {
                        throw runtime.newRuntimeError("Error with selector: " + ioe.getMessage());
                    }
                }

                public void wakeup() {
                    selector.wakeup();
                }
            });

            if (result[0] >= 1) {
                Set<SelectionKey> keySet = selector.selectedKeys();

                if (keySet.iterator().next() == key) {
                    return true;
                }
            }

            return false;
        } catch (InterruptedException ie) {
            return false;
        } finally {
            // Note: I don't like ignoring these exceptions, but it's
            // unclear how likely they are to happen or what damage we
            // might do by ignoring them. Note that the pieces are separate
            // so that we can ensure one failing does not affect the others
            // running.

            // clean up the key in the selector
            try {
                if (key != null) key.cancel();
                if (selector != null) selector.selectNow();
            } catch (Exception e) {
                // ignore
            }

            // shut down and null out the selector
            try {
                if (selector != null) {
                    runtime.getSelectorPool().put(selector);
                }
            } catch (Exception e) {
                // ignore
            }

            // remove this thread as a blocker against the given IO
            io.removeBlockingThread(thread);

            // clear thread state from blocking call
            thread.afterBlockingCall();
        }
    }
View Full Code Here

        }
    }

    public static long nativeBlockingRegion(Ruby runtime, long blocking_func, long blocking_data,
            long unblocking_func, long unblocking_data) {
        RubyThread thread = runtime.getCurrentContext().getThread();
        NativeFunctionTask task = new NativeFunctionTask(Native.getInstance(runtime), blocking_func,
                blocking_data, unblocking_func, unblocking_data);

        GC.disable();
        int lockCount = GIL.releaseAllLocks();
        try {
            thread.executeBlockingTask(task);
        } catch (InterruptedException e) {
            // ignore
        } finally  {
            GIL.acquire(lockCount);
            GC.enable();
View Full Code Here

        // 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(runtime, runtime.getObject());
        Runnable timeoutRunnable = prepareRunnable(currentThread, runtime, latch, id);
        Future timeoutFuture = null;
View Full Code Here

        // 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(runtime, runtime.getObject());
        RubyClass anonException = (RubyClass)runtime.getClassFromPath("Timeout::AnonymousException");
        Runnable timeoutRunnable = exceptionType.isNil() ?
View Full Code Here

            }
           
            // If all streams are nil, just sleep the specified time (JRUBY-4699)
            if (args[0].isNil() && args[1].isNil() && args[2].isNil()) {
                if (timeout > 0) {
                    RubyThread thread = context.getThread();
                    long now = System.currentTimeMillis();
                    thread.sleep(timeout);
                    // Guard against spurious wakeup
                    while (System.currentTimeMillis() < now + timeout) {
                        thread.sleep(1);
                    }

                }
            } else {
                doSelect(runtime, has_timeout, timeout);
View Full Code Here

    public IRubyObject accept(ThreadContext context) {
        Ruby runtime = context.runtime;
        RubyTCPSocket socket = new RubyTCPSocket(runtime, runtime.getClass("TCPSocket"));

        try {
            RubyThread thread = context.getThread();

            while (true) {
                boolean ready = thread.select(this, SelectionKey.OP_ACCEPT);

                if (!ready) {
                    // we were woken up without being selected...poll for thread events and go back to sleep
                    context.pollThreadEvents();
View Full Code Here

TOP

Related Classes of org.jruby.RubyThread$Unblocker

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.