Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.
/ jdk19 Public archive

Commit

Permalink
8288497: add support for JavaThread::is_oop_safe()
Browse files Browse the repository at this point in the history
Reviewed-by: pchilanomate, dholmes, rehn, eosterlund
  • Loading branch information
Daniel D. Daugherty committed Jun 21, 2022
1 parent c74a923 commit e26d3b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/hotspot/share/runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3599,6 +3599,12 @@ void Threads::remove(JavaThread* p, bool is_daemon) {
// StackWatermarkSet::on_safepoint(), which performs GC processing,
// requiring the GC state to be alive.
BarrierSet::barrier_set()->on_thread_detach(p);
if (p->is_exiting()) {
// If we got here via JavaThread::exit(), then we remember that the
// thread's GC barrier has been detached. We don't do this when we get
// here from another path, e.g., cleanup_failed_attach_current_thread().
p->set_terminated(JavaThread::_thread_gc_barrier_detached);
}

assert(ThreadsSMRSupport::get_java_thread_list()->includes(p), "p must be present");

Expand Down
19 changes: 14 additions & 5 deletions src/hotspot/share/runtime/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,9 @@ class JavaThread: public Thread {
// JavaThread termination support
public:
enum TerminatedTypes {
_not_terminated = 0xDEAD - 2,
_not_terminated = 0xDEAD - 3,
_thread_exiting, // JavaThread::exit() has been called for this thread
_thread_gc_barrier_detached, // thread's GC barrier has been detached
_thread_terminated, // JavaThread is removed from thread list
_vm_exited // JavaThread is still executing native code, but VM is terminated
// only VM_Exit can set _vm_exited
Expand All @@ -914,10 +915,14 @@ class JavaThread: public Thread {
private:
// In general a JavaThread's _terminated field transitions as follows:
//
// _not_terminated => _thread_exiting => _thread_terminated
// _not_terminated => _thread_exiting => _thread_gc_barrier_detached => _thread_terminated
//
// _vm_exited is a special value to cover the case of a JavaThread
// executing native code after the VM itself is terminated.
//
// A JavaThread that fails to JNI attach has these _terminated field transitions:
// _not_terminated => _thread_terminated
//
volatile TerminatedTypes _terminated;

jint _in_deopt_handler; // count of deoptimization
Expand Down Expand Up @@ -1172,13 +1177,17 @@ class JavaThread: public Thread {
bool on_thread_list() const { return _on_thread_list; }
void set_on_thread_list() { _on_thread_list = true; }

// thread has called JavaThread::exit() or is terminated
// thread has called JavaThread::exit(), thread's GC barrier is detached
// or thread is terminated
bool is_exiting() const;
// thread's GC barrier is NOT detached and thread is NOT terminated
bool is_oop_safe() const;
// thread is terminated (no longer on the threads list); we compare
// against the two non-terminated values so that a freed JavaThread
// against the three non-terminated values so that a freed JavaThread
// will also be considered terminated.
bool check_is_terminated(TerminatedTypes l_terminated) const {
return l_terminated != _not_terminated && l_terminated != _thread_exiting;
return l_terminated != _not_terminated && l_terminated != _thread_exiting &&
l_terminated != _thread_gc_barrier_detached;
}
bool is_terminated() const;
void set_terminated(TerminatedTypes t);
Expand Down
15 changes: 9 additions & 6 deletions src/hotspot/share/runtime/thread.inline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,24 @@ inline void JavaThread::set_done_attaching_via_jni() {
}

inline bool JavaThread::is_exiting() const {
// Use load-acquire so that setting of _terminated by
// JavaThread::exit() is seen more quickly.
TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
return l_terminated == _thread_exiting || check_is_terminated(l_terminated);
return l_terminated == _thread_exiting ||
l_terminated == _thread_gc_barrier_detached ||
check_is_terminated(l_terminated);
}

inline bool JavaThread::is_oop_safe() const {
TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
return l_terminated != _thread_gc_barrier_detached &&
!check_is_terminated(l_terminated);
}

inline bool JavaThread::is_terminated() const {
// Use load-acquire so that setting of _terminated by
// JavaThread::exit() is seen more quickly.
TerminatedTypes l_terminated = Atomic::load_acquire(&_terminated);
return check_is_terminated(l_terminated);
}

inline void JavaThread::set_terminated(TerminatedTypes t) {
// use release-store so the setting of _terminated is seen more quickly
Atomic::release_store(&_terminated, t);
}

Expand Down
3 changes: 2 additions & 1 deletion src/hotspot/share/services/threadService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ void ThreadService::remove_thread(JavaThread* thread, bool daemon) {

assert(!thread->is_terminated(), "must not be terminated");
if (!thread->is_exiting()) {
// JavaThread::exit() skipped calling current_thread_exiting()
// We did not get here via JavaThread::exit() so current_thread_exiting()
// was not called, e.g., JavaThread::cleanup_failed_attach_current_thread().
decrement_thread_counts(thread, daemon);
}

Expand Down

1 comment on commit e26d3b3

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.