Repository navigation
Thread: make the thread-local map on first use - #65
duncanmcqueen wants to merge 1 commit into
Conversation
Some VMs (for example Kaffe) make the Thread object of the main thread without running a constructor, so its locals field is null and every ThreadLocal fails. Make the field non-final and create the map in getThreadLocals() when it is null. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: duncanmcqueen <dwmcqueen@gmail.com>
242d7aa to
a1a8c44
Compare
|
Thanks for the detailed write-up and the reproducer. I’m not sure Classpath is the right place to fix this, though. I think it’s a bug in Kaffe. Classpath has a constructor for VMs to create Also, the patch as it stands doesn’t cover every path. The proper fix here would be to modify
(the rest of the function can stay as it is) That would fix Also, you mention that Kaffe's repository is archived, so you cannot fix this in Kaffe. But if I understand correctly, you already maintain a set of Kaffe patches in order to add Haiku support. Perhaps that's the better place for this fix? |
KaffeVM_attachFakedThreadInstance() made the Thread object of the main thread (and of threads that attach through JNI) with newObject() and set some fields from C. The fields that Kaffe does not know about kept their default values. With GNU Classpath 0.99 (ingelabs), Thread.locals was null, so each ThreadLocal on the main thread threw NullPointerException and Kaffe stopped before main(). Thread.getId() also returned 0. Use the Thread(VMThread, String, int, boolean) constructor that Classpath has for threads that start outside Java, as createDaemon() already does: make the VMThread, construct the Thread, then set the VMThread back pointer. The rest of the function does not change. Suggested by the GNU Classpath maintainer in ingelabs/classpath#65. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
patches/kaffe/0004 is the fix that the GNU Classpath maintainer suggested in ingelabs/classpath#65: KaffeVM_attachFakedThreadInstance() now uses Thread(VMThread, String, int, boolean), as createDaemon() does. This sets Thread.locals and Thread.getId() for the main thread. The same commit is on the haiku branch of duncanmcqueen/kaffe. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Thank you. I agree: the problem is in Kaffe, and your fix is better. I examined the points and they are all correct:
I changed Results on Haiku R1/beta6 x86_64, with ingelabs
The JNI path ( Thus I close this pull request. I also changed the description of #61. An AI model (Claude Code, by Anthropic) made the Kaffe change and the tests, and wrote this text. I directed the work and checked the results. |
Split from #59.
Problem and fix
ThreadLocalusesThread.locals, which aThreadconstructor sets. Kaffe makes theThreadobject of the main thread without a constructor:KaffeVM_attachFakedThreadInstance()inkaffe/kaffevm/thread.ccallsnewObject(ThreadClass)and sets the fields from C. It does not setlocals. Thus eachThreadLocalon the main thread throwsNullPointerException. Kaffe uses the same function for native threads that attach through JNI.Classpath itself uses
ThreadLocalduring startup (VMAccessController), so Kaffe cannot start any program.This change makes
localsnon-final, andThread.getThreadLocals()makes the map whenlocalsisnull.How to reproduce
This problem occurs only on a VM that makes
Threadobjects without a constructor. JamVM does not, so the test passes on JamVM with and without this change.MainThreadLocal.javaResults
Linux x86_64, ingelabs/classpath
master(0406074), JamVMmaster,javacfrom JDK 8, autoconf 2.73, automake 1.19, theconfigureoptions of the CI Linux job, out-of-tree build. "Before" ismaster. "After" ismasterwith this commit.On Kaffe 1.1.10-pre, the last version in its repository (Haiku x86_64), with the
Threadclasses ofmasterbeforeglibj.zip, Kaffe stops beforemain():With this change:
PASS: get() = value.On JamVM (Linux):
PASS: get() = valuebefore and after.Platforms
Not specific to Haiku, but specific to the VM. The problem occurs with Kaffe on all systems. Kaffe's repository is archived, so I cannot fix it in Kaffe.
I understand if you prefer not to have a workaround for one VM in
Thread. The change is small, and it has no effect on VMs that run aThreadconstructor.AI assistance
An AI model (Claude Code, by Anthropic) wrote this change, the test and this text. The repository owner directed the work and checked the results. Please review it as you review other contributions.
🤖 Generated with Claude Code