This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 40766
Collapse All | Expand All

(-)CloneableEditorSupport.java (-13 / +5 lines)
Lines 137-148 Link Here
137
    /** position manager */
137
    /** position manager */
138
    private PositionRef.Manager positionManager;
138
    private PositionRef.Manager positionManager;
139
    
139
    
140
    /* [pnejedly] This lock is shared by PositionRef.Manager and this class
141
     * to prevent PR.M modifications while the document is just loaded
142
     * or freed. See #40766.
143
     */
144
    Object docStateLock = new Object();
145
    
146
    /** The string which will be appended to the name of top component
140
    /** The string which will be appended to the name of top component
147
    * when top component becomes modified */
141
    * when top component becomes modified */
148
//    protected String modifiedAppendix = " *"; // NOI18N
142
//    protected String modifiedAppendix = " *"; // NOI18N
Lines 409-417 Link Here
409
                    // where another threads may operate already
403
                    // where another threads may operate already
410
                    if (!runningInAtomicLock) {
404
                    if (!runningInAtomicLock) {
411
                        runningInAtomicLock = true;
405
                        runningInAtomicLock = true;
412
                        synchronized (docStateLock) { // [pnejedly] See #40766
406
                        NbDocument.runAtomic(docToLoad, this);
413
                            NbDocument.runAtomic(docToLoad, this);
407
414
                        }
415
                        // Add undoable listener after atomic change has finished
408
                        // Add undoable listener after atomic change has finished
416
                        synchronized (getLock()) {
409
                        synchronized (getLock()) {
417
                            if (doc == docToLoad) { // if document still valid
410
                            if (doc == docToLoad) { // if document still valid
Lines 1468-1474 Link Here
1468
    /** Clears all data from memory.
1461
    /** Clears all data from memory.
1469
    */
1462
    */
1470
    private void closeDocument () {
1463
    private void closeDocument () {
1471
      synchronized(docStateLock) { // [pnejedly] See #40766
1472
        synchronized (getLock()) {
1464
        synchronized (getLock()) {
1473
            while (true) {
1465
            while (true) {
1474
                switch (documentStatus) {
1466
                switch (documentStatus) {
Lines 1487-1493 Link Here
1487
                }
1479
                }
1488
            }
1480
            }
1489
        }
1481
        }
1490
      }
1491
    }
1482
    }
1492
     
1483
     
1493
    /** Is called under getLock () to close the document.
1484
    /** Is called under getLock () to close the document.
Lines 1773-1778 Link Here
1773
        return listener;
1764
        return listener;
1774
    }
1765
    }
1775
1766
1767
    // [pnejedly]: helper for 40766 test
1768
    void howToReproduceDeadlock40766(boolean beforeLock) {}
1776
1769
1777
1770
1778
    /** Default editor kit.
1771
    /** Default editor kit.
Lines 1808-1815 Link Here
1808
        }
1801
        }
1809
    }
1802
    }
1810
1803
1811
    // [pnejedly]: helper for 40766 test
1804
1812
    void howToReproduceDeadlock40766(boolean beforeLock) {}
1813
1805
1814
1806
1815
    /** The listener that this support uses to communicate with
1807
    /** The listener that this support uses to communicate with
(-)PositionRef.java (-25 / +46 lines)
Lines 194-199 Link Here
194
194
195
        /** the document for this manager or null if the manager is not in memory */
195
        /** the document for this manager or null if the manager is not in memory */
196
        transient private StyledDocument doc;
196
        transient private StyledDocument doc;
197
        /** document that this thread should use */
198
        private static ThreadLocal DOCUMENT = new ThreadLocal ();
197
199
198
200
199
        static final long serialVersionUID =-4374030124265110801L;
201
        static final long serialVersionUID =-4374030124265110801L;
Lines 273-278 Link Here
273
            doc = null;
275
            doc = null;
274
        }
276
        }
275
        
277
        
278
        /** Gets the document this object should work on.
279
         * @return docoument or null
280
         */
281
        private StyledDocument getDoc () {
282
            Object d = DOCUMENT.get ();
283
            if (d instanceof StyledDocument) {
284
                return (StyledDocument)d;
285
            }
286
            if (d == this) {
287
                return null;
288
            }
289
            return this.doc;
290
        }
291
        
276
        /** Puts/gets positions to/from memory. It also provides full
292
        /** Puts/gets positions to/from memory. It also provides full
277
         * pass sweep of the data structure (inlined in the code).
293
         * pass sweep of the data structure (inlined in the code).
278
         * @param toMemory puts positions to memory if <code>true</code>,
294
         * @param toMemory puts positions to memory if <code>true</code>,
Lines 363-372 Link Here
363
                            pos.kind.toMemory(pos.insertAfter));
379
                            pos.kind.toMemory(pos.insertAfter));
364
            }
380
            }
365
 */
381
 */
366
            // [pnejedly] needed to prevent doc variable modification while adding position, #40766 
382
            kind = (Kind)new DocumentRenderer(DocumentRenderer.ADD_POSITION, pos).renderToObject();
367
            synchronized(support.docStateLock) {
368
                kind = (Kind)new DocumentRenderer(DocumentRenderer.ADD_POSITION, pos).renderToObject();
369
            }
370
            checkQueue();
383
            checkQueue();
371
384
372
            return kind;
385
            return kind;
Lines 510-516 Link Here
510
                        "Illegal PositionKind: " + pos + "[offset=" // NOI18N
523
                        "Illegal PositionKind: " + pos + "[offset=" // NOI18N
511
                        + offset + ",line=" // NOI18N
524
                        + offset + ",line=" // NOI18N
512
                        + line + ",column=" + column + "] in " // NOI18N
525
                        + line + ",column=" + column + "] in " // NOI18N
513
                        + doc + " used by " + support + "." // NOI18N
526
                        + getDoc () + " used by " + support + "." // NOI18N
514
                    );
527
                    );
515
                }
528
                }
516
                
529
                
Lines 563-569 Link Here
563
                        "Illegal OutKind[offset=" // NOI18N
576
                        "Illegal OutKind[offset=" // NOI18N
564
                        + offset + ",line=" // NOI18N
577
                        + offset + ",line=" // NOI18N
565
                        + line + ",column=" + column + "] in " // NOI18N
578
                        + line + ",column=" + column + "] in " // NOI18N
566
                        + doc + " used by " + support + "." // NOI18N
579
                        + getDoc () + " used by " + support + "." // NOI18N
567
                    );
580
                    );
568
                }
581
                }
569
                
582
                
Lines 602-608 Link Here
602
                        "Illegal OutKind[offset=" // NOI18N
615
                        "Illegal OutKind[offset=" // NOI18N
603
                        + offset + ",line=" // NOI18N
616
                        + offset + ",line=" // NOI18N
604
                        + line + ",column=" + column + "] in " // NOI18N
617
                        + line + ",column=" + column + "] in " // NOI18N
605
                        + doc + " used by " + support + "." // NOI18N
618
                        + getDoc () + " used by " + support + "." // NOI18N
606
                    );
619
                    );
607
                }
620
                }
608
                
621
                
Lines 624-630 Link Here
624
                if(offset < 0) {
637
                if(offset < 0) {
625
                    throw new IndexOutOfBoundsException(
638
                    throw new IndexOutOfBoundsException(
626
                        "Illegal OffsetKind[offset=" // NOI18N
639
                        "Illegal OffsetKind[offset=" // NOI18N
627
                        + offset + "] in " + doc + " used by " // NOI18N
640
                        + offset + "] in " + getDoc () + " used by " // NOI18N
628
                        + support + "." // NOI18N
641
                        + support + "." // NOI18N
629
                    );
642
                    );
630
                }
643
                }
Lines 658-664 Link Here
658
                if(offset < 0) {
671
                if(offset < 0) {
659
                    throw new IOException(
672
                    throw new IOException(
660
                        "Illegal OffsetKind[offset=" // NOI18N
673
                        "Illegal OffsetKind[offset=" // NOI18N
661
                        + offset + "] in " + doc + " used by " // NOI18N
674
                        + offset + "] in " + getDoc () + " used by " // NOI18N
662
                        + support + "." // NOI18N
675
                        + support + "." // NOI18N
663
                    );
676
                    );
664
                }
677
                }
Lines 683-689 Link Here
683
                    throw new IndexOutOfBoundsException(
696
                    throw new IndexOutOfBoundsException(
684
                        "Illegal LineKind[line=" // NOI18N
697
                        "Illegal LineKind[line=" // NOI18N
685
                        + line + ",column=" + column + "] in " // NOI18N
698
                        + line + ",column=" + column + "] in " // NOI18N
686
                        + doc + " used by " + support + "." // NOI18N
699
                        + getDoc () + " used by " + support + "." // NOI18N
687
                    );
700
                    );
688
                }
701
                }
689
                
702
                
Lines 739-745 Link Here
739
                    throw new IOException(
752
                    throw new IOException(
740
                        "Illegal LineKind[line=" // NOI18N
753
                        "Illegal LineKind[line=" // NOI18N
741
                        + line + ",column=" + column + "] in " // NOI18N
754
                        + line + ",column=" + column + "] in " // NOI18N
742
                        + doc + " used by " + support + "." // NOI18N
755
                        + getDoc () + " used by " + support + "." // NOI18N
743
                    );
756
                    );
744
                }
757
                }
745
                
758
                
Lines 864-873 Link Here
864
            }
877
            }
865
            
878
            
866
            void render() {
879
            void render() {
867
                if (doc != null) {
880
                StyledDocument d = getDoc ();
868
                    doc.render(this);
881
                Object prev = DOCUMENT.get ();
869
                } else {
882
                try {
870
                    this.run();
883
                    if (d != null) {
884
                        DOCUMENT.set (d);
885
                        d.render(this);
886
                    } else {
887
                        DOCUMENT.set (Manager.this);
888
                        this.run();
889
                    }
890
                } finally {
891
                    DOCUMENT.set (prev);
871
                }
892
                }
872
            }
893
            }
873
            
894
            
Lines 925-931 Link Here
925
                                try {
946
                                try {
926
                                    int line = argKind.getLine();
947
                                    int line = argKind.getLine();
927
                                    int col = argKind.getColumn();
948
                                    int col = argKind.getColumn();
928
                                    Element lineRoot = NbDocument.findLineRootElement(doc);
949
                                    Element lineRoot = NbDocument.findLineRootElement(getDoc ());
929
                                    if (line < lineRoot.getElementCount()) {
950
                                    if (line < lineRoot.getElementCount()) {
930
                                        Element lineElem = lineRoot.getElement(line);
951
                                        Element lineElem = lineRoot.getElement(line);
931
                                        int lineStartOffset = lineElem.getStartOffset();
952
                                        int lineStartOffset = lineElem.getStartOffset();
Lines 942-961 Link Here
942
                            }
963
                            }
943
964
944
                            try {
965
                            try {
945
                                p = NbDocument.createPosition (doc, offset,
966
                                p = NbDocument.createPosition (getDoc (), offset,
946
                                    argInsertAfter ? Position.Bias.Forward : Position.Bias.Backward);
967
                                    argInsertAfter ? Position.Bias.Forward : Position.Bias.Backward);
947
                            } catch (BadLocationException e) {
968
                            } catch (BadLocationException e) {
948
                                p = doc.getEndPosition ();
969
                                p = getDoc ().getEndPosition ();
949
                            }
970
                            }
950
                            retObject = (PositionKind)new PositionKind (p);
971
                            retObject = (PositionKind)new PositionKind (p);
951
                            break; }
972
                            break; }
952
973
953
                        case POSITION_KIND_GET_LINE: {
974
                        case POSITION_KIND_GET_LINE: {
954
                            retInt = NbDocument.findLineNumber(doc, argKind.getOffset());
975
                            retInt = NbDocument.findLineNumber(getDoc (), argKind.getOffset());
955
                            break; }
976
                            break; }
956
977
957
                        case POSITION_KIND_GET_COLUMN: {
978
                        case POSITION_KIND_GET_COLUMN: {
958
                            retInt = NbDocument.findLineColumn(doc, argKind.getOffset());
979
                            retInt = NbDocument.findLineColumn(getDoc (), argKind.getOffset());
959
                            break; }
980
                            break; }
960
981
961
                        case POSITION_KIND_WRITE:
982
                        case POSITION_KIND_WRITE:
Lines 980-992 Link Here
980
                        case LINE_KIND_TO_MEMORY: {
1001
                        case LINE_KIND_TO_MEMORY: {
981
                            // try to find the right position
1002
                            // try to find the right position
982
                            try {
1003
                            try {
983
                                retObject = NbDocument.createPosition (doc,
1004
                                retObject = NbDocument.createPosition (getDoc (),
984
                                    NbDocument.findLineOffset (doc, argLine) + argColumn,
1005
                                    NbDocument.findLineOffset (getDoc (), argLine) + argColumn,
985
                                    argInsertAfter ? Position.Bias.Forward : Position.Bias.Backward);
1006
                                    argInsertAfter ? Position.Bias.Forward : Position.Bias.Backward);
986
                            } catch (BadLocationException e) {
1007
                            } catch (BadLocationException e) {
987
                                retObject = doc.getEndPosition ();
1008
                                retObject = getDoc ().getEndPosition ();
988
                            } catch (IndexOutOfBoundsException e) {
1009
                            } catch (IndexOutOfBoundsException e) {
989
                                retObject = doc.getEndPosition();
1010
                                retObject = getDoc ().getEndPosition();
990
                            }
1011
                            }
991
                            break; }
1012
                            break; }
992
1013
Lines 1024-1030 Link Here
1024
                                support.howToReproduceDeadlock40766(false);
1045
                                support.howToReproduceDeadlock40766(false);
1025
                                head.next = new ChainItem(argPos, queue, head.next);
1046
                                head.next = new ChainItem(argPos, queue, head.next);
1026
1047
1027
                                retObject = (doc == null ? 
1048
                                retObject = (getDoc () == null ? 
1028
                                    argPos.kind : 
1049
                                    argPos.kind : 
1029
                                    argPos.kind.toMemory(argPos.insertAfter));
1050
                                    argPos.kind.toMemory(argPos.insertAfter));
1030
                            }
1051
                            }

Return to bug 40766