16e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray/*
26e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * Copyright (C) 2015 The Android Open Source Project
36e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray *
46e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
56e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * you may not use this file except in compliance with the License.
66e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * You may obtain a copy of the License at
76e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray *
86e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
96e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray *
106e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
116e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
126e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * See the License for the specific language governing permissions and
146e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray * limitations under the License.
156e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray */
166e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray
176e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffrayimport sun.misc.Unsafe;
186e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffrayimport java.lang.reflect.Field;
196e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray
206e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffraypublic class Main {
216e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray
226e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray  long instanceField;
236e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray  static long myLongField1;
246e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray  static long myLongField2;
256e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray
266e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray  public static void main(String[] args) throws Exception {
276e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    Unsafe unsafe = getUnsafe();
286e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    Main f = new Main();
296e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    long offset = unsafe.objectFieldOffset(Main.class.getDeclaredField("instanceField"));
306e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    getUnsafe(); // spill offset
316e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    long a = myLongField1;
326e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    // We used the hinted register for the low part of b, which is EBX, as requested
336e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    // by the intrinsic below. Allocating EBX for the low part, would put ESP as the high
346e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    // part, and we did not check that ESP was blocked.
356e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    long b = myLongField2;
366e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    unsafe.compareAndSwapLong(f, offset, a, b);
376e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray  }
386e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray
396e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray
406e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray  private static Unsafe getUnsafe() throws Exception {
416e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    Field f = Unsafe.class.getDeclaredField("theUnsafe");
426e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    f.setAccessible(true);
436e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray    return (Unsafe) f.get(null);
446e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray  }
456e9c66e099654b63ed3648bda2daeaf0a862f047Nicolas Geoffray}
46