Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15f149575c1d22cbbbe1face8dede8a0c5dde81a (plain) (blame)
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
/***************************************************************************
 * Copyright (c) 2004-2006 Eike Stepper, Fuggerstr. 39, 10777 Berlin, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.thread;


import org.eclipse.net4j.util.ImplementationError;
import org.eclipse.net4j.util.ThreadInterruptedException;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


public final class DeadlockDetector
{
  public static boolean DETECTION = false;

  public static final boolean COMPLETE_TRACE = true;

  protected static final HashMap locks = new HashMap();

  public static void preLock()
  {
    if (DETECTION)
    {
      Object old = locks.put(Thread.currentThread(), identifySource());
      if (old != null) throw new ImplementationError("Don't nest deadlock detection!");
    }
  }

  public static void postLock()
  {
    if (DETECTION) locks.remove(Thread.currentThread());
  }

  public static void sleep(long millis) throws ThreadInterruptedException
  {
    preLock();

    try
    {
      Thread.sleep(millis);
    }
    catch (InterruptedException ex)
    {
      throw new ThreadInterruptedException(ex);
    }
    finally
    {
      postLock();
    }
  }

  public static void wait(Object object) throws ThreadInterruptedException
  {
    wait(object, 0);
  }

  public static void wait(Object object, long timeout) throws ThreadInterruptedException
  {
    preLock();

    try
    {
      object.wait(timeout);
    }
    catch (InterruptedException ex)
    {
      throw new ThreadInterruptedException(ex);
    }
    finally
    {
      postLock();
    }
  }

  public static void dump()
  {
    System.out.println();
    System.out.println("Deadlock Detection Dump");
    System.out.println("=====================================================================");

    Map.Entry[] array = (Map.Entry[]) locks.entrySet().toArray(new Map.Entry[locks.size()]);
    for (int i = 0; i < array.length; i++)
    {
      Entry entry = array[i];
      Thread key = (Thread) entry.getKey();
      String val = (String) entry.getValue();
      System.out.println("Lock in " + key + "\n" + val);
    }

    System.out.println("=====================================================================");
    System.out.println();
    //    locks.clear();
  }

  public static String identifySource()
  {
    class SourceIdentificationException extends Exception
    {
      private static final long serialVersionUID = 1L;
    }

    try
    {
      throw new SourceIdentificationException();
    }
    catch (SourceIdentificationException ex)
    {
      String ignore = DeadlockDetector.class.getName();
      StackTraceElement[] frames = ex.getStackTrace();

      for (int i = 0; i < frames.length; i++)
      {
        if (!frames[i].getClassName().equals(ignore))
        {
          if (!COMPLETE_TRACE) return frames[i].toString();

          StringBuffer result = new StringBuffer();
          for (int j = i; j < frames.length; j++)
          {
            result.append("\tat " + frames[j].toString() + "\n");
          }
          return result.toString();
        }
      }

      throw new ImplementationError("identifySource() must not be called from inside the class "
          + ignore);
    }
  }
}

Back to the top