summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/psi/impl/DocumentCommitThread.java
blob: 32a2e38384d9c5012e4903cbcac7b573f62f39b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.psi.impl;

import com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator;
import com.intellij.ide.startup.impl.StartupManagerImpl;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationAdapter;
import com.intellij.openapi.application.ex.ApplicationEx;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.util.ProgressIndicatorBase;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupManager;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.FileViewProvider;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiFile;
import com.intellij.util.Processor;
import com.intellij.util.SmartList;
import com.intellij.util.containers.Queue;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DocumentCommitThread extends DocumentCommitProcessor implements Runnable, Disposable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.psi.impl.DocumentCommitThread");

  private final Queue<CommitTask> documentsToCommit = new Queue<CommitTask>(10);
  private final List<CommitTask> documentsToApplyInEDT = new ArrayList<CommitTask>(10);  // guarded by documentsToCommit
  private final ApplicationEx myApplication;
  private volatile boolean isDisposed;
  private CommitTask currentTask; // guarded by documentsToCommit
  private volatile boolean threadFinished;
  private volatile boolean myEnabled; // true if we can do commits. set to false temporarily during the write action.

  public static DocumentCommitThread getInstance() {
    return ServiceManager.getService(DocumentCommitThread.class);
  }

  public DocumentCommitThread(final ApplicationEx application) {
    myApplication = application;
    // install listener in EDT to avoid missing events in case we are inside write action right now
    application.invokeLater(new Runnable() {
      @Override
      public void run() {
        application.addApplicationListener(new ApplicationAdapter() {
          private int runningWriteActions;

          @Override
          public void beforeWriteActionStart(Object action) {
            if (runningWriteActions++ == 0) {
              disable("Write action started: " + action);
            }
          }

          @Override
          public void writeActionFinished(Object action) {
            if (--runningWriteActions == 0) {
              enable("Write action finished: " + action);
            }
          }
        }, DocumentCommitThread.this);

        enable("Listener installed, started");
      }
    });
    log("Starting thread", null, false);
    new Thread(this, "Document commit thread").start();
  }

  @Override
  public void dispose() {
    isDisposed = true;
    synchronized (documentsToCommit) {
      documentsToCommit.clear();
    }
    cancel("Stop thread");
    wakeUpQueue();
    while (!threadFinished) {
      wakeUpQueue();
      synchronized (documentsToCommit) {
        try {
          documentsToCommit.wait(10);
        }
        catch (InterruptedException ignored) {
        }
      }
    }
  }

  private void disable(@NonNls Object reason) {
    // write action has just started, all commits are useless
    cancel(reason);
    myEnabled = false;
    log("Disabled", null, false, reason);
  }

  private void enable(@NonNls Object reason) {
    myEnabled = true;
    wakeUpQueue();
    log("Enabled", null, false, reason);
  }

  private void wakeUpQueue() {
    synchronized (documentsToCommit) {
      documentsToCommit.notifyAll();
    }
  }

  private void cancel(@NonNls @NotNull Object reason) {
    startNewTask(null, reason);
  }

  @Override
  public void commitAsynchronously(@NotNull final Project project, @NotNull final Document document, @NonNls @NotNull Object reason) {
    queueCommit(project, document, reason);
  }

  public void queueCommit(@NotNull final Project project, @NotNull final Document document, @NonNls @NotNull Object reason) {
    assert !isDisposed : "already disposed";

    if (!project.isInitialized()) return;
    PsiFile psiFile = PsiDocumentManager.getInstance(project).getCachedPsiFile(document);
    if (psiFile == null) return;

    doQueue(project, document, reason);
  }

  private void doQueue(@NotNull Project project, @NotNull Document document, @NotNull Object reason) {
    synchronized (documentsToCommit) {
      ProgressIndicator indicator = new DaemonProgressIndicator();
      CommitTask newTask = new CommitTask(document, project, indicator, reason);

      markRemovedFromDocsToCommit(newTask);
      markRemovedCurrentTask(newTask);
      removeFromDocsToApplyInEDT(newTask);

      documentsToCommit.addLast(newTask);
      log("Queued", newTask, false, reason);

      wakeUpQueue();
    }
  }

  private final StringBuilder log = new StringBuilder();

  @Override
  public void log(@NonNls String msg, @Nullable CommitTask task, boolean synchronously, @NonNls Object... args) {
    if (true) return;

    String indent = new SimpleDateFormat("mm:ss:SSSS").format(new Date()) +
      (SwingUtilities.isEventDispatchThread() ? "-    " : Thread.currentThread().getName().equals("Document commit thread") ? "-  >" : "-");
    @NonNls
    String s = indent +
               msg + (synchronously ? " (sync)" : "") +
               (task == null ? "" : "; task: " + task+" ("+System.identityHashCode(task)+")");

    for (Object arg : args) {
      if (!StringUtil.isEmpty(String.valueOf(arg))) {
        s += "; "+arg;
      }
    }
    if (task != null) {
      boolean stillUncommitted = !task.project.isDisposed() &&
                                 ((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(task.project)).isInUncommittedSet(task.document);
      if (stillUncommitted) {
        s += "; Uncommitted: " + task.document;
      }
    }

    System.err.println(s);

    log.append(s).append("\n");
    if (log.length() > 1000000) {
      log.delete(0, 1000000);
    }
  }


  // cancels all pending commits
  @TestOnly
  public void cancelAll() {
    synchronized (documentsToCommit) {
      cancel("cancel all in tests");
      markRemovedFromDocsToCommit(null);
      documentsToCommit.clear();
      removeFromDocsToApplyInEDT(null);
      markRemovedCurrentTask(null);
    }
  }

  @TestOnly
  public void clearQueue() {
    cancelAll();
    log.setLength(0);
    wakeUpQueue();
  }

  private void markRemovedCurrentTask(@Nullable CommitTask newTask) {
    CommitTask task = currentTask;
    if (task != null && (newTask == null || task.equals(newTask))) {
      task.removed = true;
      cancel("Sync commit intervened");
    }
  }

  private void removeFromDocsToApplyInEDT(@Nullable("null means all") CommitTask newTask) {
    for (int i = documentsToApplyInEDT.size() - 1; i >= 0; i--) {
      CommitTask task = documentsToApplyInEDT.get(i);
      if (newTask == null || task.equals(newTask)) {
        task.removed = true;
        documentsToApplyInEDT.remove(i);
        log("Marked and Removed from EDT apply queue (sync commit called)", task, true);
      }
    }
  }

  private void markRemovedFromDocsToCommit(@Nullable("null means all") final CommitTask newTask) {
    processAll(new Processor<CommitTask>() {
      @Override
      public boolean process(CommitTask task) {
        if (newTask == null || task.equals(newTask)) {
          task.removed = true;
          log("marker as Removed in background queue", task, true);
        }
        return true;
      }
    });
  }

  @Override
  public void run() {
    threadFinished = false;
    try {
      while (!isDisposed) {
        try {
          pollQueue();
        }
        catch(Throwable e) {
          LOG.error(e);
        }
      }
    }
    finally {
      threadFinished = true;
    }
    // ping the thread waiting for close
    wakeUpQueue();
    log("Good bye", null, false);
  }

  private void pollQueue() {
    boolean success = false;
    Document document = null;
    Project project = null;
    CommitTask task = null;
    try {
      ProgressIndicator indicator;
      synchronized (documentsToCommit) {
        if (!myEnabled || documentsToCommit.isEmpty()) {
          documentsToCommit.wait();
          return;
        }
        task = documentsToCommit.pullFirst();
        document = task.document;
        indicator = task.indicator;
        project = task.project;

        log("Pulled", task, false, indicator);

        if (project.isDisposed() || !((PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project)).isInUncommittedSet(document)) {
          log("Abandon and proceed to next",task, false);
          return;
        }

        if (task.removed) {
          return; // document has been marked as removed, e.g. by synchronous commit
        }

        startNewTask(task, "Pulled new task");

        // transfer to documentsToApplyInEDT
        documentsToApplyInEDT.add(task);
      }

      Runnable finishRunnable = null;
      if (indicator.isCanceled()) {
        success = false;
      }
      else {
        final CommitTask commitTask = task;
        final Runnable[] result = new Runnable[1];
        ProgressManager.getInstance().executeProcessUnderProgress(new Runnable() {
          @Override
          public void run() {
            result[0] = commitUnderProgress(commitTask, false);
          }
        }, commitTask.indicator);
        finishRunnable = result[0];
        success = finishRunnable != null;
        log("commit returned", task, false, finishRunnable, indicator);
      }

      if (success) {
        assert !myApplication.isDispatchThread();
        UIUtil.invokeLaterIfNeeded(finishRunnable);
        log("Invoked later finishRunnable", task, false, finishRunnable, indicator);
      }
    }
    catch (ProcessCanceledException e) {
      cancel(e); // leave queue unchanged
      log("PCE", task, false, e);
      success = false;
    }
    catch (InterruptedException e) {
      // app must be closing
      log("IE", task, false, e);
      cancel(e);
    }
    catch (Throwable e) {
      LOG.error(e);
      cancel(e);
    }
    synchronized (documentsToCommit) {
      if (!success && !task.removed) { // sync commit has not intervened
        // reset status for queue back successfully
        doQueue(project, document, "re-added on failure");
      }
      currentTask = null; // do not cancel, it's being invokeLatered
    }
  }

  @Override
  public void commitSynchronously(@NotNull Document document, @NotNull Project project) {
    assert !isDisposed;
    myApplication.assertWriteAccessAllowed();

    if (!project.isInitialized() && !project.isDefault()) {
      @NonNls String s = project + "; Disposed: "+project.isDisposed()+"; Open: "+project.isOpen();
      s += "; SA Passed: ";
      try {
        s += ((StartupManagerImpl)StartupManager.getInstance(project)).startupActivityPassed();
      }
      catch (Exception e) {
        s += e;
      }
      try {
        Disposer.dispose(project);
      }
      catch (Throwable ignored) {
        // do not fill log with endless exceptions
      }
      throw new RuntimeException(s);
    }

    ProgressIndicator indicator = createProgressIndicator();
    CommitTask task = new CommitTask(document, project, indicator, "Sync commit");
    synchronized (documentsToCommit) {
      markRemovedFromDocsToCommit(task);
      markRemovedCurrentTask(task);
      removeFromDocsToApplyInEDT(task);
    }

    log("About to commit sync", task, true, indicator);

    Runnable finish = commitUnderProgress(task, true);
    log("Committed sync", task, true, finish, indicator);
    assert finish != null;

    finish.run();

    // let our thread know that queue must be polled again
    wakeUpQueue();
  }

  @NotNull
  @Override
  protected ProgressIndicator createProgressIndicator() {
    return new ProgressIndicatorBase();
  }

  private void startNewTask(@Nullable CommitTask task, @NotNull Object reason) {
    synchronized (documentsToCommit) { // sync to prevent overwriting
      CommitTask cur = currentTask;
      if (cur != null) {
        cur.indicator.cancel();
      }
      currentTask = task;
    }
    log("new task started", task, false, reason);
  }

  // returns finish commit Runnable (to be invoked later in EDT), or null on failure
  @Nullable
  private Runnable commitUnderProgress(@NotNull final CommitTask task, final boolean synchronously) {
    final Project project = task.project;
    final Document document = task.document;
    final List<Processor<Document>> finishProcessors = new SmartList<Processor<Document>>();
    Runnable runnable = new Runnable() {
      @Override
      public void run() {
        myApplication.assertReadAccessAllowed();
        if (project.isDisposed()) return;

        final PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project);
        if (documentManager.isCommitted(document)) return;

        FileViewProvider viewProvider = documentManager.getCachedViewProvider(document);
        if (viewProvider == null) {
          finishProcessors.add(handleCommitWithoutPsi(documentManager, document, task, synchronously));
          return;
        }

        List<PsiFile> psiFiles = viewProvider.getAllFiles();
        for (PsiFile file : psiFiles) {
          if (file.isValid()) {
            Processor<Document> finishProcessor = doCommit(task, file, synchronously);
            if (finishProcessor != null) {
              finishProcessors.add(finishProcessor);
            }
          }
        }
      }
    };
    if (synchronously) {
      myApplication.assertWriteAccessAllowed();
      runnable.run();
    }
    else if (!myApplication.tryRunReadAction(runnable)) {
      log("Could not start read action", task, synchronously, myApplication.isReadAccessAllowed(), Thread.currentThread());
      return null;
    }

    boolean canceled = task.indicator.isCanceled();
    assert !synchronously || !canceled;
    if (canceled || task.removed) {
      return null;
    }

    Runnable finishRunnable = new Runnable() {
      @Override
      public void run() {
        myApplication.assertIsDispatchThread();

        Project project = task.project;
        if (project.isDisposed()) return;

        Document document = task.document;

        synchronized (documentsToCommit) {
          boolean isValid = !task.removed;
          for (int i = documentsToApplyInEDT.size() - 1; i >= 0; i--) {
            CommitTask queuedTask = documentsToApplyInEDT.get(i);
            boolean taskIsValid = !queuedTask.removed;
            if (task == queuedTask) { // find the same task in the queue
              documentsToApplyInEDT.remove(i);
              isValid &= taskIsValid;
              log("Task matched, removed from documentsToApplyInEDT", queuedTask, false, task);
            }
            else if (!taskIsValid) {
              documentsToApplyInEDT.remove(i);
              log("Task invalid, removed from documentsToApplyInEDT", queuedTask, false);
            }
          }
          if (!isValid) {
            log("Marked as already committed in EDT apply queue, return", task, true);
            return;
          }
        }

        PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl)PsiDocumentManager.getInstance(project);

        log("Executing later finishCommit", task, false);
        boolean success = documentManager.finishCommit(document, finishProcessors, synchronously, task.reason);
        if (synchronously) {
          assert success;
        }
        log("after call finishCommit",task, synchronously, success);
        if (synchronously || success) {
          assert !documentManager.isInUncommittedSet(document);
        }
        if (!success) {
          // add document back to the queue
          queueCommit(project, document, "Re-added back");
        }
      }
    };
    return finishRunnable;
  }

  @NotNull
  private Processor<Document> handleCommitWithoutPsi(@NotNull final PsiDocumentManagerImpl documentManager,
                                                     @NotNull Document document,
                                                     @NotNull final CommitTask task,
                                                     final boolean synchronously) {
    final long startDocModificationTimeStamp = document.getModificationStamp();
    return new Processor<Document>() {
      @Override
      public boolean process(Document document) {
        log("Finishing without PSI", task, synchronously, document.getModificationStamp(), startDocModificationTimeStamp);
        if (document.getModificationStamp() != startDocModificationTimeStamp ||
            documentManager.getCachedViewProvider(document) != null) {
          return false;
        }

        documentManager.handleCommitWithoutPsi(document);
        return true;
      }
    };
  }

  private boolean processAll(final Processor<CommitTask> processor) {
    final boolean[] result = {true};
    synchronized (documentsToCommit) {
      documentsToCommit.process(new Processor<CommitTask>() {
        @Override
        public boolean process(CommitTask commitTask) {
          result[0] &= processor.process(commitTask);
          return true;
        }
      });
    }
    return result[0];
  }

  @TestOnly
  boolean isEnabled() {
    return myEnabled;
  }

  @TestOnly
  public void waitUntilAllCommitted(long timeout) throws InterruptedException {
    if (!myEnabled) {
      throw new IllegalStateException("DocumentCommitThread is disabled");
    }
    int attempts = 0;
    int delay = 100;
    synchronized (documentsToCommit) {
      while(!documentsToCommit.isEmpty() || currentTask != null) {
        documentsToCommit.wait(delay);
        if (delay * attempts > timeout) {
          throw new RuntimeException("timeout");
        }
        attempts++;
      }
    }
  }
}