X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.simulator.variable%2Fsrc%2Forg%2Fsimantics%2Fsimulator%2Fvariable%2Fimpl%2FSimpleRealm.java;fp=bundles%2Forg.simantics.simulator.variable%2Fsrc%2Forg%2Fsimantics%2Fsimulator%2Fvariable%2Fimpl%2FSimpleRealm.java;h=0aa1ff3b834e65221cd4eb3951f3bee628647980;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=2f29037dd3df1bfe888996de2316ce5b4b49e03b;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/impl/SimpleRealm.java b/bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/impl/SimpleRealm.java index 2f29037dd..0aa1ff3b8 100644 --- a/bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/impl/SimpleRealm.java +++ b/bundles/org.simantics.simulator.variable/src/org/simantics/simulator/variable/impl/SimpleRealm.java @@ -1,62 +1,62 @@ -/******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - * Semantum Oy - initial API and implementation - *******************************************************************************/ -package org.simantics.simulator.variable.impl; - -import java.util.ArrayDeque; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Semaphore; -import java.util.concurrent.TimeUnit; - -import org.simantics.simulator.variable.Realm; - -public class SimpleRealm implements Realm { - - private ArrayDeque tasks = new ArrayDeque(); - private Semaphore taskCount = new Semaphore(0); - - public void executeTasks() { - while(taskCount.tryAcquire()) { - Runnable task; - synchronized (tasks) { - task = tasks.poll(); - } - task.run(); - } - } - - public void waitTasks(long waitDuration) throws InterruptedException { - taskCount.tryAcquire(waitDuration, TimeUnit.MILLISECONDS); - } - - @Override - public void syncExec(final Runnable runnable) throws InterruptedException { - final CountDownLatch latch = new CountDownLatch(1); - asyncExec(new Runnable() { - @Override - public void run() { - runnable.run(); - latch.countDown(); - } - }); - latch.await(); - } - - @Override - public void asyncExec(Runnable runnable) { - synchronized (tasks) { - tasks.add(runnable); - } - taskCount.release(); - } - -} +/******************************************************************************* + * Copyright (c) 2013 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + * Semantum Oy - initial API and implementation + *******************************************************************************/ +package org.simantics.simulator.variable.impl; + +import java.util.ArrayDeque; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Semaphore; +import java.util.concurrent.TimeUnit; + +import org.simantics.simulator.variable.Realm; + +public class SimpleRealm implements Realm { + + private ArrayDeque tasks = new ArrayDeque(); + private Semaphore taskCount = new Semaphore(0); + + public void executeTasks() { + while(taskCount.tryAcquire()) { + Runnable task; + synchronized (tasks) { + task = tasks.poll(); + } + task.run(); + } + } + + public void waitTasks(long waitDuration) throws InterruptedException { + taskCount.tryAcquire(waitDuration, TimeUnit.MILLISECONDS); + } + + @Override + public void syncExec(final Runnable runnable) throws InterruptedException { + final CountDownLatch latch = new CountDownLatch(1); + asyncExec(new Runnable() { + @Override + public void run() { + runnable.run(); + latch.countDown(); + } + }); + latch.await(); + } + + @Override + public void asyncExec(Runnable runnable) { + synchronized (tasks) { + tasks.add(runnable); + } + taskCount.release(); + } + +}