/*
 * Copyright 2000-2015 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.openapi.util;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.concurrency.Semaphore;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ShutDownTracker implements Runnable {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.ShutDownTracker");
  private final List<Thread> myThreads = new ArrayList<Thread>();
  private final LinkedList<Thread> myShutdownThreads = new LinkedList<Thread>();
  private final LinkedList<Runnable> myShutdownTasks = new LinkedList<Runnable>();
  private volatile boolean myIsShutdownHookRunning = false;

  private ShutDownTracker() {
    //noinspection HardCodedStringLiteral
    Runtime.getRuntime().addShutdownHook(new Thread(this, "Shutdown tracker"));
  }

  private static class ShutDownTrackerHolder {
    private static final ShutDownTracker ourInstance = new ShutDownTracker();
  }

  @NotNull
  public static ShutDownTracker getInstance() {
    return ShutDownTrackerHolder.ourInstance;
  }

  public static boolean isShutdownHookRunning() {
    return getInstance().myIsShutdownHookRunning;
  }

  @Override
  public void run() {
    myIsShutdownHookRunning = true;

    ensureStopperThreadsFinished();

    for (Runnable task = removeLast(myShutdownTasks); task != null; task = removeLast(myShutdownTasks)) {
      //  task can change myShutdownTasks
      try {
        task.run();
      }
      catch (Throwable e) {
        LOG.error(e);
      }
    }

    for (Thread thread = removeLast(myShutdownThreads); thread != null; thread = removeLast(myShutdownThreads)) {
      thread.start();
      try {
        thread.join();
      }
      catch (InterruptedException ignored) {
      }
    }
  }

  public final void ensureStopperThreadsFinished() {
    Thread[] threads = getStopperThreads();
    final long started = System.currentTimeMillis();
    while (threads.length > 0) {
      Thread thread = threads[0];
      if (!thread.isAlive()) {
        if (isRegistered(thread)) {
          LOG.error("Thread '" + thread.getName() + "' did not unregister itself from ShutDownTracker.");
          unregisterStopperThread(thread);
        }
      }
      else {
        final long totalTimeWaited = System.currentTimeMillis() - started;
        if (totalTimeWaited > 3000) {
          // okay, we are waiting fo too long already, lets stop everyone:
          // in some cases, stopper thread may try to run readAction while we are shutting down (under a writeAction) and deadlock.
          thread.interrupt();
        }

        try {
          thread.join(100);
        }
        catch (InterruptedException ignored) {
        }
      }
      threads = getStopperThreads();
    }
  }

  private synchronized boolean isRegistered(@NotNull Thread thread) {
    return myThreads.contains(thread);
  }

  @NotNull
  private synchronized Thread[] getStopperThreads() {
    return myThreads.toArray(new Thread[myThreads.size()]);
  }

  public synchronized void registerStopperThread(@NotNull Thread thread) {
    myThreads.add(thread);
  }

  public synchronized void unregisterStopperThread(@NotNull Thread thread) {
    myThreads.remove(thread);
  }

  public synchronized void registerShutdownThread(@NotNull Thread thread) {
    myShutdownThreads.addLast(thread);
  }

  public synchronized void registerShutdownThread(int index, @NotNull Thread thread) {
    myShutdownThreads.add(index, thread);
  }

  public synchronized void registerShutdownTask(@NotNull Runnable task) {
    myShutdownTasks.addLast(task);
  }

  public synchronized void unregisterShutdownTask(@NotNull Runnable task) {
    myShutdownTasks.remove(task);
  }
  
  private synchronized <T> T removeLast(@NotNull LinkedList<T> list) {
    return list.isEmpty()? null : list.removeLast();
  }

  public static void invokeAndWait(boolean returnOnTimeout, boolean runInEdt, @NotNull final Runnable runnable) {
    if (!runInEdt) {
      if (returnOnTimeout) {
        final Semaphore semaphore = new Semaphore();
        semaphore.down();
        new Thread(new Runnable() {
          @Override
          public void run() {
            runnable.run();
            semaphore.up();
          }
        },"shutdown tracker invoker").start();
        semaphore.waitFor(1000);
      }
      else {
        runnable.run();
      }
      return;
    }

    if (returnOnTimeout) {
      final Semaphore semaphore = new Semaphore();
      semaphore.down();
      SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
          runnable.run();
          semaphore.up();
        }
      });
      semaphore.waitFor(1000);
      return;
    }

    try {
      UIUtil.invokeAndWaitIfNeeded(runnable);
    }
    catch (Exception e) {
      LOG.error(e);
    }
  }
}

@@ -962,110 +962,8 @@
 e %7B%0A
-  private static final Logger LOG = Logger.getInstance(%22#com.intellij.openapi.util.ShutDownTracker%22);%0A
   pr
@@ -1016,32 +1016,32 @@
 List%3CThread%3E();%0A
+
   private final 
@@ -1283,50 +1283,8 @@
 ) %7B%0A
-    //noinspection HardCodedStringLiteral%0A
     
@@ -2038,26 +2038,64 @@
  %7B%0A        L
-OG
+ogger.getInstance(ShutDownTracker.class)
 .error(e);%0A 
@@ -2331,22 +2331,16 @@
 nored) %7B
-%0A     
  %7D%0A    %7D
@@ -2636,26 +2636,64 @@
 %0A          L
-OG
+ogger.getInstance(ShutDownTracker.class)
 .error(%22Thre
@@ -3306,24 +3306,16 @@
 nored) %7B
-%0A       
  %7D%0A     
@@ -3820,32 +3820,108 @@
 e(thread);%0A  %7D%0A%0A
+  /** @deprecated to be removed in IDEA 16 */%0A  @SuppressWarnings(%22unused%22)%0A
   public synchro
@@ -4016,32 +4016,108 @@
 t(thread);%0A  %7D%0A%0A
+  /** @deprecated to be removed in IDEA 16 */%0A  @SuppressWarnings(%22unused%22)%0A
   public synchro
@@ -5001,16 +5001,17 @@
       %7D,
+ 
 %22shutdow
@@ -5244,24 +5244,63 @@
 ore.down();%0A
+      //noinspection SSBasedInspection%0A
       SwingU
@@ -5606,10 +5606,48 @@
    L
-OG
+ogger.getInstance(ShutDownTracker.class)
 .err

correct logging in ShutDownHook