-
Notifications
You must be signed in to change notification settings - Fork 33
/
CloseInProgressSynchronizer.java
266 lines (222 loc) · 8.23 KB
/
CloseInProgressSynchronizer.java
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* Copyright (c) 2000, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.messaging.jmq.util.synchronizer;
public class CloseInProgressSynchronizer {
private static final long WAIT_INTERVAL = 15 * 1000L; // 15 seconds
// whether it's closed for operation or not
private boolean closed = false;
private Object closedLock = new Object();
// number of operations in progress
private int inprogressCount = 0;
private Object inprogressLock = new Object();
private Object logger = null;
public CloseInProgressSynchronizer(Object logger) {
this.logger = logger;
}
public void reset() {
synchronized (closedLock) {
closed = false;
closedLock.notifyAll();
}
synchronized (inprogressLock) {
inprogressCount = 0;
inprogressLock.notifyAll();
}
}
/**
* Set closed to true so that no new operation allowed
*/
public void setClosedAndWait(CloseInProgressCallback cb, String waitlogmsg) {
synchronized (closedLock) {
closed = true;
}
if (cb != null) {
cb.beforeWaitAfterSetClosed();
}
synchronized (inprogressLock) {
inprogressLock.notifyAll();
if (inprogressCount == 0) {
return;
}
logInfo(waitlogmsg);
long currtime = System.currentTimeMillis();
long lastlogtime = currtime;
while (inprogressCount > 0) {
try {
if ((currtime - lastlogtime) >= WAIT_INTERVAL) {
logInfo(waitlogmsg);
lastlogtime = currtime;
}
inprogressLock.wait(WAIT_INTERVAL);
if (inprogressCount > 0) {
currtime = System.currentTimeMillis();
}
} catch (Exception e) {
}
}
}
}
/**
* @param timeout in seconds, 0 means no timeout
*
* @throws java.util.concurrent.TimeoutException if wait timed out
* @throws InterruptedException if wait interrupted
*/
public void setClosedAndWaitWithTimeout(CloseInProgressCallback cb, int timeout, String waitlogmsg)
throws InterruptedException, java.util.concurrent.TimeoutException {
if (timeout <= 0) {
setClosedAndWait(cb, waitlogmsg);
return;
}
synchronized (closedLock) {
closed = true;
}
if (cb != null) {
cb.beforeWaitAfterSetClosed();
}
long maxwait = timeout * 1000L;
long waittime = maxwait;
synchronized (inprogressLock) {
inprogressLock.notifyAll();
if (inprogressCount == 0) {
return;
}
logInfo(waitlogmsg);
long currtime = System.currentTimeMillis();
long precurrtime = currtime;
long lastlogtime = 0L;
long totalwaited = 0L;
while (inprogressCount > 0 && (waittime > 0)) {
try {
if ((currtime - lastlogtime) > WAIT_INTERVAL) {
logInfo(waitlogmsg);
lastlogtime = currtime;
}
inprogressLock.wait(waittime);
precurrtime = currtime;
currtime = System.currentTimeMillis();
totalwaited += ((currtime - precurrtime) > 0 ? (currtime - precurrtime) : 0);
if (inprogressCount > 0) {
waittime = maxwait - totalwaited;
}
} catch (InterruptedException e) {
throw e;
}
}
if (inprogressCount > 0) {
throw new java.util.concurrent.TimeoutException("timeout");
}
}
}
/**
* @throws IllegalAccessException if closed
*/
public void checkClosedAndSetInProgress() throws IllegalAccessException {
synchronized (closedLock) {
if (closed) {
throw new IllegalAccessException("closed");
}
setInProgress(true);
}
}
public void setInProgress(boolean flag) {
synchronized (inprogressLock) {
if (flag) {
inprogressCount++;
} else {
inprogressCount--;
}
if (inprogressCount == 0) {
inprogressLock.notifyAll();
}
}
}
/**
* @param timeout timeout in seconds, 0 means no tmieout
* @throws IllegalStateException if closed
* @throws java.util.concurrent.TimeoutException if timeout
* @throws InterruptedException if wait interrupted
*/
public void checkClosedAndSetInProgressWithWait(int timeout, String waitlogmsg)
throws InterruptedException, java.util.concurrent.TimeoutException {
synchronized (closedLock) {
if (closed) {
throw new IllegalStateException("closed");
}
setInProgressWithWait(timeout, waitlogmsg);
}
}
/**
* @throws IllegalStateException if closed
* @throws java.util.concurrent.TimeoutException if timeout
* @throws InterruptedException if wait interrupted
*/
private void setInProgressWithWait(int timeout, String waitlogmsg)
throws InterruptedException, java.util.concurrent.TimeoutException {
synchronized (inprogressLock) {
if (inprogressCount == 0) {
inprogressCount++;
return;
}
long maxwait = timeout * 1000L;
long waittime = ((maxwait <= 0 || maxwait > WAIT_INTERVAL) ? WAIT_INTERVAL : maxwait);
logInfo(waitlogmsg);
long currtime = System.currentTimeMillis();
long precurrtime = currtime;
long lastlogtime = 0L;
long totalwaited = 0L;
while (inprogressCount > 0 && !closed && (waittime > 0)) {
if ((currtime - lastlogtime) > WAIT_INTERVAL) {
logInfo(waitlogmsg);
lastlogtime = currtime;
}
inprogressLock.wait(waittime);
precurrtime = currtime;
currtime = System.currentTimeMillis();
totalwaited += ((currtime - precurrtime) > 0 ? (currtime - precurrtime) : 0);
if (inprogressCount > 0) {
waittime = (maxwait <= 0 ? WAIT_INTERVAL : (maxwait - totalwaited));
}
if (waittime > WAIT_INTERVAL) {
waittime = WAIT_INTERVAL;
}
}
if (closed) {
throw new IllegalStateException("closed");
}
if (inprogressCount == 0) {
inprogressCount++;
return;
}
throw new java.util.concurrent.TimeoutException("timeout");
}
}
public boolean isClosed() {
synchronized (closedLock) {
return closed;
}
}
private void logInfo(String msg) {
if (logger instanceof com.sun.messaging.jmq.util.LoggerWrapper) {
((com.sun.messaging.jmq.util.LoggerWrapper) logger).logInfo(msg, null);
} else if (logger instanceof java.util.logging.Logger) {
((java.util.logging.Logger) logger).log(java.util.logging.Level.INFO, msg);
} else {
System.out.println("INFO: " + msg);
}
}
}