ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166e/LongMaxUpdater.java
Revision: 1.1
Committed: Tue Aug 2 18:04:12 2011 UTC (12 years, 9 months ago) by dl
Branch: MAIN
Log Message:
Refactor and introduce new forms of update

File Contents

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