--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ * Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.simulator.variable.impl;\r
+\r
+import java.util.ArrayDeque;\r
+import java.util.concurrent.CountDownLatch;\r
+import java.util.concurrent.Semaphore;\r
+import java.util.concurrent.TimeUnit;\r
+\r
+import org.simantics.simulator.variable.Realm;\r
+\r
+public class SimpleRealm implements Realm {\r
+\r
+ private ArrayDeque<Runnable> tasks = new ArrayDeque<Runnable>();\r
+ private Semaphore taskCount = new Semaphore(0);\r
+\r
+ public void executeTasks() { \r
+ while(taskCount.tryAcquire()) {\r
+ Runnable task;\r
+ synchronized (tasks) {\r
+ task = tasks.poll();\r
+ }\r
+ task.run();\r
+ }\r
+ }\r
+ \r
+ public void waitTasks(long waitDuration) throws InterruptedException {\r
+ taskCount.tryAcquire(waitDuration, TimeUnit.MILLISECONDS);\r
+ }\r
+ \r
+ @Override\r
+ public void syncExec(final Runnable runnable) throws InterruptedException {\r
+ final CountDownLatch latch = new CountDownLatch(1);\r
+ asyncExec(new Runnable() {\r
+ @Override\r
+ public void run() {\r
+ runnable.run();\r
+ latch.countDown();\r
+ } \r
+ });\r
+ latch.await();\r
+ }\r
+\r
+ @Override\r
+ public void asyncExec(Runnable runnable) {\r
+ synchronized (tasks) {\r
+ tasks.add(runnable); \r
+ }\r
+ taskCount.release();\r
+ }\r
+\r
+}\r