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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
* Copyright (C) 2009, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.util.io;
import java.text.MessageFormat;
import org.eclipse.jgit.internal.JGitText;
/**
* Triggers an interrupt on the calling thread if it doesn't complete a block.
* <p>
* Classes can use this to trip an alarm interrupting the calling thread if it
* doesn't complete a block within the specified timeout. Typical calling
* pattern is:
*
* <pre>
* private InterruptTimer myTimer = ...;
* void foo() {
* try {
* myTimer.begin(timeout);
* // work
* } finally {
* myTimer.end();
* }
* }
* </pre>
* <p>
* An InterruptTimer is not recursive. To implement recursive timers,
* independent InterruptTimer instances are required. A single InterruptTimer
* may be shared between objects which won't recursively call each other.
* <p>
* Each InterruptTimer spawns one background thread to sleep the specified time
* and interrupt the thread which called {@link #begin(int)}. It is up to the
* caller to ensure that the operations within the work block between the
* matched begin and end calls tests the interrupt flag (most IO operations do).
* <p>
* To terminate the background thread, use {@link #terminate()}. If the
* application fails to terminate the thread, it will (eventually) terminate
* itself when the InterruptTimer instance is garbage collected.
*
* @see TimeoutInputStream
*/
public final class InterruptTimer {
private final AlarmState state;
private final AlarmThread thread;
final AutoKiller autoKiller;
/**
* Create a new timer with a default thread name.
*/
public InterruptTimer() {
this("JGit-InterruptTimer"); //$NON-NLS-1$
}
/**
* Create a new timer to signal on interrupt on the caller.
* <p>
* The timer thread is created in the calling thread's ThreadGroup.
*
* @param threadName
* name of the timer thread.
*/
public InterruptTimer(String threadName) {
state = new AlarmState();
autoKiller = new AutoKiller(state);
thread = new AlarmThread(threadName, state);
thread.start();
}
/**
* Arm the interrupt timer before entering a blocking operation.
*
* @param timeout
* number of milliseconds before the interrupt should trigger.
* Must be > 0.
*/
public void begin(int timeout) {
if (timeout <= 0)
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().invalidTimeout, Integer.valueOf(timeout)));
Thread.interrupted();
state.begin(timeout);
}
/**
* Disable the interrupt timer, as the operation is complete.
*/
public void end() {
state.end();
}
/**
* Shutdown the timer thread, and wait for it to terminate.
*/
public void terminate() {
state.terminate();
boolean interrupted = false;
try {
while (true) {
try {
thread.join();
return;
} catch (InterruptedException e) {
interrupted = true;
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
}
static final class AlarmThread extends Thread {
AlarmThread(String name, AlarmState q) {
super(q);
setName(name);
setDaemon(true);
}
}
// The trick here is, the AlarmThread does not have a reference to the
// AutoKiller instance, only the InterruptTimer itself does. Thus when
// the InterruptTimer is GC'd, the AutoKiller is also unreachable and
// can be GC'd. When it gets finalized, it tells the AlarmThread to
// terminate, triggering the thread to exit gracefully.
//
private static final class AutoKiller {
private final AlarmState state;
AutoKiller(AlarmState s) {
state = s;
}
@Override
protected void finalize() throws Throwable {
state.terminate();
}
}
static final class AlarmState implements Runnable {
private Thread callingThread;
private long deadline;
private boolean terminated;
AlarmState() {
callingThread = Thread.currentThread();
}
@Override
public synchronized void run() {
while (!terminated && callingThread.isAlive()) {
try {
if (0 < deadline) {
final long delay = deadline - now();
if (delay <= 0) {
deadline = 0;
callingThread.interrupt();
} else {
wait(delay);
}
} else {
wait(1000);
}
} catch (InterruptedException e) {
// Treat an interrupt as notice to examine state.
}
}
}
synchronized void begin(int timeout) {
if (terminated)
throw new IllegalStateException(JGitText.get().timerAlreadyTerminated);
callingThread = Thread.currentThread();
deadline = now() + timeout;
notifyAll();
}
synchronized void end() {
if (0 == deadline)
Thread.interrupted();
else
deadline = 0;
notifyAll();
}
synchronized void terminate() {
if (!terminated) {
deadline = 0;
terminated = true;
notifyAll();
}
}
private static long now() {
return System.currentTimeMillis();
}
}
}
|