IDEA-67320 GitTask: don't hold lock if the process finished early.
authorKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Fri, 1 Apr 2011 07:42:12 +0000 (11:42 +0400)
committerKirill Likhodedov <kirill.likhodedov@jetbrains.com>
Fri, 1 Apr 2011 07:50:24 +0000 (11:50 +0400)
plugins/git4idea/src/git4idea/commands/GitTask.java

index dacec589a33d87fdd86ac7e29caf295979ec35ad..d29fcbff009bf40fce7aa70eb5f41cd8a412799f 100644 (file)
@@ -33,6 +33,7 @@ import org.jetbrains.annotations.Nullable;
 
 import java.util.Timer;
 import java.util.TimerTask;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
 /**
@@ -118,14 +119,17 @@ public class GitTask {
    */
   public void execute(boolean sync, boolean modal, final GitTaskResultHandler resultHandler) {
     final Object LOCK = new Object();
+    final AtomicBoolean completed = new AtomicBoolean();
 
     if (modal) {
       ModalTask task = new ModalTask(myProject, myHandler, myTitle) {
         @Override public void onSuccess() {
           commonOnSuccess(LOCK, resultHandler);
+          completed.set(true);
         }
         @Override public void onCancel() {
           commonOnCancel(LOCK, resultHandler);
+          completed.set(true);
         }
       };
       ProgressManager.getInstance().run(task);
@@ -133,21 +137,25 @@ public class GitTask {
       BackgroundableTask task = new BackgroundableTask(myProject, myHandler, myTitle) {
         @Override public void onSuccess() {
           commonOnSuccess(LOCK, resultHandler);
+          completed.set(true);
         }
         @Override public void onCancel() {
           commonOnCancel(LOCK, resultHandler);
+          completed.set(true);
         }
       };
       GitVcs.runInBackground(task);
     }
 
     if (sync) {
-      try {
-        synchronized (LOCK) {
-          LOCK.wait();
+      while (!completed.get()) {
+        try {
+          synchronized (LOCK) {
+            LOCK.wait(50);
+          }
+        } catch (InterruptedException e) {
+          LOG.info(e);
         }
-      } catch (InterruptedException e) {
-        LOG.info(e);
       }
     }
   }