author | dfuchs |
Mon, 27 Nov 2017 16:40:01 +0000 | |
branch | http-client-branch |
changeset 55892 | 9f345a976249 |
parent 55852 | 32f6aefec11e |
child 55941 | 2d423c9b73bb |
permissions | -rw-r--r-- |
55799 | 1 |
/* |
55819
18e431209168
http-client-branch: Update copyright years in tests
chegar
parents:
55799
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
55799 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
import java.io.IOException; |
|
27 |
import java.util.LinkedList; |
|
28 |
import java.util.stream.Stream; |
|
29 |
||
30 |
// Each stream has one of these for input. Each Http2Connection has one |
|
31 |
// for output. Can be used blocking or asynchronously. |
|
32 |
||
33 |
public class Queue<T> implements ExceptionallyCloseable { |
|
34 |
||
35 |
private final LinkedList<T> q = new LinkedList<>(); |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
36 |
private boolean closed = false; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
37 |
private boolean closing = false; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
38 |
private Throwable exception = null; |
55799 | 39 |
private Runnable callback; |
40 |
private boolean callbackDisabled = false; |
|
41 |
private int waiters; // true if someone waiting |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
42 |
private final T closeSentinel; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
43 |
|
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
44 |
Queue(T closeSentinel) { |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
45 |
this.closeSentinel = closeSentinel; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
46 |
} |
55799 | 47 |
|
48 |
public synchronized int size() { |
|
49 |
return q.size(); |
|
50 |
} |
|
51 |
||
52 |
public synchronized void put(T obj) throws IOException { |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
53 |
if (closed || closing) { |
55799 | 54 |
throw new IOException("stream closed"); |
55 |
} |
|
56 |
||
57 |
q.add(obj); |
|
58 |
||
59 |
if (waiters > 0) { |
|
60 |
notifyAll(); |
|
61 |
} |
|
62 |
||
63 |
if (callbackDisabled) { |
|
64 |
return; |
|
65 |
} |
|
66 |
||
67 |
if (q.size() > 0 && callback != null) { |
|
68 |
// Note: calling callback while holding the lock is |
|
69 |
// dangerous and may lead to deadlocks. |
|
70 |
callback.run(); |
|
71 |
} |
|
72 |
} |
|
73 |
||
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
74 |
// Other close() variants are immediate and abortive |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
75 |
// This allows whatever is on Q to be processed first. |
55799 | 76 |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
77 |
public synchronized void orderlyClose() { |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
78 |
if (closing || closed) |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
79 |
return; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
80 |
try { |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
81 |
put(closeSentinel); |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
82 |
} catch (IOException e) { |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
83 |
e.printStackTrace(); |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
84 |
} |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
85 |
closing = true; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
86 |
} |
55799 | 87 |
|
88 |
@Override |
|
89 |
public synchronized void close() { |
|
90 |
closed = true; |
|
91 |
notifyAll(); |
|
92 |
} |
|
93 |
||
94 |
@Override |
|
95 |
public synchronized void closeExceptionally(Throwable t) { |
|
96 |
if (exception == null) exception = t; |
|
97 |
else if (t != null && t != exception) { |
|
98 |
if (!Stream.of(exception.getSuppressed()) |
|
99 |
.filter(x -> x == t) |
|
100 |
.findFirst() |
|
101 |
.isPresent()) |
|
102 |
{ |
|
103 |
exception.addSuppressed(t); |
|
104 |
} |
|
105 |
} |
|
106 |
close(); |
|
107 |
} |
|
108 |
||
109 |
public synchronized T take() throws IOException { |
|
110 |
if (closed) { |
|
111 |
throw newIOException("stream closed"); |
|
112 |
} |
|
113 |
try { |
|
114 |
while (q.size() == 0) { |
|
115 |
waiters++; |
|
116 |
wait(); |
|
117 |
if (closed) { |
|
118 |
throw newIOException("Queue closed"); |
|
119 |
} |
|
120 |
waiters--; |
|
121 |
} |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
122 |
T item = q.removeFirst(); |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
123 |
if (item.equals(closeSentinel)) { |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
124 |
closed = true; |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
125 |
assert q.isEmpty(); |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
126 |
} |
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
127 |
return item; |
55799 | 128 |
} catch (InterruptedException ex) { |
129 |
throw new IOException(ex); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
public synchronized T poll() throws IOException { |
|
134 |
if (closed) { |
|
135 |
throw newIOException("stream closed"); |
|
136 |
} |
|
137 |
||
138 |
if (q.isEmpty()) { |
|
139 |
return null; |
|
140 |
} |
|
55852
32f6aefec11e
http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents:
55819
diff
changeset
|
141 |
return take(); |
55799 | 142 |
} |
143 |
||
144 |
private IOException newIOException(String msg) { |
|
145 |
if (exception == null) { |
|
146 |
return new IOException(msg); |
|
147 |
} else { |
|
148 |
return new IOException(msg, exception); |
|
149 |
} |
|
150 |
} |
|
151 |
} |