CommandQueue synchronized

This commit is contained in:
Milos Kozak 2019-10-08 09:52:55 +02:00
parent 4b8395a373
commit 4d1fccaf01

View file

@ -101,17 +101,21 @@ public class CommandQueue {
} }
private synchronized void removeAll(Command.CommandType type) { private synchronized void removeAll(Command.CommandType type) {
synchronized (queue) {
for (int i = queue.size() - 1; i >= 0; i--) { for (int i = queue.size() - 1; i >= 0; i--) {
if (queue.get(i).commandType == type) { if (queue.get(i).commandType == type) {
queue.remove(i); queue.remove(i);
} }
} }
} }
}
private synchronized boolean isLastScheduled(Command.CommandType type) { private synchronized boolean isLastScheduled(Command.CommandType type) {
synchronized (queue) {
if (queue.size() > 0 && queue.get(queue.size() - 1).commandType == type) { if (queue.size() > 0 && queue.get(queue.size() - 1).commandType == type) {
return true; return true;
} }
}
return false; return false;
} }
@ -119,27 +123,34 @@ public class CommandQueue {
// inject as a first command // inject as a first command
if (L.isEnabled(L.PUMPQUEUE)) if (L.isEnabled(L.PUMPQUEUE))
log.debug("Adding as first: " + command.getClass().getSimpleName() + " - " + command.status()); log.debug("Adding as first: " + command.getClass().getSimpleName() + " - " + command.status());
synchronized (queue) {
queue.addFirst(command); queue.addFirst(command);
} }
}
private synchronized void add(Command command) { private synchronized void add(Command command) {
if (L.isEnabled(L.PUMPQUEUE)) if (L.isEnabled(L.PUMPQUEUE))
log.debug("Adding: " + command.getClass().getSimpleName() + " - " + command.status()); log.debug("Adding: " + command.getClass().getSimpleName() + " - " + command.status());
synchronized (queue) {
queue.add(command); queue.add(command);
} }
}
synchronized void pickup() { synchronized void pickup() {
synchronized (queue) {
performing = queue.poll(); performing = queue.poll();
} }
}
synchronized void clear() { synchronized void clear() {
performing = null; performing = null;
synchronized (queue) {
for (int i = 0; i < queue.size(); i++) { for (int i = 0; i < queue.size(); i++) {
queue.get(i).cancel(); queue.get(i).cancel();
} }
queue.clear(); queue.clear();
} }
}
public int size() { public int size() {
return queue.size(); return queue.size();
@ -181,11 +192,13 @@ public class CommandQueue {
public synchronized boolean bolusInQueue() { public synchronized boolean bolusInQueue() {
if (isRunning(Command.CommandType.BOLUS)) return true; if (isRunning(Command.CommandType.BOLUS)) return true;
synchronized (queue) {
for (int i = 0; i < queue.size(); i++) { for (int i = 0; i < queue.size(); i++) {
if (queue.get(i).commandType == Command.CommandType.BOLUS) { if (queue.get(i).commandType == Command.CommandType.BOLUS) {
return true; return true;
} }
} }
}
return false; return false;
} }
@ -440,11 +453,13 @@ public class CommandQueue {
public synchronized boolean statusInQueue() { public synchronized boolean statusInQueue() {
if (isRunning(Command.CommandType.READSTATUS)) if (isRunning(Command.CommandType.READSTATUS))
return true; return true;
synchronized (queue) {
for (int i = 0; i < queue.size(); i++) { for (int i = 0; i < queue.size(); i++) {
if (queue.get(i).commandType == Command.CommandType.READSTATUS) { if (queue.get(i).commandType == Command.CommandType.READSTATUS) {
return true; return true;
} }
} }
}
return false; return false;
} }
@ -528,16 +543,19 @@ public class CommandQueue {
public Spanned spannedStatus() { public Spanned spannedStatus() {
String s = ""; String s = "";
int line = 0; int line = 0;
if (performing != null) { Command perf = performing;
s += "<b>" + performing.status() + "</b>"; if (perf != null) {
s += "<b>" + perf.status() + "</b>";
line++; line++;
} }
synchronized (queue) {
for (int i = 0; i < queue.size(); i++) { for (int i = 0; i < queue.size(); i++) {
if (line != 0) if (line != 0)
s += "<br>"; s += "<br>";
s += queue.get(i).status(); s += queue.get(i).status();
line++; line++;
} }
}
return Html.fromHtml(s); return Html.fromHtml(s);
} }