View Javadoc

1   // Unsafe.java, created Tue Dec 10 14:02:37 2002 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.ClassLib.sun15_win32.sun.misc;
5   
6   import joeq.Memory.HeapAddress;
7   
8   /***
9    * Unsafe
10   *
11   * @author  John Whaley <jwhaley@alum.mit.edu>
12   * @version $Id: Unsafe.java 1451 2004-03-09 06:27:08Z jwhaley $
13   */
14  public final class Unsafe {
15  
16      public boolean compareAndSwapLong(java.lang.Object o, long off, long a, long b) {
17          HeapAddress p = (HeapAddress) HeapAddress.addressOf(o).offset((int)off);
18          // todo: atomic cas8
19          if (p.peek8() == a) {
20              p.poke8(b);
21              return true;
22          } else {
23              return false;
24          }
25      }
26      
27      public boolean compareAndSwapInt(java.lang.Object o, long off, int a, int b) {
28          HeapAddress p = (HeapAddress) HeapAddress.addressOf(o).offset((int)off);
29          // todo: atomic cas4
30          if (p.peek4() == a) {
31              p.poke4(b);
32              return true;
33          } else {
34              return false;
35          }
36      }
37      
38      public boolean compareAndSwapObject(java.lang.Object o, long off, java.lang.Object a, java.lang.Object b) {
39          HeapAddress p = (HeapAddress) HeapAddress.addressOf(o).offset((int)off);
40          // todo: atomic cas4
41          if (p.peek() == HeapAddress.addressOf(a)) {
42              p.poke(HeapAddress.addressOf(b));
43              return true;
44          } else {
45              return false;
46          }
47      }
48  }