1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.orchestra.lib;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public class _ReentrantLock implements java.io.Serializable
42 {
43 private static final long serialVersionUID = 7373984872572414699L;
44 private static final long WAIT_WARN_MILLIS = 30L * 1000L;
45
46 private final Log log = LogFactory.getLog(_ReentrantLock.class);
47 private transient Thread lockedBy;
48 private int lockCount;
49
50 public void lockInterruptibly() throws InterruptedException
51 {
52 Thread caller = Thread.currentThread();
53 for(;;)
54 {
55 synchronized(this)
56 {
57 if (lockedBy == null)
58 {
59 lockedBy = caller;
60 lockCount = 1;
61 return;
62 }
63
64 if (lockedBy == caller)
65 {
66 ++lockCount;
67 return;
68 }
69
70 long waitedFor;
71 try
72 {
73 if (log.isDebugEnabled())
74 {
75 log.debug("Waiting for lock " + this.toString()
76 + " which is owned by thread "
77 + lockedBy.getName());
78 }
79 long beganAt = System.currentTimeMillis();
80 this.wait(WAIT_WARN_MILLIS);
81 waitedFor = System.currentTimeMillis() - beganAt;
82 }
83 catch(InterruptedException e)
84 {
85 throw e;
86 }
87
88 if (waitedFor >= WAIT_WARN_MILLIS)
89 {
90
91 if (log.isWarnEnabled())
92 {
93 String lockedByName;
94 if (lockedBy != null)
95 {
96 lockedByName = lockedBy.getName();
97 }
98 else
99 {
100
101 lockedByName = "(none)";
102 }
103 log.warn("Waited for longer than " + WAIT_WARN_MILLIS
104 + " milliseconds for access to lock " + this.toString()
105 + " which is locked by thread " + lockedByName);
106 }
107 }
108 }
109 }
110 }
111
112 public void unlock()
113 {
114 Thread caller = Thread.currentThread();
115 synchronized(this)
116 {
117 if (lockedBy != caller)
118 {
119 throw new IllegalStateException("Unlock on lock not owned by caller");
120 }
121
122 --lockCount;
123 if (lockCount == 0)
124 {
125 lockedBy = null;
126 this.notifyAll();
127 }
128 }
129 }
130
131 public boolean isHeldByCurrentThread()
132 {
133 Thread caller = Thread.currentThread();
134 synchronized(this)
135 {
136 return lockedBy == caller;
137 }
138 }
139 }