import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
+import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.segmentstore.core.ISegment;
import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
import org.eclipse.tracecompass.segmentstore.core.SegmentComparators;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
private long fSize;
+ private @Nullable transient Iterable<T> fLastSnapshot = null;
+
/**
* Constructor
*/
fSize = 0;
}
- /**
- * Warning, this is not thread safe, and can cause concurrent modification
- * exceptions
- */
@Override
public Iterator<T> iterator() {
- return checkNotNull(fStartTimesIndex.values().iterator());
+ fLock.readLock().lock();
+ try {
+ Iterable<T> lastSnapshot = fLastSnapshot;
+ if (lastSnapshot == null) {
+ lastSnapshot = checkNotNull(ImmutableList.copyOf(fStartTimesIndex.values()));
+ fLastSnapshot = lastSnapshot;
+ }
+ return checkNotNull(lastSnapshot.iterator());
+ } finally {
+ fLock.readLock().unlock();
+ }
}
@Override
if (fStartTimesIndex.put(Long.valueOf(val.getStart()), val)) {
fEndTimesIndex.put(Long.valueOf(val.getEnd()), val);
fSize++;
+ fLastSnapshot = null;
}
} finally {
fLock.writeLock().unlock();