001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Artem A. Aliev, Andrey Y. Chernyshev, Sergey V. Dmitriev
019: * @version $Revision: 1.1.6.3 $
020: */package org.apache.harmony.util.concurrent;
021:
022: import java.lang.reflect.Field;
023:
024: /**
025: * Allows to atomically update the contents of fields for the specific object. The primary purpose
026: * of this class is to provide the low-level atomic field access operations useful for
027: * implementing the classes from java.util.concurrent.atomic package.
028: *
029: * @see java.util.concurrent.atomic
030: */
031: public final class Atomics {
032:
033: private Atomics() {
034: };
035:
036: /**
037: * Returns offset of the first array's element
038: * @param arrayClass class of the array
039: *
040: * @return offset of the array's first element
041: */
042: public static native int arrayBaseOffset(Class arrayClass);
043:
044: /**
045: * Returns size of the array's element
046: * @param arrayClass class of the array
047: *
048: * @return size of the array's element
049: */
050: public static native int arrayIndexScale(Class arrayClass);
051:
052: /*
053: * Writes new value to the object's field (by given offset) in volatile manner
054: * @param obj object which field needs to be set
055: * @param offset the field offset within the given object
056: * @param value value to set
057: */
058: public static native void setIntVolatile(Object obj, long offset,
059: int value);
060:
061: /*
062: * Reads value from the object's field (by given offset) in volatile manner
063: * @param obj object which field needs to be read
064: * @param offset the field offset within the given object
065: * @return the field's value
066: */
067: public static native int getIntVolatile(Object obj, long offset);
068:
069: /*
070: * Writes new value to the object's field (by given offset) in volatile manner
071: * @param obj object which field needs to be set
072: * @param offset the field offset within the given object
073: * @param value value to set
074: */
075: public static native void setLongVolatile(Object obj, long offset,
076: long value);
077:
078: /*
079: * Reads value from the object's field (by given offset) in volatile manner
080: * @param obj object which field needs to be read
081: * @param offset the field offset within the given object
082: * @return the field's value
083: */
084: public static native long getLongVolatile(Object obj, long offset);
085:
086: /*
087: * Writes new value to the object's field (by given offset) in volatile manner
088: * @param obj object which field needs to be set
089: * @param offset the field offset within the given object
090: * @param value value to set
091: */
092: public static native void setObjectVolatile(Object obj,
093: long offset, Object value);
094:
095: /*
096: * Reads value from the object's field (by given offset) in volatile manner
097: * @param obj object which field needs to be read
098: * @param offset the field offset within the given object
099: * @return the field's value
100: */
101: public static native Object getObjectVolatile(Object obj,
102: long offset);
103:
104: /**
105: * Returns offset of the given field.
106: * @param field the field for which offset is returned
107: *
108: * @return offset of the given field
109: */
110: public static native long getFieldOffset(Field field);
111:
112: /**
113: * Atomically sets an integer field to x if it currently contains the expected value.
114: * @param o object those integer field needs to be set
115: * @param field the field to be set
116: * @param expected expected field value
117: * @param x value to set.
118: *
119: * @return true if the value was set.
120: * False return indicates that the actual value was not equal to the expected value.
121: */
122: public static native boolean compareAndSetInt(Object o,
123: long offset, int expected, int x);
124:
125: /**
126: * Atomically sets a boolean field to x if it currently contains the expected value.
127: * @param o object those boolean field needs to be set
128: * @param field the field to be set
129: * @param expected expected field value
130: * @param x value to set.
131: *
132: * @return true if the value was set.
133: * False return indicates that the actual value was not equal to the expected value.
134: */
135: public static native boolean compareAndSetBoolean(Object o,
136: long offset, boolean expected, boolean x);
137:
138: /**
139: * Atomically sets a long field to x if it currently contains the expected value.
140: * @param o object those long field needs to be set
141: * @param field the field to be set
142: * @param expected expected field value
143: * @param x value to set.
144: *
145: * @return true if the value was set.
146: * False return indicates that the actual value was not equal to the expected value.
147: */
148: public static native boolean compareAndSetLong(Object o,
149: long offset, long expected, long x);
150:
151: /**
152: * Atomically sets a reference type field to x if it currently contains the expected value.
153: * @param o object those reference type field needs to be set
154: * @param field the field to be set
155: * @param expected expected field value
156: * @param x value to set.
157: *
158: * @return true if the value was set.
159: * False return indicates that the actual value was not equal to the expected value.
160: */
161: public static native boolean compareAndSetObject(Object o,
162: long offset, Object expected, Object x);
163:
164: /**
165: * Atomically sets an element within array of integers to x if it currently contains the expected value.
166: * @param arr array those integer element needs to be set
167: * @param index an index within an array
168: * @param expected expected field value
169: * @param x value to set.
170: *
171: * @return true if the value was set.
172: * False return indicates that the actual value was not equal to the expected value.
173: */
174: public static native boolean compareAndSetInt(int[] arr, int index,
175: int expected, int x);
176:
177: /**
178: * Atomically sets an element within array of booleans to x if it currently contains the expected value.
179: * @param arr array those boolean element needs to be set
180: * @param index an index within an array
181: * @param expected expected field value
182: * @param x value to set.
183: *
184: * @return true if the value was set.
185: * False return indicates that the actual value was not equal to the expected value.
186: */
187: public static native boolean compareAndSetBoolean(boolean[] arr,
188: int index, boolean expected, boolean x);
189:
190: /**
191: * Atomically sets an element within array of longs to x if it currently contains the expected value.
192: * @param arr array those long element needs to be set
193: * @param index an index within an array
194: * @param expected expected field value
195: * @param x value to set.
196: *
197: * @return true if the value was set.
198: * False return indicates that the actual value was not equal to the expected value.
199: */
200: public static native boolean compareAndSetLong(long[] arr,
201: int index, long expected, long x);
202:
203: /**
204: * Atomically sets an element within array of objects to x if it currently contains the expected value.
205: * @param arr array those object element needs to be set
206: * @param index an index within an array
207: * @param expected expected field value
208: * @param x value to set.
209: *
210: * @return true if the value was set.
211: * False return indicates that the actual value was not equal to the expected value.
212: */
213: public static native boolean compareAndSetObject(Object[] arr,
214: int index, Object expected, Object x);
215: }
|