ss: Add proper locking to the AttributeTree
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 2 Sep 2015 23:18:54 +0000 (19:18 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 3 Sep 2015 18:52:23 +0000 (14:52 -0400)
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 <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/55156
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/AttributeTree.java

index 330c6ad830e8830de99a86b31bf1ae0339d80cf1..3ac74c8218a241943d7587869384bdb70356db18 100644 (file)
@@ -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<Attribute>());
+        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<Integer> getSubAttributes(int attributeQuark, boolean recursive)
+    public synchronized @NonNull List<Integer> getSubAttributes(int attributeQuark, boolean recursive)
             throws AttributeNotFoundException {
         List<Integer> listOfChildren = new ArrayList<>();
         Attribute startingAttribute;
@@ -291,7 +290,7 @@ public final class AttributeTree {
      * @return Quark of the parent attribute or <code>-1</code> 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);
     }
 
This page took 0.027252 seconds and 5 git commands to generate.