Search in sources :

Example 1 with Pair

use of co.paralleluniverse.common.util.Pair in project quasar by puniverse.

the class ReceivePortGroup method setupSelector.

private void setupSelector() {
    // Freeze for this call
    final Pair<Selector<M>, Map<? extends ReceivePort<? extends M>, State>> curr = selector.get();
    final Map<? extends ReceivePort<? extends M>, State> currStates = curr != null ? curr.getSecond() : states.get();
    final Map<? extends ReceivePort<? extends M>, State> newStates = states.get();
    if (currStates == newStates && curr != null && curr.getFirst() != null) {
        // State has not changed, just reset the selector
        curr.getFirst().reset();
    } else {
        // State has chnaged, update the selector
        final Set<? extends ReceivePort<? extends M>> newPorts = newStates.keySet();
        // Build a new selector containing receive actions for all non-paused ports
        final List<SelectAction<M>> mutedActions = new ArrayList<>(newPorts.size());
        final List<SelectAction<M>> enabledActions = new ArrayList<>(newPorts.size());
        for (final ReceivePort<? extends M> port : newPorts) {
            if (!isPaused(port, newStates)) {
                if (isMuted(port, newStates))
                    mutedActions.add(Selector.receive(port));
                else
                    enabledActions.add(Selector.receive(port));
            }
        }
        final List<SelectAction<M>> actions = new ArrayList<>(newPorts.size());
        // Always receive change pings
        actions.add(Selector.receive(changedCh));
        actions.addAll(mutedActions);
        actions.addAll(enabledActions);
        // Priority to change signal, then to muted so they get elimintated first, then normal ones
        selector.set(new Pair(new Selector<>(true, actions), newStates));
    }
}
Also used : ArrayList(java.util.ArrayList) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) Pair(co.paralleluniverse.common.util.Pair)

Example 2 with Pair

use of co.paralleluniverse.common.util.Pair in project quasar by puniverse.

the class SuspendableHelper method isCallSiteInstrumented.

public static Pair<Boolean, Instrumented> isCallSiteInstrumented(/*Executable*/
Member m, int sourceLine, int bci, ExtendedStackTraceElement[] stes, int currentSteIdx) {
    if (m == null)
        return new Pair<>(false, null);
    if (isSyntheticAndNotLambda(m))
        return new Pair<>(true, null);
    final ExtendedStackTraceElement calleeSte = currentSteIdx - 1 >= 0 ? stes[currentSteIdx - 1] : null;
    if (calleeSte != null && // `verifySuspend` and `popMethod` calls are not suspendable call sites, not verifying them.
    ((calleeSte.getClassName().equals(Fiber.class.getName()) && calleeSte.getMethodName().equals("verifySuspend")) || (calleeSte.getClassName().equals(Stack.class.getName()) && calleeSte.getMethodName().equals("popMethod")))) {
        return new Pair<>(true, null);
    } else {
        final Instrumented i = getAnnotation(m, Instrumented.class);
        if (i == null)
            return new Pair<>(false, i);
        if (calleeSte != null && i.suspendableCallSiteNames() != null) {
            // check by callsite name (fails for bootstrapped lambdas)
            final Member callee = calleeSte.getMethod();
            if (callee == null) {
                final String methodName = "." + calleeSte.getMethodName() + "(";
                for (String callsite : i.suspendableCallSiteNames()) {
                    if (callsite.contains(methodName)) {
                        return new Pair(true, i);
                    }
                }
            } else {
                final String nameAndDescSuffix = "." + callee.getName() + ASMUtil.getDescriptor(callee);
                final String[] callsites = i.suspendableCallSiteNames();
                for (String callsite : callsites) {
                    if (callsite.endsWith(nameAndDescSuffix)) {
                        Class<?> callsiteOwner = null;
                        try {
                            callsiteOwner = Class.forName(getCallsiteOwner(callsite));
                        } catch (ClassNotFoundException e) {
                        }
                        if (callsiteOwner != null) {
                            final Class<?> owner = callee.getDeclaringClass();
                            if (declareInCommonAncestor(nameAndDescSuffix, owner, callsiteOwner)) {
                                return new Pair(true, i);
                            }
                        }
                    }
                }
            }
        }
        if (bci >= 0) {
            // check by bci; may be brittle
            final int[] scs = i.suspendableCallSitesOffsetsAfterInstr();
            for (int j : scs) {
                if (j == bci)
                    return new Pair<>(true, i);
            }
        } else if (sourceLine >= 0) {
            // check by source line
            final int[] scs = i.suspendableCallSites();
            for (int j : scs) {
                if (j == sourceLine)
                    return new Pair<>(true, i);
            }
        }
        return new Pair<>(false, i);
    }
}
Also used : Instrumented(co.paralleluniverse.fibers.Instrumented) Member(java.lang.reflect.Member) ExtendedStackTraceElement(co.paralleluniverse.common.util.ExtendedStackTraceElement) Pair(co.paralleluniverse.common.util.Pair)

Example 3 with Pair

use of co.paralleluniverse.common.util.Pair in project quasar by puniverse.

the class Pipeline method parallelTransfer.

private void parallelTransfer() throws SuspendExecution, InterruptedException {
    // 1) Fire workers
    for (int i = 0; i < parallelism; i++) {
        strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

            @Override
            public void run() throws SuspendExecution, InterruptedException {
                // Get first job
                Pair<S, Channel<Channel<T>>> job = jobs.receive();
                while (job != null) {
                    // Build result channel
                    final Channel<T> res = resultChannelBuilder.run();
                    // Process
                    transformer.call(job.getFirst(), res);
                    final Channel<Channel<T>> resWrapper = job.getSecond();
                    // Send result asynchronously
                    strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

                        @Override
                        public void run() throws SuspendExecution, InterruptedException {
                            resWrapper.send(res);
                        }
                    })).start();
                    // Get next job
                    job = jobs.receive();
                }
                // No more jobs, close results channel and quit worker
                results.close();
            }
        })).start();
    }
    // 2) Send jobs asynchronously
    strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

        @Override
        public void run() throws SuspendExecution, InterruptedException {
            // Get first input
            S s = from.receive();
            while (s != null) {
                final Channel<Channel<T>> resultWrapper = Channels.newChannel(1, Channels.OverflowPolicy.BLOCK, true, true);
                jobs.send(new Pair<>(s, resultWrapper));
                results.send(resultWrapper);
                // Get next input
                s = from.receive();
            }
            // No more inputs, close jobs channel and quit
            jobs.close();
        }
    })).start();
    // 3) Collect and transfer results asynchronously
    try {
        final Strand collector = strandFactory.newStrand(SuspendableUtils.runnableToCallable(new SuspendableRunnable() {

            @Override
            public void run() throws SuspendExecution, InterruptedException {
                // Get first result
                Channel<Channel<T>> resWrapper = results.receive();
                while (resWrapper != null) {
                    // Get wrapper
                    Channel<T> res = resWrapper.receive();
                    // Get first actual result
                    T out = res.receive();
                    while (out != null) {
                        // Send to output channel
                        to.send(out);
                        // Increment counter
                        transferred.incrementAndGet();
                        // Get next result
                        out = res.receive();
                    }
                    resWrapper = results.receive();
                }
            // No more results, quit
            }
        })).start();
        // TODO solve nasty instrumentation problems on Strand.join()
        if (collector.isFiber()) {
            Fiber f = (Fiber) collector.getUnderlying();
            f.join();
        } else
            collector.join();
    } catch (ExecutionException ee) {
        throw new AssertionError(ee);
    }
}
Also used : SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) SuspendableRunnable(co.paralleluniverse.strands.SuspendableRunnable) Channel(co.paralleluniverse.strands.channels.Channel) Fiber(co.paralleluniverse.fibers.Fiber) Strand(co.paralleluniverse.strands.Strand) ExecutionException(java.util.concurrent.ExecutionException) Pair(co.paralleluniverse.common.util.Pair)

Example 4 with Pair

use of co.paralleluniverse.common.util.Pair in project quasar by puniverse.

the class ProxyServerActor method getProxyClass.

private static Class<? extends Server> getProxyClass(Class<?>[] interfaces, boolean callOnVoidMethods) {
    final Pair<Set<Class<?>>, Boolean> key = new Pair(ImmutableSet.copyOf(interfaces), callOnVoidMethods);
    Class<? extends Server> clazz = classes.get(key);
    if (clazz == null) {
        clazz = // http://bytebuddy.net/
        new ByteBuddy().subclass(Server.class).implement(interfaces).implement(java.io.Serializable.class).method(isDeclaredBy(anyOf(interfaces))).intercept(InvocationHandlerAdapter.of(callOnVoidMethods ? handler1 : handler2)).make().load(ProxyServerActor.class.getClassLoader(), ClassLoadingStrategy.Default.WRAPPER).getLoaded();
        final Class<? extends Server> old = classes.putIfAbsent(key, clazz);
        if (old != null)
            clazz = old;
    }
    return clazz;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ByteBuddy(net.bytebuddy.ByteBuddy) Pair(co.paralleluniverse.common.util.Pair)

Aggregations

Pair (co.paralleluniverse.common.util.Pair)4 ExtendedStackTraceElement (co.paralleluniverse.common.util.ExtendedStackTraceElement)1 Fiber (co.paralleluniverse.fibers.Fiber)1 Instrumented (co.paralleluniverse.fibers.Instrumented)1 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 Strand (co.paralleluniverse.strands.Strand)1 SuspendableRunnable (co.paralleluniverse.strands.SuspendableRunnable)1 Channel (co.paralleluniverse.strands.channels.Channel)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Member (java.lang.reflect.Member)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 ByteBuddy (net.bytebuddy.ByteBuddy)1