(I'm posting this to classpath-patches, too)

Attached is a first attempt at implementing sun.misc.Unsafe for JamVM. This is used by the JSR166 code, java.util.concurrent. Some stuff obviously doesn't work (atomic operations on longs, for one; also, somehow jamvm can't find the `unpark' method when I try it). I think the integer and reference methods are correct, but I haven't run any tests that confirm atomicity. There are probably also endian issues that I'm ignoring.

Anyway, here it is; hopefully this is a step in the right direction.

Thanks.

? rifle-range
? unsafe.patch.txt
? lib/sun
? src/os/darwin/i386/Makefile.in
Index: configure.ac
===================================================================
RCS file: /cvsroot/jamvm/jamvm/configure.ac,v
retrieving revision 1.9
diff -u -r1.9 configure.ac
--- configure.ac        22 Mar 2006 04:27:00 -0000      1.9
+++ configure.ac        16 Jul 2006 08:03:34 -0000
@@ -70,7 +70,7 @@
 powerpc*-*-openbsd*) host_cpu=powerpc host_os=linux libdl_needed=no ;;
 powerpc*-*-freebsd*) host_cpu=powerpc host_os=linux libdl_needed=no ;;
 powerpc-*-darwin*) host_os=darwin ;;
-i[[456]]86-*-darwin*) host_cpu=i386 host_os=darwin ;;
+i[[3456]]86-*-darwin*) host_cpu=i386 host_os=darwin ;;
 *) AC_MSG_ERROR($host not supported) ;;
 esac
 
@@ -224,7 +224,9 @@
     lib/java/lang/reflect/Makefile \
     lib/java/security/Makefile \
     lib/gnu/Makefile \
-    lib/gnu/classpath/Makefile)
+    lib/gnu/classpath/Makefile
+    lib/sun/Makefile \
+    lib/sun/misc/Makefile)
 
 AC_OUTPUT
 
Index: lib/Makefile.am
===================================================================
RCS file: /cvsroot/jamvm/jamvm/lib/Makefile.am,v
retrieving revision 1.4
diff -u -r1.4 Makefile.am
--- lib/Makefile.am     18 Jan 2006 10:13:03 -0000      1.4
+++ lib/Makefile.am     16 Jul 2006 08:03:34 -0000
@@ -1,7 +1,7 @@
 @use_zip_yes@ GLIBJ_ZIP = 
${with_classpath_install_dir}/share/classpath/glibj.zip
 @use_zip_no@ GLIBJ_ZIP = ${with_classpath_install_dir}/share/classpath/
 
-SUBDIRS = jamvm java gnu
+SUBDIRS = jamvm java gnu sun
 EXTRA_DIST = classes.zip README
 
 noinst_DATA = inst_classes.zip
@@ -17,7 +17,8 @@
              $(srcdir)/java/lang/reflect/Method.java \
              $(srcdir)/java/security/VMAccessController.java \
              $(srcdir)/gnu/classpath/VMSystemProperties.java \
-             $(srcdir)/gnu/classpath/VMStackWalker.java
+             $(srcdir)/gnu/classpath/VMStackWalker.java \
+            $(srcdir)/sun/misc/Unsafe.java
 
 
 inst_classes.zip: classes.zip
Index: src/natives.c
===================================================================
RCS file: /cvsroot/jamvm/jamvm/src/natives.c,v
retrieving revision 1.14
diff -u -r1.14 natives.c
--- src/natives.c       10 Apr 2006 01:09:26 -0000      1.14
+++ src/natives.c       16 Jul 2006 08:03:35 -0000
@@ -926,6 +926,411 @@
     return ostack;
 }
 
+/* sun.misc.Unsafe */
+
+#define FIELD_MASK 0x80000000
+
+/* Class method objectFieldOffset(Ljava/lang/reflect/Field;)J */
+uintptr_t *unsafe_objectFieldOffset(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *field = (Object *) ostack[1];
+    FieldBlock *fb = findField(field->class, "slot", "I");
+    *ostack++ = INST_DATA(field)[fb->offset];
+    *ostack++ = FIELD_MASK;
+    return ostack;
+}
+
+/* Class method compareAndSwapInt(Ljava/lang/Object;JII)Z */
+uintptr_t *unsafe_compareAndSwapInt(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int expect = (int) ostack[4];
+    int update = (int) ostack[5];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        *ostack++ = COMPARE_AND_SWAP(&(INST_DATA(obj)[fb->offset]), expect, 
update);
+    }
+    else
+    {
+        *ostack++ = COMPARE_AND_SWAP((&(((uintptr_t *) 
ARRAY_DATA(obj))[offset])), expect, update);
+    }
+    return ostack;
+}
+
+/* Class method compareAndSwapLong(Ljava/lang/Object;JJJ)Z */
+uintptr_t *unsafe_compareAndSwapLong(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    /* XXX should figure out which architectures can support 8-byte
+       compare-and-swap */
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int expect0 = (int) ostack[4];
+    int expect1 = (int) ostack[5];
+    int update0 = (int) ostack[6];
+    int update1 = (int) ostack[7];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    
+    objectLock(obj);
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        if (INST_DATA(obj)[fb->offset] == expect0
+            && INST_DATA(obj)[fb->offset + 1] == expect1)
+        {
+            INST_DATA(obj)[fb->offset] = update0;
+            INST_DATA(obj)[fb->offset + 1] = update1;
+            *ostack++ = TRUE;
+        }
+        else
+            *ostack++ = FALSE;
+    }
+    else
+    {
+        if (((uintptr_t *) ARRAY_DATA(obj))[offset] == expect0
+            && ((uintptr_t *) ARRAY_DATA(obj))[offset] == expect1)
+        {
+            ((uintptr_t *) ARRAY_DATA(obj))[offset] = update0;
+            ((uintptr_t *) ARRAY_DATA(obj))[offset + 1] = update1;
+            *ostack++ = TRUE;
+        }
+        else
+            *ostack++ = FALSE;
+    }
+    objectUnlock(obj);
+    return ostack;
+}
+
+/* Class method 
compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z 
*/
+uintptr_t *unsafe_compareAndSwapObject(Class *class, MethodBlock *mb, 
uintptr_t *ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    Object *expect = (Object *) ostack[4];
+    Object *update = (Object *) ostack[5];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        *ostack++ = COMPARE_AND_SWAP(&(INST_DATA(obj)[fb->offset]), expect, 
update);
+    }
+    else
+    {
+        *ostack++ = COMPARE_AND_SWAP(&(((uintptr_t *) 
ARRAY_DATA(obj))[offset]), expect, update);
+    }
+    return ostack;
+}
+
+/* Class method putOrederedInt(Ljava/lang/Object;JI)V */
+uintptr_t *unsafe_putOrderedInt(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int value = (int) ostack[4];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        ATOMIC_WRITE((&(INST_DATA(obj)[fb->offset])), value);
+    }
+    else
+    {
+        ATOMIC_WRITE((&(((uintptr_t *) ARRAY_DATA(obj))[offset])), value);
+    }
+    return ostack;
+}
+
+/* Class method putOrderedLong(Ljava/lang/Object;JJ)V */
+uintptr_t *unsafe_putOrderedLong(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    /* XXX can we use atomic writes here? */
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int value0 = (int) ostack[4];
+    int value1 = (int) ostack[5];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    objectLock(obj);
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        INST_DATA(obj)[fb->offset] = value0;
+        INST_DATA(obj)[fb->offset + 1] = value1;
+    }
+    else
+    {
+        ((uintptr_t *) ARRAY_DATA(obj))[offset] = value0;
+        ((uintptr_t *) ARRAY_DATA(obj))[offset + 1] = value1;
+    }
+    objectUnlock(obj);
+    return ostack;
+}
+
+/* Class method putOrderedObject(Ljava/lang/Object;JLjava/lang/Object;)V */
+uintptr_t *unsafe_putOrderedObject(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    Object *value = (Object *) ostack[4];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((offset & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        ATOMIC_WRITE((&(INST_DATA(obj)[fb->offset])), (uintptr_t) value);
+    }
+    else
+    {
+        ATOMIC_WRITE((&(((uintptr_t *) ARRAY_DATA(obj))[offset])), (uintptr_t) 
value);
+    }
+    return ostack;
+}
+
+/* Class method putIntVolatile(Ljava/lang/Object;JI)V */
+uintptr_t *unsafe_putIntVolatile(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int value = (int) ostack[4];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        ATOMIC_WRITE((&(INST_DATA(obj)[fb->offset])), (uintptr_t) value);
+    }
+    else
+    {
+        ATOMIC_WRITE((&(((uintptr_t *) ARRAY_DATA(obj))[offset])), value);
+    }
+    return ostack;
+}
+
+/* Class method getIntVolatile(Ljava/lang/Object;J)I */
+uintptr_t *unsafe_getIntVolatile(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int value = (int) ostack[4];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        *ostack++ = (uintptr_t) ATOMIC_READ((&(INST_DATA(obj)[fb->offset])));
+    }
+    else
+    {
+        *ostack++ = (uintptr_t) ATOMIC_READ((&(((uintptr_t *) 
ARRAY_DATA(obj))[offset])));
+    }
+    return ostack;
+}
+
+/* Class method putLongVolatile(Ljava/lang/Object;JJ)V */
+uintptr_t *unsafe_putLongVolatile(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    /* XXX can we use atomic writes here? */
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int value0 = (int) ostack[4];
+    int value1 = (int) ostack[5];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    objectLock(obj);
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        INST_DATA(obj)[fb->offset] = value0;
+        INST_DATA(obj)[fb->offset + 1] = value1;
+    }
+    else
+    {
+        ((uintptr_t *) ARRAY_DATA(obj))[offset] = value0;
+        ((uintptr_t *) ARRAY_DATA(obj))[offset + 1] = value1;
+    }
+    objectUnlock(obj);
+    return ostack;
+}
+
+/* Class method putLong(Ljava/lang/Object;JJ)V */
+uintptr_t *unsafe_putLong(Class *class, MethodBlock *mb, uintptr_t *ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    int value0 = (int) ostack[4];
+    int value1 = (int) ostack[5];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        INST_DATA(obj)[fb->offset] = value0;
+        INST_DATA(obj)[fb->offset + 1] = value1;
+    }
+    else
+    {
+        ((uintptr_t *) ARRAY_DATA(obj))[offset] = value0;
+        ((uintptr_t *) ARRAY_DATA(obj))[offset + 1] = value1;
+    }
+    return ostack;
+}
+
+/* Class method putLongVolatile(Ljava/lang/Object;JJ)V */
+uintptr_t *unsafe_getLongVolatile(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    /* XXX this is not correct. */
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    ClassBlock *cb = CLASS_CB(obj->class);
+    FieldBlock *fb = &(cb->fields[offset]);
+    objectLock(obj);
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        *ostack++ = INST_DATA(obj)[fb->offset];
+        *ostack++ = INST_DATA(obj)[fb->offset + 1];
+    }
+    else
+    {
+        *ostack++ = ((uintptr_t *) ARRAY_DATA(obj))[offset];
+        *ostack++ = ((uintptr_t *) ARRAY_DATA(obj))[offset + 1];
+    }
+    objectUnlock(obj);
+    return ostack;
+}
+
+/* Class method getLong(Ljava/lang/Object;J)J */
+uintptr_t *unsafe_getLong(Class *class, MethodBlock *mb, uintptr_t *ostack)
+{
+    /* XXX can we use atomic reads here? */
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        *ostack++ = INST_DATA(obj)[fb->offset];
+        *ostack++ = INST_DATA(obj)[fb->offset + 1];
+    }
+    else
+    {
+        *ostack++ = ((uintptr_t *) ARRAY_DATA(obj))[offset];
+        *ostack++ = ((uintptr_t *) ARRAY_DATA(obj))[offset + 1];
+    }
+    return ostack;
+}
+
+/* Class method putObjectVolatile(Ljava/lang/Object;JLjava/lang/Object;)V */
+uintptr_t *unsafe_putObjectVolatile(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    Object *value = (Object *) ostack[4];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        ATOMIC_WRITE((&(INST_DATA(obj)[fb->offset])), (uintptr_t) value);
+    }
+    else
+    {
+        ATOMIC_WRITE((&(((uintptr_t *) ARRAY_DATA(obj))[offset])), (uintptr_t) 
value);
+    }
+    return ostack;
+}
+
+/* Class method putObject(Ljava/lang/Object;JLjava/lang/Object;)V */
+uintptr_t *unsafe_putObject(Class *class, MethodBlock *mb, uintptr_t *ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    Object *value = (Object *) ostack[4];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        INST_DATA(obj)[fb->offset] = (uintptr_t) value;
+    }
+    else
+    {
+        ((uintptr_t *) ARRAY_DATA(obj))[offset] = (uintptr_t) value;
+    }
+    return ostack;
+}
+
+/* Class method getObjectVolatile(Ljava/lang/Object;J)Ljava/lang/Object; */
+uintptr_t *unsafe_getObjectVolatile(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    int offset = (int) ostack[2];
+    ClassBlock *cb = NULL;
+    FieldBlock *fb = NULL;
+    if ((ostack[3] & FIELD_MASK) != 0)
+    {
+        cb = CLASS_CB(obj->class);
+        fb = &(cb->fields[offset]);
+        *ostack++ = (uintptr_t) ATOMIC_READ((&(INST_DATA(obj)[fb->offset])));
+    }
+    else
+    {
+        *ostack++ = ATOMIC_READ((&(((uintptr_t *) ARRAY_DATA(obj))[offset])));
+    }
+    return ostack;
+}
+
+/* Class method arrayBaseOffset(Ljava/lang/Class;)I _/
+uintptr_t *unsafe_arrayBaseOffset(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    *ostack++ = 0;
+    return ostack;
+}
+
+/* Class method arrayIndexScale(Ljava/lang/Class;)I _/
+uintptr_t *unsafe_arrayIndexScale(Class *class, MethodBlock *mb, uintptr_t 
*ostack)
+{
+    signalException("java/lang/InternalError", " not implemented");
+    ostack++;
+    return ostack;
+}
+
+/* Class method unpark(Ljava/lang/Thread;)V */
+uintptr_t *unsafe_unpark(Class *class, MethodBlock *mb, uintptr_t *ostack)
+{
+    Object *obj = (Object *) ostack[1];
+    objectLock(obj);
+    objectNotify(obj);
+    objectUnlock(obj);
+    return ostack;
+}
+
+/* Class method park(ZJ)V _/
+uintptr_t *unsafe_park(Class *class, MethodBlock *mb, uintptr_t *ostack)
+{
+    signalException("java/lang/InternalError", " not implemented");
+    return ostack;
+}*/
+
 VMMethod vm_object[] = {
     {"getClass",                    getClass},
     {"clone",                       jamClone},
@@ -1068,6 +1473,30 @@
     {NULL,                          NULL}
 };
 
+VMMethod vm_unsafe[] = {
+    {"objectFieldOffset",           unsafe_objectFieldOffset},
+    {"compareAndSwapInt",           unsafe_compareAndSwapInt},
+    {"compareAndSwapLong",          unsafe_compareAndSwapLong},
+    {"compareAndSwapObject",        unsafe_compareAndSwapObject},
+    {"putOrderedInt",               unsafe_putOrderedInt},
+    {"putOrderedLong",              unsafe_putOrderedLong},
+    {"putOrderedObject",            unsafe_putOrderedObject},
+    {"putIntVolatile",              unsafe_putIntVolatile},
+    {"getIntVolatile",              unsafe_getIntVolatile},
+    {"putLongVolatile",             unsafe_putLongVolatile},
+    {"putLong",                     unsafe_putLong},
+    {"getLongVolatile",             unsafe_getLongVolatile},
+    {"getLong",                     unsafe_getLong},
+    {"putObjectVolatile",           unsafe_putObjectVolatile},
+    {"putObject",                   unsafe_putObject},
+    {"getObjectVolatile",           unsafe_getObjectVolatile},
+    /* {"arrayBaseOffset",             unsafe_arrayBaseOffset}, */
+    /* {"arrayIndexScale",             unsafe_arrayIndexScale}, */
+    {"unpark",                      unsafe_unpark},
+    /* {"park",                        unsafe_park}, */
+    {NULL,                          NULL}
+};
+
 VMClass native_methods[] = {
     {"java/lang/VMClass",                vm_class},
     {"java/lang/VMObject",               vm_object},
@@ -1083,5 +1512,6 @@
     {"java/security/VMAccessController", vm_access_controller},
     {"gnu/classpath/VMSystemProperties", vm_system_properties},
     {"gnu/classpath/VMStackWalker",      vm_stack_walker},
+    {"sun/misc/Unsafe",                  vm_unsafe},
     {NULL,                               NULL}
 };
Index: lib/sun/Makefile.am
--- /dev/null
+++ lib/sun/Makefile.am
@@ -0,0 +1,4 @@
+# used by automake to generate Makefile.in
+
+SUBDIRS = misc
+
Index: lib/sun/misc/Makefile.am
--- /dev/null
+++ lib/sun/misc/Makefile.am
@@ -0,0 +1,4 @@
+# used by automake to generate Makefile.in
+
+EXTRA_DIST = \
+Unsafe.java
Index: lib/sun/misc/Unsafe.java
--- /dev/null
+++ lib/sun/misc/Unsafe.java
@@ -0,0 +1,358 @@
+/* Unsafe.java - Unsafe operations needed for concurrency
+   Copyright (C) 2006 Free Software Foundation
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+package sun.misc;
+
+import java.lang.reflect.Field;
+
+/**
+ * This class should provide access to low-level operations and its
+ * use should be limited to trusted code.  Fields can be accessed using
+ * memory addresses, with undefined behaviour occurring if invalid memory
+ * addresses are given.
+ *
+ * @author Tom Tromey ([EMAIL PROTECTED])
+ * @author Andrew John Hughes ([EMAIL PROTECTED])
+ */
+public class Unsafe
+{
+  // Singleton class.
+  private static Unsafe unsafe = new Unsafe();
+
+  /**
+   * Private default constructor to prevent creation of an arbitrary
+   * number of instances.
+   */
+  private Unsafe()
+  {
+  }
+
+  /**
+   * Retrieve the singleton instance of <code>Unsafe</code>.  The calling
+   * method should guard this instance from untrusted code, as it provides
+   * access to low-level operations such as direct memory access.
+   *
+   * @throws SecurityException if a security manager exists and prevents
+   *                           access to the system properties.
+   */
+  public static Unsafe getUnsafe()
+  {
+    SecurityManager sm = System.getSecurityManager();
+    if (sm != null)
+      sm.checkPropertiesAccess();
+    return unsafe;
+  }
+  
+  /**
+   * Returns the memory address offset of the given static field.
+   * The offset is merely used as a means to access a particular field
+   * in the other methods of this class.  The value is unique to the given
+   * field and the same value should be returned on each subsequent call.
+   *
+   * @param field the field whose offset should be returned.
+   * @return the offset of the given field.
+   */
+  public native long objectFieldOffset(Field field);
+
+  /**
+   * Compares the value of the integer field at the specified offset
+   * in the supplied object with the given expected value, and updates
+   * it if they match.  The operation of this method should be atomic,
+   * thus providing an uninterruptible way of updating an integer field.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the integer field within <code>obj</code>.
+   * @param expect the expected value of the field.
+   * @param update the new value of the field if it equals <code>expect</code>.
+   * @return true if the field was changed.
+   */
+  public native boolean compareAndSwapInt(Object obj, long offset,
+                                          int expect, int update);
+
+  /**
+   * Compares the value of the long field at the specified offset
+   * in the supplied object with the given expected value, and updates
+   * it if they match.  The operation of this method should be atomic,
+   * thus providing an uninterruptible way of updating a long field.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the long field within <code>obj</code>.
+   * @param expect the expected value of the field.
+   * @param update the new value of the field if it equals <code>expect</code>.
+   * @return true if the field was changed.
+   */
+  public native boolean compareAndSwapLong(Object obj, long offset,
+                                           long expect, long update);
+
+  /**
+   * Compares the value of the object field at the specified offset
+   * in the supplied object with the given expected value, and updates
+   * it if they match.  The operation of this method should be atomic,
+   * thus providing an uninterruptible way of updating an object field.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the object field within <code>obj</code>.
+   * @param expect the expected value of the field.
+   * @param update the new value of the field if it equals <code>expect</code>.
+   * @return true if the field was changed.
+   */
+  public native boolean compareAndSwapObject(Object obj, long offset,
+                                             Object expect, Object update);
+
+  /**
+   * Sets the value of the integer field at the specified offset in the
+   * supplied object to the given value.  This is an ordered or lazy
+   * version of <code>putIntVolatile(Object,long,int)</code>, which
+   * doesn't guarantee the immediate visibility of the change to other
+   * threads.  It is only really useful where the integer field is
+   * <code>volatile</code>, and is thus expected to change unexpectedly.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the integer field within <code>obj</code>.
+   * @param value the new value of the field.
+   * @see #putIntVolatile(Object,long,int)
+   */
+  public native void putOrderedInt(Object obj, long offset, int value);
+
+  /**
+   * Sets the value of the long field at the specified offset in the
+   * supplied object to the given value.  This is an ordered or lazy
+   * version of <code>putLongVolatile(Object,long,long)</code>, which
+   * doesn't guarantee the immediate visibility of the change to other
+   * threads.  It is only really useful where the long field is
+   * <code>volatile</code>, and is thus expected to change unexpectedly.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the long field within <code>obj</code>.
+   * @param value the new value of the field.
+   * @see #putLongVolatile(Object,long,long)
+   */
+  public native void putOrderedLong(Object obj, long offset, long value);
+
+  /**
+   * Sets the value of the object field at the specified offset in the
+   * supplied object to the given value.  This is an ordered or lazy
+   * version of <code>putObjectVolatile(Object,long,Object)</code>, which
+   * doesn't guarantee the immediate visibility of the change to other
+   * threads.  It is only really useful where the object field is
+   * <code>volatile</code>, and is thus expected to change unexpectedly.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the object field within <code>obj</code>.
+   * @param value the new value of the field.
+   */
+  public native void putOrderedObject(Object obj, long offset, Object value);
+
+  /**
+   * Sets the value of the integer field at the specified offset in the
+   * supplied object to the given value, with volatile store semantics.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the integer field within <code>obj</code>.
+   * @param value the new value of the field.
+   */
+  public native void putIntVolatile(Object obj, long offset, int value);
+
+  /**
+   * Retrieves the value of the integer field at the specified offset in the
+   * supplied object with volatile load semantics.
+   *
+   * @param obj the object containing the field to read.
+   * @param offset the offset of the integer field within <code>obj</code>.
+   */
+  public native int getIntVolatile(Object obj, long offset);
+
+  /**
+   * Sets the value of the long field at the specified offset in the
+   * supplied object to the given value, with volatile store semantics.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the long field within <code>obj</code>.
+   * @param value the new value of the field.
+   * @see #putLong(Object,long,long)
+   */
+  public native void putLongVolatile(Object obj, long offset, long value);
+
+  /**
+   * Sets the value of the long field at the specified offset in the
+   * supplied object to the given value.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the long field within <code>obj</code>.
+   * @param value the new value of the field.
+   * @see #putLongVolatile(Object,long,long)
+   */
+  public native void putLong(Object obj, long offset, long value);
+
+  /**
+   * Retrieves the value of the long field at the specified offset in the
+   * supplied object with volatile load semantics.
+   *
+   * @param obj the object containing the field to read.
+   * @param offset the offset of the long field within <code>obj</code>.
+   * @see #getLong(Object,long)
+   */
+  public native long getLongVolatile(Object obj, long offset);
+
+  /**
+   * Retrieves the value of the long field at the specified offset in the
+   * supplied object.
+   *
+   * @param obj the object containing the field to read.
+   * @param offset the offset of the long field within <code>obj</code>.
+   * @see #getLongVolatile(Object,long)
+   */
+  public native long getLong(Object obj, long offset);
+
+  /**
+   * Sets the value of the object field at the specified offset in the
+   * supplied object to the given value, with volatile store semantics.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the object field within <code>obj</code>.
+   * @param value the new value of the field.
+   * @see #putObject(Object,long,Object)
+   */
+  public native void putObjectVolatile(Object obj, long offset, Object value);
+
+  /**
+   * Sets the value of the object field at the specified offset in the
+   * supplied object to the given value.
+   *
+   * @param obj the object containing the field to modify.
+   * @param offset the offset of the object field within <code>obj</code>.
+   * @param value the new value of the field.
+   * @see #putObjectVolatile(Object,long,Object)
+   */
+  public native void putObject(Object obj, long offset, Object value);
+
+  /**
+   * Retrieves the value of the object field at the specified offset in the
+   * supplied object with volatile load semantics.
+   *
+   * @param obj the object containing the field to read.
+   * @param offset the offset of the object field within <code>obj</code>.
+   */
+  public native Object getObjectVolatile(Object obj, long offset);
+
+  /**
+   * Returns the offset of the first element for a given array class.
+   * To access elements of the array class, this value may be used along
+   * with that returned by 
+   * <a href="#arrayIndexScale"><code>arrayIndexScale</code></a>,
+   * if non-zero.
+   *
+   * @param arrayClass the class for which the first element's address should
+   *                   be obtained.
+   * @return the offset of the first element of the array class.
+   * @see arrayIndexScale(Class)
+   */
+  public int arrayBaseOffset(Class arrayClass)
+  {
+    return 0;
+  }
+
+  /**
+   * Returns the scale factor used for addressing elements of the supplied
+   * array class.  Where a suitable scale factor can not be returned (e.g.
+   * for primitive types), zero should be returned.  The returned value
+   * can be used with 
+   * <a href="#arrayBaseOffset"><code>arrayBaseOffset</code></a>
+   * to access elements of the class.
+   *
+   * @param arrayClass the class whose scale factor should be returned.
+   * @return the scale factor, or zero if not supported for this array class.
+   */
+  public int arrayIndexScale(Class arrayClass)
+  {
+    Class elementType = arrayClass.getComponentType();
+    if (elementType.equals(Long.TYPE) || elementType.equals(Double.TYPE))
+      return 2;
+    return 1;
+  }
+  
+  /**
+   * Releases the block on a thread created by 
+   * <a href="#park"><code>park</code></a>.  This method can also be used
+   * to terminate a blockage caused by a prior call to <code>park</code>.
+   * This operation is unsafe, as the thread must be guaranteed to be
+   * live.  This is true of Java, but not native code.
+   *
+   * @param thread the thread to unblock.
+   */
+  public native void unpark(Thread thread);
+//   {
+//     synchronized (thread)
+//       {
+//         thread.notify();
+//       }
+//   }
+
+  /**
+   * Blocks the thread until a matching 
+   * <a href="#unpark"><code>unpark</code></a> occurs, the thread is
+   * interrupted or the optional timeout expires.  If an <code>unpark</code>
+   * call has already occurred, this also counts.  A timeout value of zero
+   * is defined as no timeout.  When <code>isAbsolute</code> is
+   * <code>true</code>, the timeout is in milliseconds relative to the
+   * epoch.  Otherwise, the value is the number of nanoseconds which must
+   * occur before timeout.  This call may also return spuriously (i.e.
+   * for no apparent reason).
+   *
+   * @param isAbsolute true if the timeout is specified in milliseconds from
+   *                   the epoch.
+   * @param time either the number of nanoseconds to wait, or a time in
+   *             milliseconds from the epoch to wait for.
+   */
+  public void park(boolean isAbsolute, long time)
+  {
+    Thread t = Thread.currentThread();
+    synchronized (t)
+      {
+        try
+          {
+            if (isAbsolute)
+              t.wait(time - System.currentTimeMillis());
+            else
+              t.wait(0, (int) time);
+          }
+        catch (InterruptedException ie)
+          {
+          }
+      }
+  }
+}

Attachment: PGP.sig
Description: This is a digitally signed message part

Reply via email to