Appropriate way to delete NonJavaThread objects?
Liu, Xin
xxinliu at amazon.com
Thu Apr 8 06:56:09 UTC 2021
Hi, Hotspot developers,
I have a question about NonJavaThread destructor. Currently, there are
some NonJavaThread subclasses. Here is a simple class hierarchy.
Thread
NonJavaThrea
NamedThread
WorkerThread
VMThread
WatcherThread
JfrThreadSampler
I found some subclasses of NonJavaThread disallow
destruction. Eg. VMThread and WatcherThread simply abort.
// No destruction allowed
~VMThread() {
guarantee(false, "VMThread deletion must fix the race with VM termination");
}
Is it absolutely no resource leak if we skip ~Thread()? I know
Linux/FreeBSD guarantee to reclaim anything when a process is
terminated, but is it true for other platforms?
Putting aside resource leak, I don't think all NonJavaThread
subclasses necessarily have same lifecycle of JVM. Current
NonJavaThread implementation seems to inhibit from complete
destruction. eg. JfrThreadSampler uses the following approach to
delete itself.
void JfrThreadSampler::post_run() {
this->NonJavaThread::post_run();
delete this;
}
I think this code is correct, but it is fragile because
NonJavaThread::post_run() nullifies Thread::clear_thread_current()
before Thread::~Thread(). "delete this" will hit the assertion of
Thread::current() at this statement.
// osthread() can be NULL, if creation of thread failed.
if (osthread() != NULL) os::free_thread(osthread())
Does it matter if we ignore all destructions of NonJavaThread objects?
If the answer is no, is there an appropriate way to delete them?
Thanks,
--lx
More information about the hotspot-runtime-dev
mailing list