2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package java.util.concurrent.atomic;
21 import java.io.Serializable;
22 import java.util.Arrays;
25 * model class for AtomicLongArray
27 public class AtomicLongArray implements Serializable {
28 private static final long serialVersionUID = -2308431214976778248L;
30 private final long[] array;
32 public AtomicLongArray(int length) {
33 array = new long[length];
34 // <2do> need a volatile write in order to conform to JMM // Does this really matter in JPF?
37 public AtomicLongArray(long[] array) {
39 throw new NullPointerException();
41 int length = array.length;
42 this.array = new long[length];
44 for (int i = 0; i < length; ++i)
45 this.array[i] = array[i];
47 // <2do> need a volatile write in order to conform to JMM // Does this really matter in JPF?
50 public final int length() {
54 public final long get(int i) {
59 private final native long getNative(int i);
61 public final boolean compareAndSet(int i, long expect, long update) {
63 return(compareAndSetNative(i, expect, update));
66 private final native boolean compareAndSetNative(int i, long expect, long update);
68 public final boolean weakCompareAndSet(int i, long expect, long update) {
69 return(compareAndSet(i, expect, update));
72 public final long getAndSet(int i, long newValue) {
74 long current = get(i);
75 if (compareAndSet(i, current, newValue))
80 public final void set(int i, long newValue) {
81 getAndSet(i, newValue);
84 public final void lazySet(int i, long newValue) {
88 public final long getAndIncrement(int i) {
89 return(getAndAdd(i, 1));
92 public final long getAndDecrement(int i) {
93 return(getAndAdd(i, -1));
96 public final long getAndAdd(int i, long delta) {
98 long current = get(i);
99 long next = current + delta;
100 if (compareAndSet(i, current, next))
105 public final long incrementAndGet(int i) {
106 return(getAndIncrement(i) + 1);
109 public final long decrementAndGet(int i) {
110 return(getAndDecrement(i) - 1);
113 public final long addAndGet(int i, long delta) {
114 return(getAndAdd(i, delta) + delta);
118 public String toString() {
119 // <2do> need a volatile read in order to conform to JMM // Does this really matter in JPF?
120 return(Arrays.toString(array));
123 private void checkIndex(int i) {
124 if (i < 0 || i >= array.length)
125 throw new IndexOutOfBoundsException("index " + i);