[[nbpython-commits]] [hg] main/contrib: Fix 155236: Add document read locks around d...

  • From: Tor Norbye < >
  • To:
  • Subject: [[nbpython-commits]] [hg] main/contrib: Fix 155236: Add document read locks around d...
  • Date: Thu, 21 Oct 2010 08:34:27 -0700

changeset 87826656ad34 in main/contrib
details: http://hg.netbeans.org/main/contrib?cmd=changeset;node=87826656ad34
description:
        Fix 155236:  Add document read locks around document accesses

diffstat:

 gsf/src/org/netbeans/modules/editor/gsfret/InstantRenameAction.java          
                       |  13 +-
 gsf/src/org/netbeans/modules/gsfret/editor/fold/GsfFoldManager.java          
                       |   6 +-
 
python.editor/src/org/netbeans/modules/python/editor/PythonOccurrencesMarker.java
                   |   3 +
 
python.editor/src/org/netbeans/modules/python/editor/PythonStructureScanner.java
                    |  31 +++--
 
python.editor/src/org/netbeans/modules/python/editor/hints/PythonHintsProvider.java
                 |  57 +++++++--
 
python.editor/src/org/netbeans/modules/python/editor/refactoring/PythonRenameRefactoringPlugin.java
 |   8 +-
 
python.editor/src/org/netbeans/modules/python/editor/refactoring/WhereUsedElement.java
              |   6 +-
 7 files changed, 89 insertions(+), 35 deletions(-)

diffs (272 lines):

diff --git 
a/gsf/src/org/netbeans/modules/editor/gsfret/InstantRenameAction.java 
b/gsf/src/org/netbeans/modules/editor/gsfret/InstantRenameAction.java
--- a/gsf/src/org/netbeans/modules/editor/gsfret/InstantRenameAction.java
+++ b/gsf/src/org/netbeans/modules/editor/gsfret/InstantRenameAction.java
@@ -90,10 +90,19 @@
     }
 
     public void actionPerformed(ActionEvent evt, final JTextComponent 
target) {
+        String ident = null;
+        final int caret = target.getCaretPosition();
+        BaseDocument doc = Utilities.getDocument(target);
         try {
-            final int caret = target.getCaretPosition();
-            String ident = 
Utilities.getIdentifier(Utilities.getDocument(target), caret);
+            doc.readLock(); // For Utilities.getId
+            ident = Utilities.getIdentifier( doc, caret);
+        } catch (BadLocationException e) {
+            ErrorManager.getDefault().notify(e);
+        } finally {
+            doc.readUnlock();
+        }
 
+        try {
             if (ident == null) {
                 Utilities.setStatusBoldText(target, "Cannot perform rename 
here.");
 
diff --git 
a/gsf/src/org/netbeans/modules/gsfret/editor/fold/GsfFoldManager.java 
b/gsf/src/org/netbeans/modules/gsfret/editor/fold/GsfFoldManager.java
--- a/gsf/src/org/netbeans/modules/gsfret/editor/fold/GsfFoldManager.java
+++ b/gsf/src/org/netbeans/modules/gsfret/editor/fold/GsfFoldManager.java
@@ -394,14 +394,18 @@
                             }
                         }
 
+                        BaseDocument baseDoc = (BaseDocument) doc;
                         try {
+                            baseDoc.readLock();
                             // Start the fold at the END of the line
-                            startOffset = 
org.netbeans.editor.Utilities.getRowEnd((BaseDocument)doc, startOffset);
+                            startOffset = 
org.netbeans.editor.Utilities.getRowEnd( baseDoc, startOffset);
                             if (startOffset >= endOffset) {
                                 return true;
                             }
                         } catch (BadLocationException ex) {
                             ErrorManager.getDefault().notify(ex);
+                        } finally {
+                            baseDoc.readUnlock();
                         }
                         
                         folds.add(new FoldInfo(doc, startOffset, endOffset, 
INITIAL_COMMENT_FOLD_TEMPLATE, collapsed));
diff --git 
a/python.editor/src/org/netbeans/modules/python/editor/PythonOccurrencesMarker.java
 
b/python.editor/src/org/netbeans/modules/python/editor/PythonOccurrencesMarker.java
--- 
a/python.editor/src/org/netbeans/modules/python/editor/PythonOccurrencesMarker.java
+++ 
b/python.editor/src/org/netbeans/modules/python/editor/PythonOccurrencesMarker.java
@@ -296,6 +296,7 @@
     private Set<OffsetRange> findNameFromImport(int lexOffset, 
PythonParserResult ppr, AstPath path, CompilationInfo info, Set<OffsetRange> 
offsets) {
         BaseDocument doc = (BaseDocument)info.getDocument();
         try {
+            doc.readLock();
             String identifier = Utilities.getIdentifier(doc, lexOffset);
             if (identifier == null) {
                 return null;
@@ -311,6 +312,8 @@
             }
         } catch (BadLocationException ex) {
             Exceptions.printStackTrace(ex);
+        } finally {
+            doc.readUnlock();
         }
 
         return null;
diff --git 
a/python.editor/src/org/netbeans/modules/python/editor/PythonStructureScanner.java
 
b/python.editor/src/org/netbeans/modules/python/editor/PythonStructureScanner.java
--- 
a/python.editor/src/org/netbeans/modules/python/editor/PythonStructureScanner.java
+++ 
b/python.editor/src/org/netbeans/modules/python/editor/PythonStructureScanner.java
@@ -116,20 +116,25 @@
         //List<?extends AstElement> elements = ar.getElements();
         //List<StructureItem> itemList = new 
ArrayList<StructureItem>(elements.size());
 
-        FoldVisitor visitor = new FoldVisitor(info);
-        List<OffsetRange> codeBlocks;
-        try {
-            visitor.visit(root);
-            codeBlocks = visitor.getCodeBlocks();
+        BaseDocument doc = (BaseDocument)info.getDocument();
+        if (doc != null) {
+            try {
+                doc.readLock(); // For Utilities.getRowEnd() access
+                FoldVisitor visitor = new FoldVisitor(info, doc);
+                visitor.visit(root);
+                List<OffsetRange> codeBlocks = visitor.getCodeBlocks();
 
-            Map<String, List<OffsetRange>> folds = new HashMap<String, 
List<OffsetRange>>();
-            folds.put("codeblocks", codeBlocks); // NOI18N
+                Map<String, List<OffsetRange>> folds = new HashMap<String, 
List<OffsetRange>>();
+                folds.put("codeblocks", codeBlocks); // NOI18N
 
-            return folds;
-        } catch (Exception ex) {
-            Exceptions.printStackTrace(ex);
-            return Collections.emptyMap();
+                return folds;
+            } catch (Exception ex) {
+                Exceptions.printStackTrace(ex);
+            } finally {
+                doc.readUnlock();
+            }
         }
+        return Collections.emptyMap();
     }
 
     public Configuration getConfiguration() {
@@ -141,10 +146,10 @@
         private CompilationInfo info;
         private BaseDocument doc;
 
-        private FoldVisitor(CompilationInfo info) {
+        private FoldVisitor(CompilationInfo info, BaseDocument doc) {
             this.info = info;
 
-            this.doc = (BaseDocument)info.getDocument();
+            this.doc = doc;
         }
 
         private void addFoldRange(PythonTree node) {
diff --git 
a/python.editor/src/org/netbeans/modules/python/editor/hints/PythonHintsProvider.java
 
b/python.editor/src/org/netbeans/modules/python/editor/hints/PythonHintsProvider.java
--- 
a/python.editor/src/org/netbeans/modules/python/editor/hints/PythonHintsProvider.java
+++ 
b/python.editor/src/org/netbeans/modules/python/editor/hints/PythonHintsProvider.java
@@ -154,8 +154,13 @@
             return;
         }
 
-        applySelectionRules(manager, context, hints, result);
-    }
+        try {
+            context.doc.readLock();
+            applySelectionRules(manager, context, hints, result);
+        } finally {
+            context.doc.readUnlock();
+        }
+}
 
     private void applySelectionRules(HintsManager manager, RuleContext 
context, List<PythonSelectionRule> rules, List<Hint> result) {
 
@@ -170,7 +175,12 @@
                 continue;
             }
 
-            rule.run(pythonContext, result);
+            try {
+                context.doc.readLock();
+                rule.run(pythonContext, result);
+            } finally {
+                context.doc.readUnlock();
+            }
         }
     }
 
@@ -208,9 +218,12 @@
 
         RuleApplicator finder = new RuleApplicator(manager, context, hints, 
result);
         try {
+            context.doc.readLock();
             finder.visit(root);
         } catch (Exception ex) {
             Exceptions.printStackTrace(ex);
+        } finally {
+            context.doc.readUnlock();
         }
     }
 
@@ -255,19 +268,24 @@
             return;
         }
 
-        CompilationInfo info = context.compilationInfo;
-        int astOffset = PythonAstUtils.getAstOffset(info, caretOffset);
+        try {
+            context.doc.readLock();
 
-        AstPath path = AstPath.get(root, astOffset);
-        Iterator<PythonTree> it = path.leafToRoot();
-        while (it.hasNext()) {
-            if (isCancelled()) {
-                return;
+            CompilationInfo info = context.compilationInfo;
+            int astOffset = PythonAstUtils.getAstOffset(info, caretOffset);
+            AstPath path = AstPath.get(root, astOffset);
+            Iterator<PythonTree> it = path.leafToRoot();
+            while (it.hasNext()) {
+                if (isCancelled()) {
+                    return;
+                }
+
+                PythonTree node = it.next();
+
+                applySuggestions(manager, context, node.getClass(), node, 
path, suggestions, result);
             }
-
-            PythonTree node = it.next();
-
-            applySuggestions(manager, context, node.getClass(), node, path, 
suggestions, result);
+        } finally {
+            context.doc.readUnlock();
         }
 
     //applyRules(NodeTypes.ROOTNODE, path, info, suggestions, caretOffset, 
result);
@@ -282,10 +300,15 @@
             pyCtx.node = node;
             pyCtx.path = path;
 
-            for (PythonAstRule rule : rules) {
-                if (manager.isEnabled(rule)) {
-                    rule.run(pyCtx, result);
+            try {
+                context.doc.readLock();
+                for (PythonAstRule rule : rules) {
+                    if (manager.isEnabled(rule)) {
+                        rule.run(pyCtx, result);
+                    }
                 }
+            } finally {
+                context.doc.readUnlock();
             }
         }
     }
diff --git 
a/python.editor/src/org/netbeans/modules/python/editor/refactoring/PythonRenameRefactoringPlugin.java
 
b/python.editor/src/org/netbeans/modules/python/editor/refactoring/PythonRenameRefactoringPlugin.java
--- 
a/python.editor/src/org/netbeans/modules/python/editor/refactoring/PythonRenameRefactoringPlugin.java
+++ 
b/python.editor/src/org/netbeans/modules/python/editor/refactoring/PythonRenameRefactoringPlugin.java
@@ -561,8 +561,10 @@
             int start = pos;
             int end = pos + oldCode.length();
             // TODO if a SymbolNode, +=1 since the symbolnode includes the 
":"
+            BaseDocument doc = null;
             try {
-                BaseDocument doc = (BaseDocument)ces.openDocument();
+                doc = (BaseDocument)ces.openDocument();
+                doc.readLock();
 
                 if (start > doc.getLength()) {
                     start = end = doc.getLength();
@@ -623,6 +625,10 @@
                 Exceptions.printStackTrace(ie);
             } catch (BadLocationException ble) {
                 Exceptions.printStackTrace(ble);
+            } finally {
+                if (doc != null) {
+                    doc.readUnlock();
+                }
             }
 
             if (newCode == null) {
diff --git 
a/python.editor/src/org/netbeans/modules/python/editor/refactoring/WhereUsedElement.java
 
b/python.editor/src/org/netbeans/modules/python/editor/refactoring/WhereUsedElement.java
--- 
a/python.editor/src/org/netbeans/modules/python/editor/refactoring/WhereUsedElement.java
+++ 
b/python.editor/src/org/netbeans/modules/python/editor/refactoring/WhereUsedElement.java
@@ -136,8 +136,10 @@
         int en = start; // ! Same line as start
         String content = null;
 
+        BaseDocument bdoc = PythonRefUtils.getDocument(info, 
info.getFileObject());
         try {
-            BaseDocument bdoc = PythonRefUtils.getDocument(info, 
info.getFileObject());
+            bdoc.readLock();
+
             // I should be able to just call tree.getInfo().getText() to get 
cached
             // copy - but since I'm playing fast and loose with 
compilationinfos
             // for for example find subclasses (using a singly dummy 
FileInfo) I need
@@ -170,6 +172,8 @@
             }
         } catch (Exception ex) {
             Exceptions.printStackTrace(ex);
+        } finally {
+            bdoc.readUnlock();
         }
 
         StringBuilder sb = new StringBuilder();

[[nbpython-commits]] [hg] main/contrib: Fix 155236: Add document read locks around d...

Tor Norbye 10/21/2010

Project Features

About this Project

www was started in November 2009, is owned by jpirek, and has 21 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close