From b8716616b088a6be8b783b1c7e043cbbf858e1a0 Mon Sep 17 00:00:00 2001 From: Nathan Baltzell Date: Tue, 16 May 2023 10:15:59 -0400 Subject: [PATCH] fix thread safety in swaps --- .../org/jlab/detector/swaps/SwapManager.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/common-tools/clas-detector/src/main/java/org/jlab/detector/swaps/SwapManager.java b/common-tools/clas-detector/src/main/java/org/jlab/detector/swaps/SwapManager.java index b5a9f4f2c..3eb35eb0d 100644 --- a/common-tools/clas-detector/src/main/java/org/jlab/detector/swaps/SwapManager.java +++ b/common-tools/clas-detector/src/main/java/org/jlab/detector/swaps/SwapManager.java @@ -46,7 +46,8 @@ public class SwapManager { "RTPC" }; - private final HashMap> swaps = new HashMap<>(); + private volatile HashMap> swaps = new HashMap<>(); + private final Map banksToTables = new HashMap<>(); private final Map> detsToBanks = new HashMap<>(); private final Map detsToTables = new HashMap<>(); @@ -189,12 +190,15 @@ public int[] get(DataEvent event, String bankName, int row) { * Initialize the swaps for a given run number. * @param run */ - private void add(int run) { - this.swaps.put(run,new HashMap<>()); - for (String tableName : this.banksToTables.values()) { - IndexedTable prev = prevConman.getConstants(run, tableName); - IndexedTable curr = currConman.getConstants(run, tableName); - this.swaps.get(run).put(tableName,new SwapTable(prev,curr)); + synchronized private void add(int run) { + if (!this.swaps.containsKey(run)) { + HashMap x = new HashMap<>(); + for (String tableName : this.banksToTables.values()) { + IndexedTable prev = prevConman.getConstants(run, tableName); + IndexedTable curr = currConman.getConstants(run, tableName); + x.put(tableName,new SwapTable(prev,curr)); + } + this.swaps.put(run, x); } }