]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics / src / org / simantics / SleepingDatabaseJob.java
diff --git a/bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java b/bundles/org.simantics/src/org/simantics/SleepingDatabaseJob.java
new file mode 100644 (file)
index 0000000..879183d
--- /dev/null
@@ -0,0 +1,111 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 Association for Decentralized Information Management in\r
+ * 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
+ *     Semantum Oy - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics;\r
+\r
+import java.util.concurrent.Semaphore;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Status;\r
+import org.eclipse.core.runtime.jobs.Job;\r
+\r
+/**\r
+ * A dummy database-family job that can be used for signaling that a large\r
+ * database job is in progress although it is technically not running in a\r
+ * {@link Job}.\r
+ * \r
+ * <p>\r
+ * To start the job and wait for it to start, use\r
+ * {@link #scheduleAndWaitForRunning()}. To end the job and wait for it to die,\r
+ * use {@link #disposeAndJoin()}.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class SleepingDatabaseJob extends DatabaseJob {\r
+\r
+    private final Semaphore start = new Semaphore(0);\r
+    private final Semaphore end = new Semaphore(0);\r
+\r
+    public SleepingDatabaseJob(String name) {\r
+        super(name);\r
+    }\r
+\r
+    @Override\r
+    protected final IStatus run(IProgressMonitor monitor) {\r
+        start.release();\r
+        try {\r
+            return work(monitor);\r
+        } finally {\r
+            try {\r
+                end.acquire();\r
+            } catch (InterruptedException e) {\r
+                // Some other party wanted to kill the job. So be it.\r
+            }\r
+        }\r
+    }\r
+\r
+    protected IStatus work(IProgressMonitor monitor) {\r
+        return Status.OK_STATUS;\r
+    }\r
+\r
+    public SleepingDatabaseJob scheduleAndWaitForRunning() throws InterruptedException {\r
+        schedule();\r
+        start.acquire();\r
+        start.release();\r
+        return this;\r
+    }\r
+\r
+    public void scheduleAndWaitForRunningUninterruptibly() {\r
+        schedule();\r
+        start.acquireUninterruptibly();\r
+        start.release();\r
+    }\r
+\r
+    public void disposeAndJoin() throws InterruptedException {\r
+        end.release();\r
+        join();\r
+    }\r
+\r
+    /**\r
+     * @param name\r
+     * @param runnable\r
+     * @throws InterruptedException\r
+     */\r
+    public static void sleepWhile(String name, Runnable runnable) throws InterruptedException {\r
+        SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);\r
+        try {\r
+            dbjob.scheduleAndWaitForRunning();\r
+            runnable.run();\r
+        } finally {\r
+            dbjob.disposeAndJoin();\r
+        }\r
+    }\r
+\r
+    /**\r
+     * @param name\r
+     * @param runnable\r
+     * @throws InterruptedException\r
+     */\r
+    public static void sleepUninterruptiblyWhile(String name, Runnable runnable) {\r
+        SleepingDatabaseJob dbjob = new SleepingDatabaseJob(name);\r
+        try {\r
+            dbjob.scheduleAndWaitForRunningUninterruptibly();\r
+            runnable.run();\r
+        } finally {\r
+            try {\r
+                dbjob.disposeAndJoin();\r
+            } catch (InterruptedException e) {\r
+            }\r
+        }\r
+    }\r
+\r
+}\r