138 * Atomically sets the value to the given updated value |
138 * Atomically sets the value to the given updated value |
139 * if the current value {@code ==} the expected value. |
139 * if the current value {@code ==} the expected value. |
140 * |
140 * |
141 * @param expect the expected value |
141 * @param expect the expected value |
142 * @param update the new value |
142 * @param update the new value |
143 * @return true if successful. False return indicates that |
143 * @return {@code true} if successful. False return indicates that |
144 * the actual value was not equal to the expected value. |
144 * the actual value was not equal to the expected value. |
145 */ |
145 */ |
146 public final boolean compareAndSet(long expect, long update) { |
146 public final boolean compareAndSet(long expect, long update) { |
147 return unsafe.compareAndSwapLong(this, valueOffset, expect, update); |
147 return unsafe.compareAndSwapLong(this, valueOffset, expect, update); |
148 } |
148 } |
149 |
149 |
150 /** |
150 /** |
151 * Atomically sets the value to the given updated value |
151 * Atomically sets the value to the given updated value |
152 * if the current value {@code ==} the expected value. |
152 * if the current value {@code ==} the expected value. |
153 * |
153 * |
154 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a> |
154 * <p><a href="package-summary.html#weakCompareAndSet">May fail |
155 * and does not provide ordering guarantees, so is only rarely an |
155 * spuriously and does not provide ordering guarantees</a>, so is |
156 * appropriate alternative to {@code compareAndSet}. |
156 * only rarely an appropriate alternative to {@code compareAndSet}. |
157 * |
157 * |
158 * @param expect the expected value |
158 * @param expect the expected value |
159 * @param update the new value |
159 * @param update the new value |
160 * @return true if successful |
160 * @return {@code true} if successful |
161 */ |
161 */ |
162 public final boolean weakCompareAndSet(long expect, long update) { |
162 public final boolean weakCompareAndSet(long expect, long update) { |
163 return unsafe.compareAndSwapLong(this, valueOffset, expect, update); |
163 return unsafe.compareAndSwapLong(this, valueOffset, expect, update); |
164 } |
164 } |
165 |
165 |
167 * Atomically increments by one the current value. |
167 * Atomically increments by one the current value. |
168 * |
168 * |
169 * @return the previous value |
169 * @return the previous value |
170 */ |
170 */ |
171 public final long getAndIncrement() { |
171 public final long getAndIncrement() { |
172 return getAndAdd(1); |
172 return unsafe.getAndAddLong(this, valueOffset, 1L); |
173 } |
173 } |
174 |
174 |
175 /** |
175 /** |
176 * Atomically decrements by one the current value. |
176 * Atomically decrements by one the current value. |
177 * |
177 * |
178 * @return the previous value |
178 * @return the previous value |
179 */ |
179 */ |
180 public final long getAndDecrement() { |
180 public final long getAndDecrement() { |
181 return getAndAdd(-1); |
181 return unsafe.getAndAddLong(this, valueOffset, -1L); |
182 } |
182 } |
183 |
183 |
184 /** |
184 /** |
185 * Atomically adds the given value to the current value. |
185 * Atomically adds the given value to the current value. |
186 * |
186 * |
195 * Atomically increments by one the current value. |
195 * Atomically increments by one the current value. |
196 * |
196 * |
197 * @return the updated value |
197 * @return the updated value |
198 */ |
198 */ |
199 public final long incrementAndGet() { |
199 public final long incrementAndGet() { |
200 return getAndAdd(1) + 1; |
200 return unsafe.getAndAddLong(this, valueOffset, 1L) + 1L; |
201 } |
201 } |
202 |
202 |
203 /** |
203 /** |
204 * Atomically decrements by one the current value. |
204 * Atomically decrements by one the current value. |
205 * |
205 * |
206 * @return the updated value |
206 * @return the updated value |
207 */ |
207 */ |
208 public final long decrementAndGet() { |
208 public final long decrementAndGet() { |
209 return getAndAdd(-1) - 1; |
209 return unsafe.getAndAddLong(this, valueOffset, -1L) - 1L; |
210 } |
210 } |
211 |
211 |
212 /** |
212 /** |
213 * Atomically adds the given value to the current value. |
213 * Atomically adds the given value to the current value. |
214 * |
214 * |
215 * @param delta the value to add |
215 * @param delta the value to add |
216 * @return the updated value |
216 * @return the updated value |
217 */ |
217 */ |
218 public final long addAndGet(long delta) { |
218 public final long addAndGet(long delta) { |
219 return getAndAdd(delta) + delta; |
219 return unsafe.getAndAddLong(this, valueOffset, delta) + delta; |
220 } |
220 } |
221 |
221 |
222 /** |
222 /** |
223 * Atomically updates the current value with the results of |
223 * Atomically updates the current value with the results of |
224 * applying the given function, returning the previous value. The |
224 * applying the given function, returning the previous value. The |