1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
|
* Expert Group and released to the public domain, as explained at |
4 |
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package jsr166e; |
8 |
|
|
import java.io.Serializable; |
9 |
|
|
|
10 |
|
|
/** |
11 |
|
|
* One or more variables that together maintain a running {@code long} |
12 |
|
|
* maximum with initial value {@code Long.MIN_VALUE}. When updates |
13 |
|
|
* (method {@link #update}) are contended across threads, the set of |
14 |
|
|
* variables may grow dynamically to reduce contention. Method {@link |
15 |
|
|
* #max} (or, equivalently, {@link #longValue}) returns the current |
16 |
|
|
* maximum across the variables maintaining updates. |
17 |
|
|
* |
18 |
|
|
* <p>This class extends {@link Number}, but does <em>not</em> define |
19 |
jsr166 |
1.5 |
* methods such as {@code equals}, {@code hashCode} and {@code |
20 |
|
|
* compareTo} because instances are expected to be mutated, and so are |
21 |
|
|
* not useful as collection keys. |
22 |
dl |
1.1 |
* |
23 |
|
|
* <p><em>jsr166e note: This class is targeted to be placed in |
24 |
jsr166 |
1.4 |
* java.util.concurrent.atomic.</em> |
25 |
dl |
1.1 |
* |
26 |
jsr166 |
1.2 |
* @since 1.8 |
27 |
dl |
1.1 |
* @author Doug Lea |
28 |
|
|
*/ |
29 |
|
|
public class LongMaxUpdater extends Striped64 implements Serializable { |
30 |
|
|
private static final long serialVersionUID = 7249069246863182397L; |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* Version of max for use in retryUpdate |
34 |
|
|
*/ |
35 |
|
|
final long fn(long v, long x) { return v > x ? v : x; } |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* Creates a new instance with initial maximum of {@code |
39 |
|
|
* Long.MIN_VALUE}. |
40 |
|
|
*/ |
41 |
|
|
public LongMaxUpdater() { |
42 |
|
|
base = Long.MIN_VALUE; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
/** |
46 |
|
|
* Updates the maximum to be at least the given value. |
47 |
|
|
* |
48 |
|
|
* @param x the value to update |
49 |
|
|
*/ |
50 |
|
|
public void update(long x) { |
51 |
|
|
Cell[] as; long b, v; HashCode hc; Cell a; int n; |
52 |
|
|
if ((as = cells) != null || |
53 |
|
|
(b = base) < x && !casBase(b, x)) { |
54 |
|
|
boolean uncontended = true; |
55 |
|
|
int h = (hc = threadHashCode.get()).code; |
56 |
|
|
if (as == null || (n = as.length) < 1 || |
57 |
|
|
(a = as[(n - 1) & h]) == null || |
58 |
|
|
((v = a.value) < x && !(uncontended = a.cas(v, x)))) |
59 |
|
|
retryUpdate(x, hc, uncontended); |
60 |
|
|
} |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/** |
64 |
|
|
* Returns the current maximum. The returned value is |
65 |
jsr166 |
1.6 |
* <em>NOT</em> an atomic snapshot: invocation in the absence of |
66 |
dl |
1.1 |
* concurrent updates returns an accurate result, but concurrent |
67 |
|
|
* updates that occur while the value is being calculated might |
68 |
|
|
* not be incorporated. |
69 |
|
|
* |
70 |
|
|
* @return the maximum |
71 |
|
|
*/ |
72 |
|
|
public long max() { |
73 |
|
|
Cell[] as = cells; |
74 |
|
|
long max = base; |
75 |
|
|
if (as != null) { |
76 |
|
|
int n = as.length; |
77 |
|
|
long v; |
78 |
|
|
for (int i = 0; i < n; ++i) { |
79 |
|
|
Cell a = as[i]; |
80 |
|
|
if (a != null && (v = a.value) > max) |
81 |
|
|
max = v; |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
return max; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* Resets variables maintaining updates to {@code Long.MIN_VALUE}. |
89 |
|
|
* This method may be a useful alternative to creating a new |
90 |
|
|
* updater, but is only effective if there are no concurrent |
91 |
|
|
* updates. Because this method is intrinsically racy, it should |
92 |
|
|
* only be used when it is known that no threads are concurrently |
93 |
|
|
* updating. |
94 |
|
|
*/ |
95 |
|
|
public void reset() { |
96 |
|
|
internalReset(Long.MIN_VALUE); |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
/** |
100 |
|
|
* Equivalent in effect to {@link #max} followed by {@link |
101 |
|
|
* #reset}. This method may apply for example during quiescent |
102 |
|
|
* points between multithreaded computations. If there are |
103 |
|
|
* updates concurrent with this method, the returned value is |
104 |
|
|
* <em>not</em> guaranteed to be the final value occurring before |
105 |
|
|
* the reset. |
106 |
|
|
* |
107 |
|
|
* @return the maximum |
108 |
|
|
*/ |
109 |
|
|
public long maxThenReset() { |
110 |
|
|
Cell[] as = cells; |
111 |
|
|
long max = base; |
112 |
|
|
base = Long.MIN_VALUE; |
113 |
|
|
if (as != null) { |
114 |
|
|
int n = as.length; |
115 |
|
|
for (int i = 0; i < n; ++i) { |
116 |
|
|
Cell a = as[i]; |
117 |
|
|
if (a != null) { |
118 |
|
|
long v = a.value; |
119 |
|
|
a.value = Long.MIN_VALUE; |
120 |
|
|
if (v > max) |
121 |
|
|
max = v; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
} |
125 |
|
|
return max; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
/** |
129 |
|
|
* Returns the String representation of the {@link #max}. |
130 |
jsr166 |
1.3 |
* @return the String representation of the {@link #max} |
131 |
dl |
1.1 |
*/ |
132 |
|
|
public String toString() { |
133 |
|
|
return Long.toString(max()); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
/** |
137 |
|
|
* Equivalent to {@link #max}. |
138 |
|
|
* |
139 |
|
|
* @return the maximum |
140 |
|
|
*/ |
141 |
|
|
public long longValue() { |
142 |
|
|
return max(); |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
/** |
146 |
|
|
* Returns the {@link #max} as an {@code int} after a narrowing |
147 |
|
|
* primitive conversion. |
148 |
|
|
*/ |
149 |
|
|
public int intValue() { |
150 |
|
|
return (int)max(); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
/** |
154 |
|
|
* Returns the {@link #max} as a {@code float} |
155 |
|
|
* after a widening primitive conversion. |
156 |
|
|
*/ |
157 |
|
|
public float floatValue() { |
158 |
|
|
return (float)max(); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
/** |
162 |
|
|
* Returns the {@link #max} as a {@code double} after a widening |
163 |
|
|
* primitive conversion. |
164 |
|
|
*/ |
165 |
|
|
public double doubleValue() { |
166 |
|
|
return (double)max(); |
167 |
|
|
} |
168 |
|
|
|
169 |
|
|
private void writeObject(java.io.ObjectOutputStream s) |
170 |
|
|
throws java.io.IOException { |
171 |
|
|
s.defaultWriteObject(); |
172 |
|
|
s.writeLong(max()); |
173 |
|
|
} |
174 |
|
|
|
175 |
jsr166 |
1.7 |
private void readObject(java.io.ObjectInputStream s) |
176 |
|
|
throws java.io.IOException, ClassNotFoundException { |
177 |
dl |
1.1 |
s.defaultReadObject(); |
178 |
|
|
busy = 0; |
179 |
|
|
cells = null; |
180 |
|
|
base = s.readLong(); |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
} |