/**
  * Block until the provided request future request has finished or the timeout has expired.
  *
  * @param future The request future to wait for
  * @param timeout The maximum duration (in ms) to wait for the request
  * @return true if the future is done, false otherwise
  * @throws WakeupException if {@link #wakeup()} is called from another thread
  */
 public boolean poll(RequestFuture<?> future, long timeout) {
   long begin = time.milliseconds();
   long remaining = timeout;
   long now = begin;
   do {
     poll(remaining, now, true);
     now = time.milliseconds();
     long elapsed = now - begin;
     remaining = timeout - elapsed;
   } while (!future.isDone() && remaining > 0);
   return future.isDone();
 }
 public Collection<RequestFuture> cancelAutoMakeTasks(Project project) {
   final Collection<RequestFuture> futures = new ArrayList<RequestFuture>();
   synchronized (myAutomakeFutures) {
     for (Map.Entry<RequestFuture, Project> entry : myAutomakeFutures.entrySet()) {
       if (entry.getValue().equals(project)) {
         final RequestFuture future = entry.getKey();
         future.cancel(false);
         futures.add(future);
       }
     }
   }
   return futures;
 }
 private void runAutoMake() {
   if (ApplicationManager.getApplication().isUnitTestMode()) {
     return;
   }
   final Project[] openProjects = myProjectManager.getOpenProjects();
   if (openProjects.length > 0) {
     final List<RequestFuture> futures = new ArrayList<RequestFuture>();
     for (final Project project : openProjects) {
       if (project.isDefault() || project.isDisposed()) {
         continue;
       }
       final CompilerWorkspaceConfiguration config =
           CompilerWorkspaceConfiguration.getInstance(project);
       if (!config.useOutOfProcessBuild() || !config.MAKE_PROJECT_ON_SAVE) {
         continue;
       }
       final List<String> emptyList = Collections.emptyList();
       final RequestFuture future =
           scheduleBuild(
               project,
               false,
               true,
               emptyList,
               emptyList,
               emptyList,
               Collections.<String, String>emptyMap(),
               new AutoMakeMessageHandler(project));
       if (future != null) {
         futures.add(future);
         synchronized (myAutomakeFutures) {
           myAutomakeFutures.put(future, project);
         }
       }
     }
     try {
       for (RequestFuture future : futures) {
         future.waitFor();
       }
     } finally {
       synchronized (myAutomakeFutures) {
         myAutomakeFutures.keySet().removeAll(futures);
       }
     }
   }
 }
 /**
  * Block indefinitely until the given request future has finished.
  *
  * @param future The request future to await.
  * @throws WakeupException if {@link #wakeup()} is called from another thread
  */
 public void poll(RequestFuture<?> future) {
   while (!future.isDone()) poll(Long.MAX_VALUE);
 }
 @Override
 public void projectClosing(Project project) {
   for (RequestFuture future : cancelAutoMakeTasks(project)) {
     future.waitFor(500, TimeUnit.MILLISECONDS);
   }
 }