author | chegar |
Thu, 16 Nov 2017 10:29:18 +0000 | |
branch | http-client-branch |
changeset 55819 | 18e431209168 |
parent 55799 | c71f52f48d97 |
child 55852 | 32f6aefec11e |
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 jdk.incubator.http.internal.common.ExceptionallyCloseable; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.util.LinkedList; |
|
30 |
import java.util.stream.Stream; |
|
31 |
||
32 |
// Each stream has one of these for input. Each Http2Connection has one |
|
33 |
// for output. Can be used blocking or asynchronously. |
|
34 |
||
35 |
public class Queue<T> implements ExceptionallyCloseable { |
|
36 |
||
37 |
private final LinkedList<T> q = new LinkedList<>(); |
|
38 |
private volatile boolean closed = false; |
|
39 |
private volatile Throwable exception = null; |
|
40 |
private Runnable callback; |
|
41 |
private boolean callbackDisabled = false; |
|
42 |
private int waiters; // true if someone waiting |
|
43 |
||
44 |
public synchronized int size() { |
|
45 |
return q.size(); |
|
46 |
} |
|
47 |
||
48 |
// public synchronized boolean tryPut(T obj) throws IOException { |
|
49 |
// if (closed) return false; |
|
50 |
// put(obj); |
|
51 |
// return true; |
|
52 |
// } |
|
53 |
||
54 |
public synchronized void put(T obj) throws IOException { |
|
55 |
if (closed) { |
|
56 |
throw new IOException("stream closed"); |
|
57 |
} |
|
58 |
||
59 |
q.add(obj); |
|
60 |
||
61 |
if (waiters > 0) { |
|
62 |
notifyAll(); |
|
63 |
} |
|
64 |
||
65 |
if (callbackDisabled) { |
|
66 |
return; |
|
67 |
} |
|
68 |
||
69 |
if (q.size() > 0 && callback != null) { |
|
70 |
// Note: calling callback while holding the lock is |
|
71 |
// dangerous and may lead to deadlocks. |
|
72 |
callback.run(); |
|
73 |
} |
|
74 |
} |
|
75 |
||
76 |
// public synchronized void disableCallback() { |
|
77 |
// callbackDisabled = true; |
|
78 |
// } |
|
79 |
||
80 |
// public synchronized void enableCallback() { |
|
81 |
// callbackDisabled = false; |
|
82 |
// while (q.size() > 0) { |
|
83 |
// callback.run(); |
|
84 |
// } |
|
85 |
// } |
|
86 |
||
87 |
// /** |
|
88 |
// * callback is invoked any time put is called where |
|
89 |
// * the Queue was empty. |
|
90 |
// */ |
|
91 |
// public synchronized void registerPutCallback(Runnable callback) { |
|
92 |
// Objects.requireNonNull(callback); |
|
93 |
// this.callback = callback; |
|
94 |
// if (q.size() > 0) { |
|
95 |
// // Note: calling callback while holding the lock is |
|
96 |
// // dangerous and may lead to deadlocks. |
|
97 |
// callback.run(); |
|
98 |
// } |
|
99 |
// } |
|
100 |
||
101 |
@Override |
|
102 |
public synchronized void close() { |
|
103 |
closed = true; |
|
104 |
notifyAll(); |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public synchronized void closeExceptionally(Throwable t) { |
|
109 |
if (exception == null) exception = t; |
|
110 |
else if (t != null && t != exception) { |
|
111 |
if (!Stream.of(exception.getSuppressed()) |
|
112 |
.filter(x -> x == t) |
|
113 |
.findFirst() |
|
114 |
.isPresent()) |
|
115 |
{ |
|
116 |
exception.addSuppressed(t); |
|
117 |
} |
|
118 |
} |
|
119 |
close(); |
|
120 |
} |
|
121 |
||
122 |
public synchronized T take() throws IOException { |
|
123 |
if (closed) { |
|
124 |
throw newIOException("stream closed"); |
|
125 |
} |
|
126 |
try { |
|
127 |
while (q.size() == 0) { |
|
128 |
waiters++; |
|
129 |
wait(); |
|
130 |
if (closed) { |
|
131 |
throw newIOException("Queue closed"); |
|
132 |
} |
|
133 |
waiters--; |
|
134 |
} |
|
135 |
return q.removeFirst(); |
|
136 |
} catch (InterruptedException ex) { |
|
137 |
throw new IOException(ex); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
public synchronized T poll() throws IOException { |
|
142 |
if (closed) { |
|
143 |
throw newIOException("stream closed"); |
|
144 |
} |
|
145 |
||
146 |
if (q.isEmpty()) { |
|
147 |
return null; |
|
148 |
} |
|
149 |
T res = q.removeFirst(); |
|
150 |
return res; |
|
151 |
} |
|
152 |
||
153 |
// public synchronized T[] pollAll(T[] type) throws IOException { |
|
154 |
// T[] ret = q.toArray(type); |
|
155 |
// q.clear(); |
|
156 |
// return ret; |
|
157 |
// } |
|
158 |
||
159 |
// public synchronized void pushback(T v) { |
|
160 |
// q.addFirst(v); |
|
161 |
// } |
|
162 |
||
163 |
// public synchronized void pushbackAll(T[] v) { |
|
164 |
// for (int i=v.length-1; i>=0; i--) { |
|
165 |
// q.addFirst(v[i]); |
|
166 |
// } |
|
167 |
// } |
|
168 |
||
169 |
private IOException newIOException(String msg) { |
|
170 |
if (exception == null) { |
|
171 |
return new IOException(msg); |
|
172 |
} else { |
|
173 |
return new IOException(msg, exception); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
} |