[EMAIL PROTECTED] wrote: > >>>>> "Jeroen" == Jeroen Frijters <[EMAIL PROTECTED]> writes: > > Jeroen> I propose the attached change to the handling of > thread locals. This > Jeroen> fixes bug #24858 and makes the code slightly more > obvious (at the cost > Jeroen> of introducing the new class WeakIdentityHashMap). > > Jeroen> Comments? > > IMO it is fine.
Thanks for reviewing. I've committed the patch. Regards, Jeroen 2006-01-06 Jeroen Frijters <[EMAIL PROTECTED]> PR classpath/24858 * gnu/java/util/WeakIdentityHashMap.java: New file. * java/lang/InheritableThreadLocal.java (newChildThread): Modified to remove key indirection. * java/lang/Thread.java (locals): Changed type to WeakIdentityHashMap. (getThreadLocals): Instantiate WeakIdentityHashMap instead of WeakHashMap. * java/lang/ThreadLocal.java (key, Key): Removed. (get, set): Changed to use "this" instead of "key".
Index: java/lang/InheritableThreadLocal.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/InheritableThreadLocal.java,v retrieving revision 1.10 diff -u -r1.10 InheritableThreadLocal.java --- java/lang/InheritableThreadLocal.java 2 Jul 2005 20:32:38 -0000 1.10 +++ java/lang/InheritableThreadLocal.java 4 Jan 2006 10:18:20 -0000 @@ -1,5 +1,5 @@ /* InheritableThreadLocal -- a ThreadLocal which inherits values across threads - Copyright (C) 2000, 2001, 2002, 2003, 2005 Free Software Foundation, Inc. + Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -37,6 +37,7 @@ package java.lang; +import gnu.java.util.WeakIdentityHashMap; import java.util.Iterator; import java.util.WeakHashMap; @@ -98,15 +99,15 @@ Iterator keys = parentThread.locals.keySet().iterator(); while (keys.hasNext()) { - Key key = (Key)keys.next(); - if (key.get() instanceof InheritableThreadLocal) + Object key = keys.next(); + if (key instanceof InheritableThreadLocal) { - InheritableThreadLocal local = (InheritableThreadLocal)key.get(); + InheritableThreadLocal local = (InheritableThreadLocal)key; Object parentValue = parentThread.locals.get(key); Object childValue = local.childValue(parentValue == NULL ? null : parentValue); if (childThread.locals == null) - childThread.locals = new WeakHashMap(); + childThread.locals = new WeakIdentityHashMap(); childThread.locals.put(key, (childValue == null ? NULL : childValue)); } Index: java/lang/Thread.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/Thread.java,v retrieving revision 1.16 diff -u -r1.16 Thread.java --- java/lang/Thread.java 23 Oct 2005 17:04:46 -0000 1.16 +++ java/lang/Thread.java 4 Jan 2006 10:02:32 -0000 @@ -38,9 +38,9 @@ package java.lang; +import gnu.java.util.WeakIdentityHashMap; import java.security.Permission; import java.util.Map; -import java.util.WeakHashMap; /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * "The Java Language Specification", ISBN 0-201-63451-1 @@ -137,7 +137,7 @@ /** Thread local storage. Package accessible for use by * InheritableThreadLocal. */ - WeakHashMap locals; + WeakIdentityHashMap locals; /** * Allocates a new <code>Thread</code> object. This constructor has @@ -996,7 +996,7 @@ Map locals = thread.locals; if (locals == null) { - locals = thread.locals = new WeakHashMap(); + locals = thread.locals = new WeakIdentityHashMap(); } return locals; } Index: java/lang/ThreadLocal.java =================================================================== RCS file: /cvsroot/classpath/classpath/java/lang/ThreadLocal.java,v retrieving revision 1.8 diff -u -r1.8 ThreadLocal.java --- java/lang/ThreadLocal.java 13 Sep 2005 03:19:30 -0000 1.8 +++ java/lang/ThreadLocal.java 4 Jan 2006 10:00:12 -0000 @@ -1,5 +1,5 @@ /* ThreadLocal -- a variable with a unique value per thread - Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc. + Copyright (C) 2000, 2002, 2003, 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. @@ -96,21 +96,6 @@ static final Object NULL = new Object(); /** - * Serves as a key for the Thread.locals WeakHashMap. - * We can't use "this", because a subclass may override equals/hashCode - * and we need to use object identity for the map. - */ - final Key key = new Key(); - - class Key - { - ThreadLocal get() - { - return ThreadLocal.this; - } - } - - /** * Creates a ThreadLocal object without associating any value to it yet. */ public ThreadLocal() @@ -143,11 +128,11 @@ 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(key); + Object value = map.get(this); if (value == null) { value = initialValue(); - map.put(key, value == null ? NULL : value); + map.put(this, value == null ? NULL : value); } return value == NULL ? null : value; } @@ -165,6 +150,6 @@ Map map = Thread.getThreadLocals(); // Note that we don't have to synchronize, as only this thread will // ever modify the map. - map.put(key, value == null ? NULL : value); + map.put(this, value == null ? NULL : value); } }
WeakIdentityHashMap.java
Description: WeakIdentityHashMap.java
_______________________________________________ Classpath-patches mailing list Classpath-patches@gnu.org http://lists.gnu.org/mailman/listinfo/classpath-patches