changes to MGC class library
[IRC.git] / Robust / src / ClassLibrary / MGC / gnu / AtomicInteger.java
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/licenses/publicdomain
5  */
6
7 /*package java.util.concurrent.atomic;
8 import sun.misc.Unsafe;*/
9
10 /**
11  * An <tt>int</tt> value that may be updated atomically.  See the
12  * {@link java.util.concurrent.atomic} package specification for
13  * description of the properties of atomic variables. An
14  * <tt>AtomicInteger</tt> is used in applications such as atomically
15  * incremented counters, and cannot be used as a replacement for an
16  * {@link java.lang.Integer}. However, this class does extend
17  * <tt>Number</tt> to allow uniform access by tools and utilities that
18  * deal with numerically-based classes.
19  *
20  * @since 1.5
21  * @author Doug Lea
22 */
23 public class AtomicInteger /*extends Number*/ implements /*java.io.*/Serializable {
24     private static final long serialVersionUID = 6214790243416807050L;
25
26     // setup to use Unsafe.compareAndSwapInt for updates
27     /*private static final Unsafe unsafe = Unsafe.getUnsafe();
28     private static final long valueOffset;
29
30     static {
31       try {
32         valueOffset = unsafe.objectFieldOffset
33             (AtomicInteger.class.getDeclaredField("value"));
34       } catch (Exception ex) { throw new Error(ex); }
35     }*/
36
37     private volatile int value;
38
39     /**
40      * Creates a new AtomicInteger with the given initial value.
41      *
42      * @param initialValue the initial value
43      */
44     public AtomicInteger(int initialValue) {
45         value = initialValue;
46     }
47
48     /**
49      * Creates a new AtomicInteger with initial value <tt>0</tt>.
50      */
51     public AtomicInteger() {
52     }
53
54     /**
55      * Gets the current value.
56      *
57      * @return the current value
58      */
59     public final int get() {
60         return value;
61     }
62
63     /**
64      * Sets to the given value.
65      *
66      * @param newValue the new value
67      */
68     public final void set(int newValue) {
69         value = newValue;
70     }
71
72     /**
73      * Eventually sets to the given value.
74      *
75      * @param newValue the new value
76      * @since 1.6
77      */
78     public final void lazySet(int newValue) {
79         synchronized (this) {
80             value = newValue;
81         }
82         //unsafe.putOrderedInt(this, valueOffset, newValue);
83     }
84
85     /**
86      * Atomically sets to the given value and returns the old value.
87      *
88      * @param newValue the new value
89      * @return the previous value
90      */
91     public final int getAndSet(int newValue) {
92         for (;;) {
93             int current = get();
94             if (compareAndSet(current, newValue))
95                 return current;
96         }
97     }
98
99     /**
100      * Atomically sets the value to the given updated value
101      * if the current value <tt>==</tt> the expected value.
102      *
103      * @param expect the expected value
104      * @param update the new value
105      * @return true if successful. False return indicates that
106      * the actual value was not equal to the expected value.
107      */
108     public final boolean compareAndSet(int expect, int update) {
109         synchronized (this) {
110             if(expect == value) {
111                 value = update;
112                 return true;
113             } else {
114                 return false;
115             }
116         }
117       //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
118     }
119
120     /**
121      * Atomically sets the value to the given updated value
122      * if the current value <tt>==</tt> the expected value.
123      * May fail spuriously and does not provide ordering guarantees,
124      * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
125      *
126      * @param expect the expected value
127      * @param update the new value
128      * @return true if successful.
129      */
130     public final boolean weakCompareAndSet(int expect, int update) {
131         synchronized (this) {
132             if(expect == value) {
133                 value = update;
134                 return true;
135             } else {
136                 return false;
137             }
138         }
139       //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
140     }
141
142     /**
143      * Atomically increments by one the current value.
144      *
145      * @return the previous value
146      */
147     public final int getAndIncrement() {
148         for (;;) {
149             int current = get();
150             int next = current + 1;
151             if (compareAndSet(current, next))
152                 return current;
153         }
154     }
155
156     /**
157      * Atomically decrements by one the current value.
158      *
159      * @return the previous value
160      */
161     public final int getAndDecrement() {
162         for (;;) {
163             int current = get();
164             int next = current - 1;
165             if (compareAndSet(current, next))
166                 return current;
167         }
168     }
169
170     /**
171      * Atomically adds the given value to the current value.
172      *
173      * @param delta the value to add
174      * @return the previous value
175      */
176     public final int getAndAdd(int delta) {
177         for (;;) {
178             int current = get();
179             int next = current + delta;
180             if (compareAndSet(current, next))
181                 return current;
182         }
183     }
184
185     /**
186      * Atomically increments by one the current value.
187      *
188      * @return the updated value
189      */
190     public final int incrementAndGet() {
191         for (;;) {
192             int current = get();
193             int next = current + 1;
194             if (compareAndSet(current, next))
195                 return next;
196         }
197     }
198
199     /**
200      * Atomically decrements by one the current value.
201      *
202      * @return the updated value
203      */
204     public final int decrementAndGet() {
205         for (;;) {
206             int current = get();
207             int next = current - 1;
208             if (compareAndSet(current, next))
209                 return next;
210         }
211     }
212
213     /**
214      * Atomically adds the given value to the current value.
215      *
216      * @param delta the value to add
217      * @return the updated value
218      */
219     public final int addAndGet(int delta) {
220         for (;;) {
221             int current = get();
222             int next = current + delta;
223             if (compareAndSet(current, next))
224                 return next;
225         }
226     }
227
228     /**
229      * Returns the String representation of the current value.
230      * @return the String representation of the current value.
231      */
232     /*public String toString() {
233         return Integer.toString(get());
234     }*/
235
236
237     public int intValue() {
238         return get();
239     }
240
241     public long longValue() {
242         return (long)get();
243     }
244
245     public float floatValue() {
246         return (float)get();
247     }
248
249     public double doubleValue() {
250         return (double)get();
251     }
252
253 }