comparison src/classes/java/util/concurrent/atomic/AtomicIntegerArray.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
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.
17 */
18
19 package java.util.concurrent.atomic;
20
21 import java.io.Serializable;
22 import java.util.Arrays;
23
24 /**
25 * model class for AtomicIntegerArray
26 */
27 public class AtomicIntegerArray implements Serializable {
28 private static final long serialVersionUID = 2862133569453604235L;
29
30 private final int[] array;
31
32 public AtomicIntegerArray(int length) {
33 array = new int[length];
34 // <2do> need a volatile write in order to conform to JMM // Does this really matter in JPF?
35 }
36
37 public AtomicIntegerArray(int[] array) {
38 if (array == null)
39 throw new NullPointerException();
40
41 int length = array.length;
42 this.array = new int[length];
43
44 for (int i = 0; i < length; ++i)
45 this.array[i] = array[i];
46
47 // <2do> need a volatile write in order to conform to JMM // Does this really matter in JPF?
48 }
49
50 public final int length() {
51 return(array.length);
52 }
53
54 public final int get(int i) {
55 checkIndex(i);
56 return(getNative(i));
57 }
58
59 private final native int getNative(int i);
60
61 public final boolean compareAndSet(int i, int expect, int update) {
62 checkIndex(i);
63 return(compareAndSetNative(i, expect, update));
64 }
65
66 private final native boolean compareAndSetNative(int i, int expect, int update);
67
68 public final boolean weakCompareAndSet(int i, int expect, int update) {
69 return(compareAndSet(i, expect, update));
70 }
71
72 public final int getAndSet(int i, int newValue) {
73 while (true) {
74 int current = get(i);
75 if (compareAndSet(i, current, newValue))
76 return(current);
77 }
78 }
79
80 public final void set(int i, int newValue) {
81 getAndSet(i, newValue);
82 }
83
84 public final void lazySet(int i, int newValue) {
85 set(i, newValue);
86 }
87
88 public final int getAndIncrement(int i) {
89 return(getAndAdd(i, 1));
90 }
91
92 public final int getAndDecrement(int i) {
93 return(getAndAdd(i, -1));
94 }
95
96 public final int getAndAdd(int i, int delta) {
97 while (true) {
98 int current = get(i);
99 int next = current + delta;
100 if (compareAndSet(i, current, next))
101 return(current);
102 }
103 }
104
105 public final int incrementAndGet(int i) {
106 return(getAndIncrement(i) + 1);
107 }
108
109 public final int decrementAndGet(int i) {
110 return(getAndDecrement(i) - 1);
111 }
112
113 public final int addAndGet(int i, int delta) {
114 return(getAndAdd(i, delta) + delta);
115 }
116
117 @Override
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));
121 }
122
123 private void checkIndex(int i) {
124 if (i < 0 || i >= array.length)
125 throw new IndexOutOfBoundsException("index " + i);
126 }
127 }