From 42263e979ec6fae53f6c9577dc7680bd6e5c4672 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Thu, 20 Jul 2017 02:41:57 +0200 Subject: [PATCH] synchronize interval access from other classes --- .../plugins/Treatments/TreatmentsPlugin.java | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java index 6a7f8c0873..8a999cbcd6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Treatments/TreatmentsPlugin.java @@ -188,11 +188,13 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { } if (!MainApp.getConfigBuilder().isFakingTempsByExtendedBoluses()) - for (Integer pos = 0; pos < extendedBoluses.size(); pos++) { - ExtendedBolus e = extendedBoluses.get(pos); - if (e.date > time) continue; - IobTotal calc = e.iobCalc(time); - total.plus(calc); + synchronized (extendedBoluses) { + for (Integer pos = 0; pos < extendedBoluses.size(); pos++) { + ExtendedBolus e = extendedBoluses.get(pos); + if (e.date > time) continue; + IobTotal calc = e.iobCalc(time); + total.plus(calc); + } } return total; } @@ -292,20 +294,24 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { @Override public IobTotal getCalculationToTimeTempBasals(long time) { IobTotal total = new IobTotal(time); - for (Integer pos = 0; pos < tempBasals.size(); pos++) { - TemporaryBasal t = tempBasals.get(pos); - if (t.date > time) continue; - IobTotal calc = t.iobCalc(time); - //log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob); - total.plus(calc); + synchronized (tempBasals) { + for (Integer pos = 0; pos < tempBasals.size(); pos++) { + TemporaryBasal t = tempBasals.get(pos); + if (t.date > time) continue; + IobTotal calc = t.iobCalc(time); + //log.debug("BasalIOB " + new Date(time) + " >>> " + calc.basaliob); + total.plus(calc); + } } if (MainApp.getConfigBuilder().isFakingTempsByExtendedBoluses()) { IobTotal totalExt = new IobTotal(time); - for (Integer pos = 0; pos < extendedBoluses.size(); pos++) { - ExtendedBolus e = extendedBoluses.get(pos); - if (e.date > time) continue; - IobTotal calc = e.iobCalc(time); - totalExt.plus(calc); + synchronized (extendedBoluses) { + for (Integer pos = 0; pos < extendedBoluses.size(); pos++) { + ExtendedBolus e = extendedBoluses.get(pos); + if (e.date > time) continue; + IobTotal calc = e.iobCalc(time); + totalExt.plus(calc); + } } // Convert to basal iob totalExt.basaliob = totalExt.iob; @@ -420,12 +426,18 @@ public class TreatmentsPlugin implements PluginBase, TreatmentsInterface { @Override public long oldestDataAvailable() { long oldestTime = System.currentTimeMillis(); - if (tempBasals.size() > 0) - oldestTime = Math.min(oldestTime, tempBasals.get(0).date); - if (extendedBoluses.size() > 0) - oldestTime = Math.min(oldestTime, extendedBoluses.get(0).date); - if (treatments.size() > 0) - oldestTime = Math.min(oldestTime, treatments.get(treatments.size() - 1).date); + synchronized (tempBasals) { + if (tempBasals.size() > 0) + oldestTime = Math.min(oldestTime, tempBasals.get(0).date); + } + synchronized (extendedBoluses) { + if (extendedBoluses.size() > 0) + oldestTime = Math.min(oldestTime, extendedBoluses.get(0).date); + } + synchronized (treatments) { + if (treatments.size() > 0) + oldestTime = Math.min(oldestTime, treatments.get(treatments.size() - 1).date); + } oldestTime -= 15 * 60 * 1000L; // allow 15 min before return oldestTime; }