--- old/src/hotspot/share/runtime/sweeper.cpp 2018-09-23 20:45:25.744241981 +0200 +++ new/src/hotspot/share/runtime/sweeper.cpp 2018-09-23 20:45:25.442241998 +0200 @@ -247,6 +247,15 @@ } CodeBlobClosure* NMethodSweeper::prepare_mark_active_nmethods() { +#ifdef ASSERT + if (ThreadLocalHandshakes) { + assert(Thread::current()->is_Code_cache_sweeper_thread(), "must be executed under CodeCache_lock and in sweeper thread"); + assert_lock_strong(CodeCache_lock); + } else { + assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint"); + } +#endif + // If we do not want to reclaim not-entrant or zombie methods there is no need // to scan stacks if (!MethodFlushing) { @@ -295,7 +304,11 @@ assert(!CodeCache_lock->owned_by_self(), "just checking"); if (wait_for_stack_scanning()) { if (ThreadLocalHandshakes) { - CodeBlobClosure* code_cl = prepare_mark_active_nmethods(); + CodeBlobClosure* code_cl; + { + MutexLockerEx ccl(CodeCache_lock, Mutex::_no_safepoint_check_flag); + code_cl = prepare_mark_active_nmethods(); + } if (code_cl != NULL) { ThreadToCodeBlobClosure tcl(code_cl); Handshake::execute(&tcl);