optimisation: do not start background thread if there is no contention
authorAlexey Kudravtsev <cdr@intellij.com>
Fri, 27 Jan 2012 09:14:12 +0000 (13:14 +0400)
committerAlexey Kudravtsev <cdr@intellij.com>
Mon, 30 Jan 2012 06:21:16 +0000 (10:21 +0400)
platform/platform-impl/src/com/intellij/openapi/application/impl/ApplicationImpl.java

index 88e5bf57d91c87bcdc2790fbbab05791f1412e28..b88935f46efcd29fd12c2267f15dbcc01f5358c8 100644 (file)
@@ -1118,28 +1118,29 @@ public class ApplicationImpl extends ComponentManagerImpl implements Application
       fireBeforeWriteActionStart(_clazz);
       final AtomicBoolean stopped = new AtomicBoolean(false);
 
-      if (ourDumpThreadsOnLongWriteActionWaiting > 0) {
-        executeOnPooledThread(new Runnable() {
-          @Override
-          public void run() {
-            while (!stopped.get()) {
-              try {
-                Thread.sleep(ourDumpThreadsOnLongWriteActionWaiting);
-                if (!stopped.get()) {
-                  PerformanceWatcher.getInstance().dumpThreads(true);
-                }
-              }
-              catch (InterruptedException ignored) {
-              }
-            }
-          }
-        });
-      }
-
       LOG.assertTrue(myActionsLock.isWriteLockAcquired(Thread.currentThread())
                      || !Thread.holdsLock(PsiLock.LOCK), "Thread must not hold PsiLock while performing writeAction");
       try {
-        myActionsLock.writeLock().acquire();
+        if (!myActionsLock.writeLock().attempt(0)) {
+          if (ourDumpThreadsOnLongWriteActionWaiting > 0) {
+            executeOnPooledThread(new Runnable() {
+              @Override
+              public void run() {
+                while (!stopped.get()) {
+                  try {
+                    Thread.sleep(ourDumpThreadsOnLongWriteActionWaiting);
+                    if (!stopped.get()) {
+                      PerformanceWatcher.getInstance().dumpThreads(true);
+                    }
+                  }
+                  catch (InterruptedException ignored) {
+                  }
+                }
+              }
+            });
+          }
+          myActionsLock.writeLock().acquire();
+        }
         acquired();
       }
       catch (InterruptedException e) {