Search in sources :

Example 1 with BProgramException

use of il.ac.bgu.cs.bp.bpjs.exceptions.BProgramException in project BPjs by bThink-BGU.

the class BProgramSyncSnapshot method handleInterrupts.

private void handleInterrupts(BEvent anEvent, Iterable<BProgramRunnerListener> listeners, BProgram bprog, Context ctxt) {
    Set<BThreadSyncSnapshot> interrupted = threadSnapshots.stream().filter(bt -> bt.getBSyncStatement().getInterrupt().contains(anEvent)).collect(toSet());
    if (!interrupted.isEmpty()) {
        threadSnapshots.removeAll(interrupted);
        interrupted.forEach(bt -> {
            listeners.forEach(l -> l.bthreadRemoved(bprog, bt));
            bt.getInterrupt().ifPresent(func -> {
                final Scriptable scope = bt.getScope();
                // can't call bsync from a break handler.
                scope.delete("bsync");
                try {
                    ctxt.callFunctionWithContinuations(func, scope, new Object[] { anEvent });
                } catch (ContinuationPending ise) {
                    throw new BProgramException("Cannot call bsync from a break-upon handler. Please consider pushing an external event.");
                }
            });
        });
    }
}
Also used : ResumeBThread(il.ac.bgu.cs.bp.bpjs.execution.tasks.ResumeBThread) Context(org.mozilla.javascript.Context) BProgramException(il.ac.bgu.cs.bp.bpjs.exceptions.BProgramException) ContinuationPending(org.mozilla.javascript.ContinuationPending) Set(java.util.Set) Logger(java.util.logging.Logger) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Future(java.util.concurrent.Future) Collectors.toList(java.util.stream.Collectors.toList) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) StartBThread(il.ac.bgu.cs.bp.bpjs.execution.tasks.StartBThread) Scriptable(org.mozilla.javascript.Scriptable) BProgramRunnerListener(il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener) Collections(java.util.Collections) Collectors.toSet(java.util.stream.Collectors.toSet) BPEngineTask(il.ac.bgu.cs.bp.bpjs.execution.tasks.BPEngineTask) ExecutorService(java.util.concurrent.ExecutorService) ContinuationPending(org.mozilla.javascript.ContinuationPending) Scriptable(org.mozilla.javascript.Scriptable) BProgramException(il.ac.bgu.cs.bp.bpjs.exceptions.BProgramException)

Aggregations

BProgramException (il.ac.bgu.cs.bp.bpjs.exceptions.BProgramException)1 BProgramRunnerListener (il.ac.bgu.cs.bp.bpjs.execution.listeners.BProgramRunnerListener)1 BPEngineTask (il.ac.bgu.cs.bp.bpjs.execution.tasks.BPEngineTask)1 ResumeBThread (il.ac.bgu.cs.bp.bpjs.execution.tasks.ResumeBThread)1 StartBThread (il.ac.bgu.cs.bp.bpjs.execution.tasks.StartBThread)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1