Skip to content
Browse files

Revert "integrate a part of Alex' change that fixes a memleak (change…

… was because of unrelated problem)"

This reverts commit 0dfb85a.
  • Loading branch information...
1 parent 8ce8c47 commit fada0290f047bc0e50df3e4109adc8fe2e319d2b Mihail Muhin committed Dec 13, 2010
Showing with 21 additions and 6 deletions.
  1. +21 −6 core/kernel/source/jetbrains/mps/smodel/BaseSModelDescriptor.java
View
27 core/kernel/source/jetbrains/mps/smodel/BaseSModelDescriptor.java
@@ -27,8 +27,6 @@
import org.jetbrains.annotations.Nullable;
import java.util.*;
-import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.CopyOnWriteArraySet;
public abstract class BaseSModelDescriptor implements SModelDescriptor {
private static final Logger LOG = Logger.getLogger(BaseSModelDescriptor.class);
@@ -42,8 +40,10 @@
//it should be possible to add listeners from any thread so we use lock here
//access to other fields is synchronized with ModelAccess
private final Object myListenersLock = new Object();
- private Set<SModelListener> myModelListeners = new CopyOnWriteArraySet<SModelListener>();
- private Set<SModelCommandListener> myModelCommandListeners = new CopyOnWriteArraySet<SModelCommandListener>();
+ private Set<SModelListener> myModelListeners = new HashSet<SModelListener>(0);
+ private SModelListener[] myModelListenersCopy;
+ private Set<SModelCommandListener> myModelCommandListeners = new LinkedHashSet<SModelCommandListener>(0);
+ private SModelCommandListener[] myModelCommandListenersCopy;
public BaseSModelDescriptor(IModelRootManager manager, IFile modelFile, @NotNull SModelReference modelReference) {
myModelReference = modelReference;
@@ -224,6 +224,7 @@ public void dispose() {
public void addModelListener(@NotNull SModelListener listener) {
synchronized (myListenersLock) {
myModelListeners.add(listener);
+ myModelListenersCopy = null;
}
}
@@ -236,12 +237,19 @@ public boolean hasModelListener(@NotNull SModelListener listener) {
public void removeModelListener(@NotNull SModelListener listener) {
synchronized (myListenersLock) {
myModelListeners.remove(listener);
+ myModelListenersCopy = null;
}
}
@NotNull
public SModelListener[] getModelListeners() {
- return myModelListeners.toArray(new SModelListener[myModelListeners.size()]);
+ synchronized (myListenersLock) {
+ if (myModelListenersCopy == null) {
+ myModelListenersCopy = myModelListeners.toArray(new SModelListener[myModelListeners.size()]);
+ Arrays.sort(myModelListenersCopy, SModelAdapter.COMPARATOR);
+ }
+ return myModelListenersCopy;
+ }
}
public boolean hasModelCommandListener(@NotNull SModelCommandListener listener) {
@@ -253,18 +261,25 @@ public boolean hasModelCommandListener(@NotNull SModelCommandListener listener)
public void addModelCommandListener(@NotNull SModelCommandListener listener) {
synchronized (myListenersLock) {
myModelCommandListeners.add(listener);
+ myModelCommandListenersCopy = null;
}
}
public void removeModelCommandListener(@NotNull SModelCommandListener listener) {
synchronized (myListenersLock) {
myModelCommandListeners.remove(listener);
+ myModelCommandListenersCopy = null;
}
}
@NotNull
public SModelCommandListener[] getModelCommandListeners() {
- return myModelCommandListeners.toArray(new SModelCommandListener[myModelCommandListeners.size()]);
+ synchronized (myListenersLock) {
+ if (myModelCommandListenersCopy == null) {
+ myModelCommandListenersCopy = myModelCommandListeners.toArray(new SModelCommandListener[myModelCommandListeners.size()]);
+ }
+ return myModelCommandListenersCopy;
+ }
}
private void clearListeners() {

0 comments on commit fada029

Please sign in to comment.
Something went wrong with that request. Please try again.