X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;ds=inline;f=src%2Fjalview%2Fworkers%2FAlignCalcManager.java;h=d94afb744dcd62cfc512a1b39189c87685616e48;hb=8eb35a7bab22595255245c5136dd780816b98f26;hp=ef627fa6facf5462a9aa4a30263e186a69683e98;hpb=61df2ddd377ad0171a6d203fb9db2e1fa8ba5be6;p=jalview.git
diff --git a/src/jalview/workers/AlignCalcManager.java b/src/jalview/workers/AlignCalcManager.java
index ef627fa..d94afb7 100644
--- a/src/jalview/workers/AlignCalcManager.java
+++ b/src/jalview/workers/AlignCalcManager.java
@@ -1,49 +1,129 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
+ * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with Jalview. If not, see .
+ */
package jalview.workers;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import jalview.api.AlignCalcManagerI;
import jalview.api.AlignCalcWorkerI;
+import jalview.datamodel.AlignmentAnnotation;
public class AlignCalcManager implements AlignCalcManagerI
{
- private List blackList = new ArrayList();
+ private volatile List restartable = Collections
+ .synchronizedList(new ArrayList());
+
+ private volatile List blackList = Collections
+ .synchronizedList(new ArrayList());
/**
* global record of calculations in progress
*/
- private static Hashtable inProgress = new Hashtable();
+ private volatile Map inProgress = Collections
+ .synchronizedMap(new Hashtable());
/**
* record of calculations pending or in progress in the current context
*/
- private Map> updating = new Hashtable>();
+ private volatile Map> updating = Collections
+ .synchronizedMap(new Hashtable>());
@Override
public void notifyStart(AlignCalcWorkerI worker)
{
- List upd = updating.get(worker.getClass());
- if (upd == null)
+ synchronized (updating)
{
- updating.put(worker.getClass(),
- upd = new ArrayList());
+ List upd = updating.get(worker.getClass());
+ if (upd == null)
+ {
+ updating.put(
+ worker.getClass(),
+ upd = Collections
+ .synchronizedList(new ArrayList()));
+ }
+ synchronized (upd)
+ {
+ upd.add(worker);
+ }
}
- // upd.add(worker);
}
@Override
- public synchronized boolean alreadyDoing(AlignCalcWorkerI worker)
+ public boolean alreadyDoing(AlignCalcWorkerI worker)
{
- return inProgress.containsKey(worker.getClass());
+ synchronized (inProgress)
+ {
+ return inProgress.containsKey(worker.getClass());
+ }
}
+ /*
+ * (non-Javadoc)
+ *
+ * @see jalview.api.AlignCalcManagerI#isPending(jalview.api.AlignCalcWorkerI)
+ */
@Override
- public synchronized boolean notifyWorking(AlignCalcWorkerI worker)
+ public boolean isPending(AlignCalcWorkerI workingClass)
{
- // synchronized (inProgress)
+ List upd;
+ synchronized (updating)
+ {
+ upd = updating.get(workingClass.getClass());
+ if (upd == null)
+ {
+ return false;
+ }
+ synchronized (upd)
+ {
+ if (upd.size() > 1)
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
+ // TODO make into api method if needed ?
+ public int numberLive(AlignCalcWorkerI worker)
+ {
+ synchronized (updating)
+ {
+ List upd = updating.get(worker.getClass());
+ if (upd == null)
+ {
+ return 0;
+ }
+ ;
+ return upd.size();
+ }
+ }
+
+ @Override
+ public boolean notifyWorking(AlignCalcWorkerI worker)
+ {
+ synchronized (inProgress)
{
// TODO: decide if we should throw exceptions here if multiple workers
// start to work
@@ -62,45 +142,239 @@ public class AlignCalcManager implements AlignCalcManagerI
return true;
}
+ private final HashSet canUpdate = new HashSet();
+
@Override
- public synchronized void workerComplete(AlignCalcWorkerI worker)
+ public void workerComplete(AlignCalcWorkerI worker)
{
- inProgress.remove(worker.getClass());
- List upd = updating.get(worker.getClass());
- if (upd != null)
+ synchronized (inProgress)
{
- upd.remove(worker);
+ // System.err.println("Worker "+worker.getClass()+" marked as complete.");
+ inProgress.remove(worker.getClass());
+ List upd = updating.get(worker.getClass());
+ if (upd != null)
+ {
+ synchronized (upd)
+ {
+ upd.remove(worker);
+ }
+ canUpdate.add(worker);
+ }
}
-
}
@Override
public void workerCannotRun(AlignCalcWorkerI worker)
{
- blackList.add(worker.getClass());
+ synchronized (blackList)
+ {
+ blackList.add(worker.getClass());
+ }
}
public boolean isBlackListed(Class workerType)
{
- return blackList.contains(workerType);
+ synchronized (blackList)
+ {
+ return blackList.contains(workerType);
+ }
}
@Override
public void startWorker(AlignCalcWorkerI worker)
{
- new Thread(worker).start();
+ // System.err.println("Starting "+worker.getClass());
+ // new Exception("").printStackTrace();
+ Thread tw = new Thread(worker);
+ tw.setName(worker.getClass().toString());
+ tw.start();
}
@Override
- public synchronized boolean isWorking(AlignCalcWorkerI worker)
+ public boolean isWorking(AlignCalcWorkerI worker)
{
-// System.err.println("isWorking : worker "+(worker!=null ? worker.getClass():"null")+ " "+hashCode());
- return worker!=null && inProgress.get(worker.getClass()) == worker;
+ synchronized (inProgress)
+ {// System.err.println("isWorking : worker "+(worker!=null ?
+ // worker.getClass():"null")+ " "+hashCode());
+ return worker != null && inProgress.get(worker.getClass()) == worker;
+ }
}
+
@Override
public boolean isWorking()
{
-// System.err.println("isWorking "+hashCode());
- return inProgress.size()>0;
+ synchronized (inProgress)
+ {
+ // System.err.println("isWorking "+hashCode());
+ return inProgress.size() > 0;
+ }
+ }
+
+ @Override
+ public void registerWorker(AlignCalcWorkerI worker)
+ {
+ synchronized (restartable)
+ {
+ if (!restartable.contains(worker))
+ {
+ restartable.add(worker);
+ }
+ startWorker(worker);
+ }
+ }
+
+ @Override
+ public void restartWorkers()
+ {
+ synchronized (restartable)
+ {
+ for (AlignCalcWorkerI worker : restartable)
+ {
+ startWorker(worker);
+ }
+ }
+ }
+
+ @Override
+ public boolean workingInvolvedWith(AlignmentAnnotation alignmentAnnotation)
+ {
+ synchronized (inProgress)
+ {
+ for (AlignCalcWorkerI worker : inProgress.values())
+ {
+ if (worker.involves(alignmentAnnotation))
+ {
+ return true;
+ }
+ }
+ }
+ synchronized (updating)
+ {
+ for (List workers : updating.values())
+ {
+ for (AlignCalcWorkerI worker : workers)
+ if (worker.involves(alignmentAnnotation))
+ {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public void updateAnnotationFor(Class workerClass)
+ {
+
+ AlignCalcWorkerI[] workers;
+ synchronized (canUpdate)
+ {
+ workers = canUpdate.toArray(new AlignCalcWorkerI[0]);
+ }
+ for (AlignCalcWorkerI worker : workers)
+ {
+ if (workerClass.equals(worker.getClass()))
+ {
+ worker.updateAnnotation();
+ }
+ }
+ }
+
+ @Override
+ public List getRegisteredWorkersOfClass(
+ Class workerClass)
+ {
+ List workingClass = new ArrayList();
+ AlignCalcWorkerI[] workers;
+ synchronized (canUpdate)
+ {
+ workers = canUpdate.toArray(new AlignCalcWorkerI[0]);
+ }
+ for (AlignCalcWorkerI worker : workers)
+ {
+ if (workerClass.equals(worker.getClass()))
+ {
+ workingClass.add(worker);
+ }
+ }
+ return (workingClass.size() == 0) ? null : workingClass;
+ }
+
+ @Override
+ public boolean startRegisteredWorkersOfClass(Class workerClass)
+ {
+ List workers = getRegisteredWorkersOfClass(workerClass);
+ if (workers == null)
+ {
+ return false;
+ }
+ for (AlignCalcWorkerI worker : workers)
+ {
+ if (!isPending(worker))
+ {
+ startWorker(worker);
+ }
+ else
+ {
+ System.err.println("Pending exists for " + workerClass);
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public void workerMayRun(AlignCalcWorkerI worker)
+ {
+ synchronized (blackList)
+ {
+ if (blackList.contains(worker.getClass()))
+ {
+ blackList.remove(worker.getClass());
+ }
+ }
+ }
+
+ @Override
+ public void removeRegisteredWorkersOfClass(Class typeToRemove)
+ {
+ List workers = getRegisteredWorkersOfClass(typeToRemove);
+ List removable = new ArrayList();
+ Set toremovannot = new HashSet();
+ synchronized (restartable)
+ {
+ for (AlignCalcWorkerI worker : restartable)
+ {
+ if (typeToRemove.equals(worker.getClass()))
+ {
+ removable.add(worker);
+ toremovannot.add(worker);
+ }
+ }
+ restartable.removeAll(removable);
+ }
+ synchronized (canUpdate)
+ {
+ for (AlignCalcWorkerI worker : canUpdate)
+ {
+ if (typeToRemove.equals(worker.getClass()))
+ {
+ removable.add(worker);
+ toremovannot.add(worker);
+ }
+ }
+ canUpdate.removeAll(removable);
+ }
+ // TODO: finish testing this extension
+
+ /*
+ * synchronized (inProgress) { // need to kill or mark as dead any running
+ * threads... (inProgress.get(typeToRemove)); }
+ *
+ * if (workers == null) { return; } for (AlignCalcWorkerI worker : workers)
+ * {
+ *
+ * if (isPending(worker)) { worker.abortAndDestroy(); startWorker(worker); }
+ * else { System.err.println("Pending exists for " + workerClass); } }
+ */
}
}