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: package sun.misc;
019:
020: import java.lang.reflect.Field;
021: import org.apache.harmony.util.concurrent.Atomics;
022:
023: /**
024: * <p>The Unsafe service.</p>
025: *
026: */
027: public class Unsafe {
028:
029: private static final Unsafe INSTANCE = new Unsafe();
030:
031: private Unsafe() {
032: }
033:
034: /**
035: * <p>
036: * Retrieves an instance of this service.
037: * </p>
038: *
039: * @return An instance of Unsafe.
040: */
041: public static Unsafe getUnsafe() {
042: return INSTANCE;
043: }
044:
045: /**
046: * <p>
047: * Retrieves the offset value of the {@link Field} for use by other methods
048: * in this class.
049: * </p>
050: *
051: * @param field The {@link Field} to retrieve the offset for.
052: * @return The offset value.
053: */
054: public long objectFieldOffset(Field field) {
055: return Atomics.getFieldOffset(field);
056: }
057:
058: /**
059: * <p>
060: * Compares and swaps the value of an int-typed field on an Object instance.
061: * </p>
062: *
063: * @param object The instance containing the field.
064: * @param fieldOffset The offset value of the field.
065: * @param expected The expected value of the field.
066: * @param update The new value to write to the field.
067: * @return <code>true</code> if the field was updated, <code>false</code>
068: * otherwise.
069: */
070: public boolean compareAndSwapInt(Object object, long fieldOffset,
071: int expected, int update) {
072: return Atomics.compareAndSetInt(object, fieldOffset, expected,
073: update);
074: }
075:
076: /**
077: * <p>
078: * Compares and swaps the value of a long-typed field on an Object instance.
079: * </p>
080: *
081: * @param object The instance containing the field.
082: * @param fieldOffset The offset value of the field.
083: * @param expected The expected value of the field.
084: * @param update The new value to write to the field.
085: * @return <code>true</code> if the field was updated, <code>false</code>
086: * otherwise.
087: */
088: public boolean compareAndSwapLong(Object object, long fieldOffset,
089: long expected, long update) {
090: return Atomics.compareAndSetLong(object, fieldOffset, expected,
091: update);
092: }
093:
094: /**
095: * <p>
096: * Compares and swaps the value of an Object-typed field on an Object
097: * instance.
098: * </p>
099: *
100: * @param object The instance containing the field.
101: * @param fieldOffset The offset value of the field.
102: * @param expected The expected value of the field.
103: * @param update The new value to write to the field.
104: * @return <code>true</code> if the field was updated, <code>false</code>
105: * otherwise.
106: */
107: public boolean compareAndSwapObject(Object object,
108: long fieldOffset, Object expected, Object update) {
109: return Atomics.compareAndSetObject(object, fieldOffset,
110: expected, update);
111: }
112:
113: /**
114: * <p>
115: * Retrieves the base offset for the array Class given. The Class passed
116: * MUST me any array type, such that the method {@link Class#isArray()}
117: * returns <code>true</code>. For example, <code>int[].class</code>.
118: * </p>
119: *
120: * @param clazz The array Class object.
121: * @return The base offset value.
122: */
123: public int arrayBaseOffset(Class<?> clazz) {
124: return Atomics.arrayBaseOffset(clazz);
125: }
126:
127: /**
128: * <p>
129: * Retrieves the array index scale for the array Class given. The index
130: * scale is the value used to determine the offset of a particular element
131: * in the array given the array's base offset and an index. The following
132: * code snippet illustrates the usage.
133: * </p>
134: *
135: * <pre>
136: * int base = unsafe.arrayBaseOffset(int[].class);
137: *
138: * int scale = unsafe.arrayIndexScale(int[].class);
139: *
140: * int elementIdx = 1;
141: *
142: * int[] array = { 0, 1, 2 };
143: *
144: * long offsetForIdx = base + (elementIdx * scale);
145: * </pre>
146: *
147: * <p>
148: * The Class passed MUST me any array type, such that the method
149: * {@link Class#isArray()} returns <code>true</code>. For example,
150: * <code>int[].class</code>.
151: * </p>
152: *
153: * @param clazz The array Class object.
154: * @return The index scale value.
155: */
156: public int arrayIndexScale(Class<?> clazz) {
157: return Atomics.arrayIndexScale(clazz);
158: }
159:
160: /**
161: * <p>
162: * Writes an int value to an Object's field as though it were declared
163: * <code>volatile</code>.
164: * </p>
165: *
166: * @param object The instance containing the field to write to.
167: * @param fieldOffset The offset of the field to write to.
168: * @param newValue The value to write.
169: */
170: public void putIntVolatile(Object object, long fieldOffset,
171: int newValue) {
172: Atomics.setIntVolatile(object, fieldOffset, newValue);
173: }
174:
175: /**
176: * <p>
177: * Reads an int value from an Object's field as though it were declared
178: * <code>volatile</code>.
179: * </p>
180: *
181: * @param object The instance containing the field to read from.
182: * @param fieldOffset The offset of the field to read from.
183: * @return The value that was read.
184: */
185: public int getIntVolatile(Object object, long fieldOffset) {
186: return Atomics.getIntVolatile(object, fieldOffset);
187: }
188:
189: /**
190: * <p>
191: * Writes a long value to an Object's field as though it were declared
192: * <code>volatile</code>.
193: * </p>
194: *
195: * @param object The instance containing the field to write to.
196: * @param fieldOffset The offset of the field to write to.
197: * @param newValue The value to write.
198: */
199: public void putLongVolatile(Object object, long fieldOffset,
200: long newValue) {
201: Atomics.setLongVolatile(object, fieldOffset, newValue);
202: }
203:
204: /**
205: * <p>
206: * Reads a long value from an Object's field as though it were declared
207: * <code>volatile</code>.
208: * </p>
209: *
210: * @param object The instance containing the field to read from.
211: * @param fieldOffset The offset of the field to read from.
212: * @return The value that was read.
213: */
214: public long getLongVolatile(Object object, long fieldOffset) {
215: return Atomics.getLongVolatile(object, fieldOffset);
216: }
217:
218: /**
219: * <p>
220: * Writes an Object reference value to an Object's field as though it were
221: * declared <code>volatile</code>.
222: * </p>
223: *
224: * @param object The instance containing the field to write to.
225: * @param fieldOffset The offset of the field to write to.
226: * @param newValue The value to write.
227: */
228: public void putObjectVolatile(Object object, long fieldOffset,
229: Object newValue) {
230: Atomics.setObjectVolatile(object, fieldOffset, newValue);
231: }
232:
233: /**
234: * <p>
235: * Writes an int value to an Object's field as though it were declared
236: * <code>volatile</code>.
237: * </p>
238: *
239: * @param object The instance containing the field to write to.
240: * @param fieldOffset The offset of the field to write to.
241: * @param newValue The value to write.
242: */
243: public Object getObjectVolatile(Object object, long fieldOffset) {
244: return Atomics.getObjectVolatile(object, fieldOffset);
245: }
246:
247: /**
248: * <p>
249: * Writes a long value to an Object's field.
250: * </p>
251: *
252: * @param object The instance containing the field to write to.
253: * @param fieldOffset The offset of the field to write to.
254: * @param newValue The value to write.
255: */
256: public void putLong(Object object, long fieldOffset, long newValue) {
257: throw new UnsupportedOperationException("Not Yet Implemented");
258: }
259:
260: /**
261: * <p>
262: * Reads a long value from an Object's field.
263: * </p>
264: *
265: * @param object The instance containing the field to read from.
266: * @param fieldOffset The offset of the field to read from.
267: * @return The value that was read.
268: */
269: public long getLong(Object object, long fieldOffset) {
270: throw new UnsupportedOperationException("Not Yet Implemented");
271: }
272:
273: /**
274: * <p>
275: * Unparks the {@link Thread} that's passed.
276: * </p>
277: *
278: * @param thread The {@link Thread} to unpark.
279: */
280: public void unpark(Thread thread) {
281: throw new UnsupportedOperationException("Not Yet Implemented");
282: //LockSupport.unpark(thread);
283: }
284:
285: /**
286: * <p>
287: * Parks the {@link Thread#currentThread() current thread} either for a set
288: * number of nanoseconds or until a future point in time.
289: * </p>
290: *
291: * @param timestamp If <code>true</code> <code>nanosOrTimestamp</code>
292: * should be consider as a timestamp based on
293: * {@link System#currentTimeMillis()}. If <code>false</code>,
294: * then <code>nanosOrTimestamp</code> should be considered as a
295: * relative number of nanoseconds from when this method was called;
296: * the value <code>0L</code> can be used in conjunction with this
297: * to indicate that time is not a factor when parking the thread.
298: * @param nanosOrTimestamp Either a relative number of nanoseconds or a
299: * timestamp in milliseconds as defined by
300: * {@link System#currentTimeMillis()}.
301: */
302: public void park(boolean timestamp, long nanosOrTimestamp) {
303: throw new UnsupportedOperationException("Not Yet Implemented");
304: /*if (timestamp) {
305: LockSupport.parkUntil(nanosOrTimestamp);
306: } else {
307: LockSupport.parkNanos(nanosOrTimestamp);
308: }*/
309: }
310: }
|