From: Alexandre Montplaisir Date: Wed, 2 Sep 2015 23:18:54 +0000 (-0400) Subject: ss: Add proper locking to the AttributeTree X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=a69a9003f89c33915a2cafaed4e1e78505081e68;p=deliverable%2Ftracecompass.git ss: Add proper locking to the AttributeTree Instead of using a Collections.synchronizedList, extend the locking the whole object. This makes sure that data stays consistent in the longer methods like getQuarkAndAdd()/getQuarkDontAdd(). Bug: 476474. Change-Id: Ia57f77ad109dd8c0b90e1824ba04d3a57140c1b6 Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/55156 Reviewed-by: Matthew Khouzam Reviewed-by: Hudson CI --- diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/AttributeTree.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/AttributeTree.java index 330c6ad830..3ac74c8218 100644 --- a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/AttributeTree.java +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/AttributeTree.java @@ -28,7 +28,6 @@ import java.io.PrintWriter; import java.nio.channels.FileChannel; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import org.eclipse.jdt.annotation.NonNull; @@ -59,7 +58,7 @@ public final class AttributeTree { */ public AttributeTree(StateSystem ss) { this.ss = ss; - this.attributeList = Collections.synchronizedList(new ArrayList()); + this.attributeList = new ArrayList<>(); this.attributeTreeRoot = new Attribute(null, "root", -1); //$NON-NLS-1$ } @@ -112,7 +111,7 @@ public final class AttributeTree { * @param pos * The position (in bytes) in the file where to write */ - public void writeSelf(File file, long pos) { + public synchronized void writeSelf(File file, long pos) { try (FileOutputStream fos = new FileOutputStream(file, true); FileChannel fc = fos.getChannel();) { fc.position(pos); @@ -141,7 +140,7 @@ public final class AttributeTree { * * @return The current number of attributes in the tree */ - public int getNbAttributes() { + public synchronized int getNbAttributes() { return attributeList.size(); } @@ -158,7 +157,7 @@ public final class AttributeTree { * @throws AttributeNotFoundException * If the specified path was not found */ - public int getQuarkDontAdd(int startingNodeQuark, String... subPath) + public synchronized int getQuarkDontAdd(int startingNodeQuark, String... subPath) throws AttributeNotFoundException { assert (startingNodeQuark >= -1); @@ -259,7 +258,7 @@ public final class AttributeTree { * If 'attributeQuark' is invalid, or if there is no attrbiute * associated to it. */ - public @NonNull List getSubAttributes(int attributeQuark, boolean recursive) + public synchronized @NonNull List getSubAttributes(int attributeQuark, boolean recursive) throws AttributeNotFoundException { List listOfChildren = new ArrayList<>(); Attribute startingAttribute; @@ -291,7 +290,7 @@ public final class AttributeTree { * @return Quark of the parent attribute or -1 for the root * attribute */ - public int getParentAttributeQuark(int quark) { + public synchronized int getParentAttributeQuark(int quark) { if (quark == -1) { return quark; } @@ -315,7 +314,7 @@ public final class AttributeTree { * The quark of the attribute * @return The (base) name of the attribute */ - public @NonNull String getAttributeName(int quark) { + public synchronized @NonNull String getAttributeName(int quark) { return attributeList.get(quark).getName(); } @@ -326,7 +325,7 @@ public final class AttributeTree { * The quark of the attribute * @return The full path name of the attribute */ - public @NonNull String getFullAttributeName(int quark) { + public synchronized @NonNull String getFullAttributeName(int quark) { return attributeList.get(quark).getFullAttributeName(); } @@ -338,7 +337,7 @@ public final class AttributeTree { * The quark of the attribute * @return The path elements of the full path */ - public @NonNull String[] getFullAttributePathArray(int quark) { + public synchronized @NonNull String[] getFullAttributePathArray(int quark) { return attributeList.get(quark).getFullAttribute(); } @@ -348,7 +347,7 @@ public final class AttributeTree { * @param writer * The writer where to print the output */ - public void debugPrint(PrintWriter writer) { + public synchronized void debugPrint(PrintWriter writer) { attributeTreeRoot.debugPrint(writer); }