This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Fast ThreadLocal variables for gcj


We recently noticed (courtesy of oprofile) that ThreadLocal variables
are very slow in gcj.  Modern glibcs have a super-fast implementation
of thread-local storage, and this patch changes gcj to use it instead
of a WeakIdentityHashMap that maps ThreadLocal->Object.

The result is that ThreadLocal.get() is in the common cases more than
ten times faster, but ThreadLocal.set() is slightly slower.  It may be
worth doing something similar for some other VMs that use glibc.

This patch requires a change to Classpath's implementation of
ThreadLocal, which contains the line 

  static final Object NULL = new Object();

The use of NULL as the name of a field plays havoc with CNI.  I'll
submit a patch to Classpath that changes that.

Andrew.



2006-10-12  Andrew Haley  <aph@redhat.com>

	* java/lang/natThreadLocal.cc: New file.
	* java/lang/ThreadLocal.java: Rewrite to use native TLS.
	* Makefile.am: Add java/lang/natThreadLocal.cc.
	* sources.am: Move classpath/java/lang/ThreadLocal.java to
	java/lang/ThreadLocal.java.

Index: sources.am
===================================================================
--- sources.am	(revision 116629)
+++ sources.am	(working copy)
@@ -5023,7 +5023,7 @@
 java/lang/Thread.java \
 classpath/java/lang/ThreadDeath.java \
 classpath/java/lang/ThreadGroup.java \
-classpath/java/lang/ThreadLocal.java \
+java/lang/ThreadLocal.java \
 classpath/java/lang/Throwable.java \
 classpath/java/lang/TypeNotPresentException.java \
 classpath/java/lang/UnknownError.java \
Index: java/lang/ThreadLocal.java
===================================================================
--- java/lang/ThreadLocal.java	(revision 0)
+++ java/lang/ThreadLocal.java	(revision 116632)
@@ -0,0 +1,179 @@
+/* ThreadLocal -- a variable with a unique value per thread
+   Copyright (C) 2000, 2002, 2003, 2006 Free Software Foundation, Inc.
+
+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 java.lang;
+
+import java.util.Map;
+
+
+/**
+ * ThreadLocal objects have a different state associated with every
+ * Thread that accesses them. Every access to the ThreadLocal object
+ * (through the <code>get()</code> and <code>set()</code> methods)
+ * only affects the state of the object as seen by the currently
+ * executing Thread.
+ *
+ * <p>The first time a ThreadLocal object is accessed on a particular
+ * Thread, the state for that Thread's copy of the local variable is set by
+ * executing the method <code>initialValue()</code>.
+ * </p>
+ *
+ * <p>An example how you can use this:
+ * </p>
+ *
+ * <pre>
+ * class Connection
+ * {
+ *   private static ThreadLocal owner = new ThreadLocal()
+ *     {
+ *       public Object initialValue()
+ *       {
+ *         return("nobody");
+ *       }
+ *     };
+ * ...
+ * }
+ * </pre>
+ *
+ * <p>Now all instances of connection can see who the owner of the currently
+ * executing Thread is by calling <code>owner.get()</code>. By default any
+ * Thread would be associated with 'nobody'. But the Connection object could
+ * offer a method that changes the owner associated with the Thread on
+ * which the method was called by calling <code>owner.put("somebody")</code>.
+ * (Such an owner changing method should then be guarded by security checks.)
+ * </p>
+ *
+ * <p>When a Thread is garbage collected all references to values of
+ * the ThreadLocal objects associated with that Thread are removed.
+ * </p>
+ *
+ * @author Mark Wielaard (mark@klomp.org)
+ * @author Eric Blake (ebb9@email.byu.edu)
+ * @since 1.2
+ * @status updated to 1.4
+ */
+public class ThreadLocal
+{
+  /**
+   * Placeholder to distinguish between uninitialized and null set by the
+   * user. Do not expose this to the public. Package visible for use by
+   * InheritableThreadLocal
+   */
+  static final Object sentinel = new Object();
+
+  /**
+   * Creates a ThreadLocal object without associating any value to it yet.
+   */
+  public ThreadLocal()
+  {
+    constructNative();
+  }
+
+  /**
+   * Called once per thread on the first invocation of get(), if set() was
+   * not already called. The default implementation returns <code>null</code>.
+   * Often, this method is overridden to create the appropriate initial object
+   * for the current thread's view of the ThreadLocal.
+   *
+   * @return the initial value of the variable in this thread
+   */
+  protected Object initialValue()
+  {
+    return null;
+  }
+
+  /**
+   * Gets the value associated with the ThreadLocal object for the currently
+   * executing Thread. If this is the first time the current thread has called
+   * get(), and it has not already called set(), the value is obtained by
+   * <code>initialValue()</code>.
+   *
+   * @return the value of the variable in this thread
+   */
+  public native Object get();
+
+  private final Object internalGet()
+  {
+    Map map = Thread.getThreadLocals();
+    // Note that we don't have to synchronize, as only this thread will
+    // ever modify the map.
+    Object value = map.get(this);
+    if (value == null)
+      {
+        value = initialValue();
+        map.put(this, value == null ? sentinel : value);
+      }
+    return value == sentinel ? null : value;
+  }
+
+  /**
+   * Sets the value associated with the ThreadLocal object for the currently
+   * executing Thread. This overrides any existing value associated with the
+   * current Thread and prevents <code>initialValue()</code> from being
+   * called if this is the first access to this ThreadLocal in this Thread.
+   *
+   * @param value the value to set this thread's view of the variable to
+   */
+  public native void set(Object value);
+
+  private final void internalSet(Object value)
+  {
+    Map map = Thread.getThreadLocals();
+    // Note that we don't have to synchronize, as only this thread will
+    // ever modify the map.
+    map.put(this, value == null ? sentinel : value);
+  }
+
+  /**
+   * Removes the value associated with the ThreadLocal object for the
+   * currently executing Thread.
+   * @since 1.5
+   */
+  public native void remove();
+
+  private final void internalRemove()
+  {
+    Map map = Thread.getThreadLocals();
+    map.remove(this);
+  }
+
+  protected native void finalize () throws Throwable;
+
+  private native void constructNative();
+
+  private gnu.gcj.RawData TLSPointer;
+}
Index: java/lang/natThreadLocal.cc
===================================================================
--- java/lang/natThreadLocal.cc	(revision 0)
+++ java/lang/natThreadLocal.cc	(revision 116632)
@@ -0,0 +1,169 @@
+// natThreadLocal.cc - Native part of ThreadLocal class.
+
+// Fast thread local storage for systems that support the __thread
+// variable attribute.
+   
+/* Copyright (C) 2006  Free Software Foundation
+
+   This file is part of libgcj.
+
+This software is copyrighted work licensed under the terms of the
+Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
+details.  */
+
+#include <config.h>
+
+#include <stdlib.h>
+
+#include <gcj/cni.h>
+#include <jvm.h>
+#include <java-threads.h>
+
+#include <gnu/gcj/RawDataManaged.h>
+#include <java/lang/ThreadLocal.h>
+#include <java/lang/IllegalArgumentException.h>
+#include <java/lang/IllegalThreadStateException.h>
+#include <java/lang/InterruptedException.h>
+#include <java/util/Map.h>
+
+#include <jni.h>
+
+/* We would like to have fast thread local variables that behave in
+   the same way as C and C++ thread local variables.  This would mean
+   having an field attribute "thread" (like static, final, etc.).
+   However, this is not compatible with java semantics, which we wish
+   to support transparently.  The problems we must overcome are:
+
+   * In Java, ThreadLocal variables are not statically allocated: they
+     are objects, created at runtime.
+
+   * Class ThreadLocal is not final and neither are its methods, so it
+     is possible to create a subclass of ThreadLocal that overrides
+     any method.
+
+   * __thread variables in DSOs are not visible to the garbage
+     collector, so we must ensure that we keep a copy of every thread
+     local variable somewhere on the heap.
+
+   * Once a ThreadLocal instance has been created and assigned to a
+     static field, that field may be reassigned to a different
+     ThreadLocal instance or null.  
+
+   So, we can't simply replace get() and set() with accesses of a
+   __thread variable.
+
+   So, we create a pthread_key in each ThreadLocal object and use that
+   as a kind of "look-aside cache".  When a ThreadLocal is set, we
+   also set the corresponding thread-specific value.  When the
+   ThreadLocal is collected, we delete the key.
+
+   This scheme is biased towards efficiency when get() is called much
+   more frequently than set().  It is slightly internaler than the
+   all-Java solution using the underlying map in the set() case.
+   However, get() is very much more frequently invoked than set().
+
+*/
+
+
+#ifdef _POSIX_PTHREAD_SEMANTICS
+
+class tls_t
+{
+public:
+  pthread_key_t key;
+};
+
+void
+java::lang::ThreadLocal::constructNative (void)
+{
+  tls_t *tls = (tls_t *)_Jv_Malloc (sizeof (tls_t));
+  if (pthread_key_create (&tls->key, NULL) == 0)
+    TLSPointer = (::gnu::gcj::RawData *)tls;
+  else
+    _Jv_Free (tls);
+}
+
+void 
+java::lang::ThreadLocal::set (::java::lang::Object *value)
+{
+  if (TLSPointer != NULL)
+    {
+      tls_t* tls = (tls_t*)TLSPointer;
+      pthread_setspecific (tls->key, value);
+    } 
+
+  internalSet (value);
+}
+
+::java::lang::Object *
+java::lang::ThreadLocal::get (void)
+{
+  if (TLSPointer == NULL)
+    return internalGet ();
+
+  tls_t* tls = (tls_t*)TLSPointer;
+  void *obj = pthread_getspecific(tls->key);
+
+  if (obj)
+    return (::java::lang::Object *)obj;
+
+  ::java::lang::Object *value = internalGet ();
+  pthread_setspecific (tls->key, value);
+
+  return value;
+}
+
+void 
+java::lang::ThreadLocal::remove (void)
+{
+  if (TLSPointer != NULL)
+    {
+      tls_t* tls = (tls_t*)TLSPointer;
+      pthread_setspecific (tls->key, NULL);
+    }
+
+  internalRemove ();
+}
+
+void 
+java::lang::ThreadLocal::finalize (void)
+{
+  if (TLSPointer != NULL)
+    {
+      tls_t* tls = (tls_t*)TLSPointer;
+      pthread_key_delete (tls->key);
+      _Jv_Free (tls);
+    }
+}
+
+#else
+
+void
+java::lang::ThreadLocal::constructNative (void)
+{
+}
+
+void 
+java::lang::ThreadLocal::set (::java::lang::Object *value)
+{
+  internalSet (value);
+}
+
+::java::lang::Object *
+java::lang::ThreadLocal::get (void)
+{
+  return internalGet ();
+}
+
+void 
+java::lang::ThreadLocal::remove (void)
+{
+  internalRemove ();
+}
+
+void 
+java::lang::ThreadLocal::finalize (void)
+{
+}
+
+#endif
Index: Makefile.am
===================================================================
--- Makefile.am	(revision 116629)
+++ Makefile.am	(working copy)
@@ -844,6 +844,7 @@
 java/lang/natStringBuilder.cc \
 java/lang/natSystem.cc \
 java/lang/natThread.cc \
+java/lang/natThreadLocal.cc \
 java/lang/natVMClassLoader.cc \
 java/lang/natVMSecurityManager.cc \
 java/lang/natVMThrowable.cc \


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]