ctf: Fix race condition in ctfiterator
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 25 Jun 2013 21:51:40 +0000 (17:51 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 26 Jun 2013 13:23:32 +0000 (09:23 -0400)
You could advance an event while seeking, that is no longer possible
performance droped from 78000 events/s reading to 75000 events/s.
Potential hazard, threads can access the iterator and advance and seek
concurently. This is allowed by design, it's an iterator, it should be
wrapped in a safer candy coated shell so it doesn't melt in you hands.

Change-Id: I4df50389728f075f15312107f44847d61343366e
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/14060
Tested-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
IP-Clean: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Tested-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java

index befd10461cd188bd7bd09a130d82a3934623a1d4..8311e7acb8fc351abd3183feab786cb491110e6f 100644 (file)
@@ -122,7 +122,7 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext,
      * @return boolean
      * @since 2.0
      */
-    public boolean seek(final CtfLocationInfo ctfLocationData) {
+    public synchronized boolean seek(final CtfLocationInfo ctfLocationData) {
         boolean ret = false;
 
         /* Adjust the timestamp depending on the trace's offset */
@@ -139,11 +139,12 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext,
          * assign the location index correctly
          */
         long index = 0;
-        if (this.getCurrentEvent() != null) {
-            currTimestamp = this.getCurrentEvent().getTimestamp().getValue();
+        final CtfTmfEvent currentEvent = this.getCurrentEvent();
+        if (currentEvent != null) {
+            currTimestamp = currentEvent.getTimestamp().getValue();
 
             for (long i = 0; i < ctfLocationData.getIndex(); i++) {
-                if (currTimestamp == this.getCurrentEvent().getTimestamp().getValue()) {
+                if (currTimestamp == currentEvent.getTimestamp().getValue()) {
                     index++;
                 } else {
                     index = 0;
@@ -246,7 +247,7 @@ public class CtfIterator extends CTFTraceReader implements ITmfContext,
      * @return boolean successful or not
      */
     @Override
-    public boolean advance() {
+    public synchronized boolean advance() {
         long index = curLocation.getLocationInfo().getIndex();
         long timestamp = curLocation.getLocationInfo().getTimestamp();
         boolean ret = super.advance();
This page took 0.02842 seconds and 5 git commands to generate.