author | chegar |
Thu, 29 Mar 2018 20:18:43 +0100 | |
branch | http-client-branch |
changeset 56370 | 86f739cd4cc5 |
parent 56252 | e4b05854c51f |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56088
38fac6d0521d
http-client-branch: update copyright year ranges in prep for file moves
chegar
parents:
48083
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 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 |
||
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http.common; |
48083 | 27 |
|
28 |
import java.util.concurrent.atomic.AtomicLong; |
|
29 |
||
30 |
/** |
|
31 |
* Encapsulates operations with demand (Reactive Streams). |
|
32 |
* |
|
33 |
* <p> Demand is the aggregated number of elements requested by a Subscriber |
|
34 |
* which is yet to be delivered (fulfilled) by the Publisher. |
|
35 |
*/ |
|
36 |
public final class Demand { |
|
37 |
||
38 |
private final AtomicLong val = new AtomicLong(); |
|
39 |
||
40 |
/** |
|
41 |
* Increases this demand by the specified positive value. |
|
42 |
* |
|
43 |
* @param n |
|
44 |
* increment {@code > 0} |
|
45 |
* |
|
46 |
* @return {@code true} iff prior to this operation this demand was fulfilled |
|
47 |
*/ |
|
48 |
public boolean increase(long n) { |
|
49 |
if (n <= 0) { |
|
50 |
throw new IllegalArgumentException(String.valueOf(n)); |
|
51 |
} |
|
52 |
long prev = val.getAndAccumulate(n, (p, i) -> p + i < 0 ? Long.MAX_VALUE : p + i); |
|
53 |
return prev == 0; |
|
54 |
} |
|
55 |
||
56 |
/** |
|
56370
86f739cd4cc5
http-client-branch: review comment - typo in demand docs
chegar
parents:
56252
diff
changeset
|
57 |
* Increases this demand by 1 but only if it is fulfilled. |
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56092
diff
changeset
|
58 |
* @return true if the demand was increased, false otherwise. |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56092
diff
changeset
|
59 |
*/ |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56092
diff
changeset
|
60 |
public boolean increaseIfFulfilled() { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56092
diff
changeset
|
61 |
return val.compareAndSet(0, 1); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56092
diff
changeset
|
62 |
} |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56092
diff
changeset
|
63 |
|
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56092
diff
changeset
|
64 |
/** |
48083 | 65 |
* Tries to decrease this demand by the specified positive value. |
66 |
* |
|
67 |
* <p> The actual value this demand has been decreased by might be less than |
|
68 |
* {@code n}, including {@code 0} (no decrease at all). |
|
69 |
* |
|
70 |
* @param n |
|
71 |
* decrement {@code > 0} |
|
72 |
* |
|
73 |
* @return a value {@code m} ({@code 0 <= m <= n}) this demand has been |
|
74 |
* actually decreased by |
|
75 |
*/ |
|
76 |
public long decreaseAndGet(long n) { |
|
77 |
if (n <= 0) { |
|
78 |
throw new IllegalArgumentException(String.valueOf(n)); |
|
79 |
} |
|
80 |
long p, d; |
|
81 |
do { |
|
82 |
d = val.get(); |
|
83 |
p = Math.min(d, n); |
|
84 |
} while (!val.compareAndSet(d, d - p)); |
|
85 |
return p; |
|
86 |
} |
|
87 |
||
88 |
/** |
|
89 |
* Tries to decrease this demand by {@code 1}. |
|
90 |
* |
|
91 |
* @return {@code true} iff this demand has been decreased by {@code 1} |
|
92 |
*/ |
|
93 |
public boolean tryDecrement() { |
|
94 |
return decreaseAndGet(1) == 1; |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* @return {@code true} iff there is no unfulfilled demand |
|
99 |
*/ |
|
100 |
public boolean isFulfilled() { |
|
101 |
return val.get() == 0; |
|
102 |
} |
|
103 |
||
104 |
/** |
|
105 |
* Resets this object to its initial state. |
|
106 |
*/ |
|
107 |
public void reset() { |
|
108 |
val.set(0); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
112 |
* Returns the current value of this demand. |
|
113 |
* |
|
114 |
* @return the current value of this demand |
|
115 |
*/ |
|
116 |
public long get() { |
|
117 |
return val.get(); |
|
118 |
} |
|
119 |
||
120 |
@Override |
|
121 |
public String toString() { |
|
122 |
return String.valueOf(val.get()); |
|
123 |
} |
|
124 |
} |