forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoin.java
More file actions
347 lines (304 loc) · 12 KB
/
Copy pathJoin.java
File metadata and controls
347 lines (304 loc) · 12 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 1.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 1.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.eclipse.org/legal/epl-v10.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2008 MenTaLguY <mental@rydia.net>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.util;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.Executor;
public final class Join {
public static final Executor TRIVIAL_EXECUTOR = new Executor() {
public void execute(Runnable command) {
(new Thread(command)).start();
}
};
private final Executor executor;
private final LinkedList[] writes;
private final long asyncMask;
private long mask = 0;
private final Reaction[][] reactionsPerChannel;
public static class Spec {
private ArrayList<ArrayList<Reaction>> reactionsPerChannel = new ArrayList<ArrayList<Reaction>>();
private long asyncMask = 0;
private long mask = 0;
private volatile Reaction[][] cachedReactionsPerChannel = null;
public Spec() {}
public void addReaction(Reaction reaction) {
if ( ( mask & ~asyncMask & reaction.asyncMask ) != 0 ) {
throw new IllegalArgumentException("Cannot use a synchronous channel in a non-head position");
}
if ( ( reaction.mask & ~reaction.asyncMask & asyncMask ) != 0 ) {
throw new IllegalArgumentException("Cannot use an asynchronous channel in the head position of a synchronous reaction");
}
cachedReactionsPerChannel = null;
final int[] indices = reaction.indices;
for ( int i = 0 ; i < indices.length ; i++ ) {
final int index = indices[i];
if ( reactionsPerChannel.size() <= index ) {
reactionsPerChannel.ensureCapacity(index+1);
while ( reactionsPerChannel.size() <= index ) {
reactionsPerChannel.add(null);
}
}
ArrayList<Reaction> reactions = reactionsPerChannel.get(index);
if ( reactions == null ) {
reactions = new ArrayList<Reaction>();
reactionsPerChannel.set(index, reactions);
}
reactions.add(reaction);
}
asyncMask |= reaction.asyncMask;
mask |= reaction.mask;
}
public Join createJoin() {
return createJoin(TRIVIAL_EXECUTOR);
}
private static final Reaction[] EMPTY_REACTIONS = new Reaction[0];
public Join createJoin(final Executor executor) {
if (cachedReactionsPerChannel == null) {
final int length = reactionsPerChannel.size();
final Reaction[][] localReactionsPerChannel = new Reaction[length][];
for ( int i = 0 ; i < length ; ++i ) {
final ArrayList<Reaction> reactions = reactionsPerChannel.get(i);
if ( reactions != null ) {
localReactionsPerChannel[i] = reactions.toArray(EMPTY_REACTIONS);
}
}
cachedReactionsPerChannel = localReactionsPerChannel;
}
return new Join(asyncMask, cachedReactionsPerChannel, executor);
}
}
public static abstract class Reaction {
private final int[] indices;
private final long mask;
private final long asyncMask;
private static int[] toIndices(Enum<?> head, Enum<?>[] channels) {
final int[] indices = new int[channels.length+1];
indices[0] = head.ordinal();
for ( int i = 0 ; i < channels.length ; ++i ) {
indices[i+1] = channels[i].ordinal();
}
return indices;
}
Reaction(Enum<?> head, Enum<?>[] channels, boolean isAsync) {
this(toIndices(head, channels), isAsync);
}
Reaction(int[] indices, boolean isAsync) {
long mask = 0;
for ( int i = 0 ; i < indices.length ; ++i ) {
final int index = indices[i];
if ( index < 0 || index > 63 ) {
throw new IndexOutOfBoundsException();
}
if ( ( mask & ( 1L << index ) ) != 0 ) {
throw new IllegalArgumentException("Duplicate channels in reaction");
}
mask |= 1L << index;
}
this.indices = indices;
this.mask = mask;
if (isAsync) {
this.asyncMask = mask;
} else {
this.asyncMask = mask & ~( 1L << indices[0] );
}
}
abstract void dispatch(Join join, Object[] args);
}
public static abstract class FastReaction extends Reaction {
public FastReaction(int[] indices) {
super(indices.clone(), true);
}
public FastReaction(Enum<?> head, Enum<?> ... channels) {
super(head, channels, true);
}
@Override
void dispatch(final Join join, final Object[] args) {
try {
react(join, args);
} catch (Exception e) {
}
}
public abstract void react(Join join, Object[] args);
}
public static abstract class AsyncReaction extends Reaction {
public AsyncReaction(int[] indices) {
super(indices.clone(), true);
}
public AsyncReaction(Enum<?> head, Enum<?> ... channels) {
super(head, channels, true);
}
@Override
void dispatch(final Join join, final Object[] args) {
final AsyncReaction reaction = this;
join.executor.execute(new Runnable() {
public void run() {
reaction.react(join, args);
}
});
}
public abstract void react(Join join, Object[] args);
}
public static abstract class SyncReaction extends Reaction {
public SyncReaction(int[] indices) {
super(indices.clone(), false);
}
public SyncReaction(Enum<?> head, Enum<?> ... channels) {
super(head, channels, false);
}
@Override
void dispatch(Join join, final Object[] args) {
final Call call = (Call)args[0];
args[0] = call.getMessage();
call.activate(join, this, args);
}
public abstract Object react(Join join, Object[] args);
}
private Join(final long asyncMask, final Reaction[][] reactionsPerChannel, Executor executor) {
final LinkedList[] writes = new LinkedList[reactionsPerChannel.length];
for ( int i = 0 ; i < writes.length ; ++i ) {
if ( reactionsPerChannel[i] != null ) {
writes[i] = new LinkedList();
}
}
this.asyncMask = asyncMask;
this.reactionsPerChannel = reactionsPerChannel;
this.writes = writes;
this.executor = executor;
}
private void sendRaw(int index, Object message) {
Reaction selectedReaction = null;
Object[] args = null;
synchronized (this) {
final LinkedList writing = writes[index];
if ( writing == null ) {
throw new IndexOutOfBoundsException();
}
writing.addLast(message);
mask |= 1L << index;
final Reaction[] reactions = reactionsPerChannel[index];
for (Reaction reaction: reactions) {
if ( ( reaction.mask & mask ) == reaction.mask ) {
final int[] indices = reaction.indices;
args = new Object[indices.length];
for ( int i = 0 ; i < indices.length ; ++i ) {
final int readIndex = indices[i];
final LinkedList reading = writes[readIndex];
args[i] = reading.removeFirst();
if (reading.isEmpty()) {
mask &= ~(1L << readIndex);
}
}
selectedReaction = reaction;
break;
}
}
}
if ( selectedReaction != null ) {
selectedReaction.dispatch(this, args);
}
}
public boolean isAsync(int channel) {
return ( ( 1L << channel ) & asyncMask ) != 0;
}
public void send(int channel, Object message) {
if (isAsync(channel)) {
sendRaw(channel, message);
} else {
sendRaw(channel, new AsyncCall(message));
}
}
public void send(Enum<?> channel, Object message) {
send(channel.ordinal(), message);
}
public Object call(int channel, Object message) {
if (isAsync(channel)) {
sendRaw(channel, message);
return null;
} else {
SyncCall request = new SyncCall(message);
sendRaw(channel, request);
return request.call();
}
}
public Object call(Enum<?> channel, Object message) {
return call(channel.ordinal(), message);
}
private static abstract class Call {
private final Object message;
public Call(Object message) {
this.message = message;
}
public Object getMessage() {
return message;
}
public abstract void activate(Join join, SyncReaction reaction, Object[] args);
}
private static class AsyncCall extends Call {
public AsyncCall(Object message) {
super(message);
}
public void activate(final Join join, final SyncReaction reaction, final Object[] args) {
join.executor.execute(new Runnable() {
public void run() {
reaction.react(join, args);
}
});
}
}
private static class SyncCall extends Call {
private Join join = null;
private SyncReaction reaction = null;
private Object[] args = null;
public SyncCall(Object message) {
super(message);
}
public synchronized void activate(Join join, SyncReaction reaction, Object[] args) {
this.join = join;
this.reaction = reaction;
this.args = args;
notifyAll();
}
public synchronized Object call() {
boolean interrupted = false;
try {
while ( reaction == null ) {
try {
wait();
} catch (InterruptedException e) {
interrupted = true;
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
return reaction.react(join, args);
}
}
}