1 /*
  2  * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  *
 24  */
 25 
 26 #include "precompiled.hpp"
 27 #include "cds/cdsConfig.hpp"
 28 #include "classfile/javaClasses.hpp"
 29 #include "classfile/javaThreadStatus.hpp"
 30 #include "gc/shared/barrierSet.hpp"
 31 #include "jfr/jfrEvents.hpp"
 32 #include "jvm.h"
 33 #include "jvmtifiles/jvmtiEnv.hpp"
 34 #include "logging/log.hpp"
 35 #include "memory/allocation.inline.hpp"
 36 #include "memory/iterator.hpp"
 37 #include "memory/resourceArea.hpp"
 38 #include "nmt/memTracker.hpp"
 39 #include "oops/oop.inline.hpp"
 40 #include "runtime/atomic.hpp"
 41 #include "runtime/handles.inline.hpp"
 42 #include "runtime/javaThread.inline.hpp"
 43 #include "runtime/nonJavaThread.hpp"
 44 #include "runtime/orderAccess.hpp"
 45 #include "runtime/osThread.hpp"
 46 #include "runtime/safepoint.hpp"
 47 #include "runtime/safepointMechanism.inline.hpp"
 48 #include "runtime/thread.inline.hpp"
 49 #include "runtime/threadSMR.inline.hpp"
 50 #include "utilities/macros.hpp"
 51 #include "utilities/spinYield.hpp"
 52 #if INCLUDE_JFR
 53 #include "jfr/jfr.hpp"
 54 #endif
 55 
 56 #ifndef USE_LIBRARY_BASED_TLS_ONLY
 57 // Current thread is maintained as a thread-local variable
 58 THREAD_LOCAL Thread* Thread::_thr_current = nullptr;
 59 #endif
 60 
 61 // ======= Thread ========
 62 // Base class for all threads: VMThread, WatcherThread, ConcurrentMarkSweepThread,
 63 // JavaThread
 64 
 65 DEBUG_ONLY(Thread* Thread::_starting_thread = nullptr;)
 66 
 67 Thread::Thread(MemTag mem_tag) {
 68 
 69   DEBUG_ONLY(_run_state = PRE_CALL_RUN;)
 70 
 71   // stack and get_thread
 72   set_stack_base(nullptr);
 73   set_stack_size(0);
 74   set_lgrp_id(-1);
 75   DEBUG_ONLY(clear_suspendible_thread();)
 76   DEBUG_ONLY(clear_indirectly_suspendible_thread();)
 77   DEBUG_ONLY(clear_indirectly_safepoint_thread();)
 78 
 79   // allocated data structures
 80   set_osthread(nullptr);
 81   set_resource_area(new (mem_tag) ResourceArea(mem_tag));
 82   DEBUG_ONLY(_current_resource_mark = nullptr;)
 83   set_handle_area(new (mem_tag) HandleArea(mem_tag, nullptr));
 84   set_metadata_handles(new (mtClass) GrowableArray<Metadata*>(30, mtClass));
 85   set_last_handle_mark(nullptr);
 86 
 87   // Initial value of zero ==> never claimed.
 88   _threads_do_token = 0;
 89   _threads_hazard_ptr = nullptr;
 90   _threads_list_ptr = nullptr;
 91   _nested_threads_hazard_ptr_cnt = 0;
 92   _rcu_counter = 0;
 93 
 94   // the handle mark links itself to last_handle_mark
 95   new HandleMark(this);
 96 
 97   // plain initialization
 98   debug_only(_owned_locks = nullptr;)
 99   NOT_PRODUCT(_skip_gcalot = false;)
100   _jvmti_env_iteration_count = 0;
101   set_allocated_bytes(0);
102   _current_pending_raw_monitor = nullptr;
103   _vm_error_callbacks = nullptr;
104 
105   // thread-specific hashCode stream generator state - Marsaglia shift-xor form
106   // If we are dumping, keep ihashes constant. Note that during dumping we only
107   // ever run one java thread, and no other thread should generate ihashes either,
108   // so using a constant seed should work fine.
109   _hashStateX = CDSConfig::is_dumping_static_archive() ? 0x12345678 : os::random();
110   _hashStateY = 842502087;
111   _hashStateZ = 0x8767;    // (int)(3579807591LL & 0xffff) ;
112   _hashStateW = 273326509;
113 
114   // Many of the following fields are effectively final - immutable
115   // Note that nascent threads can't use the Native Monitor-Mutex
116   // construct until the _MutexEvent is initialized ...
117   // CONSIDER: instead of using a fixed set of purpose-dedicated ParkEvents
118   // we might instead use a stack of ParkEvents that we could provision on-demand.
119   // The stack would act as a cache to avoid calls to ParkEvent::Allocate()
120   // and ::Release()
121   _ParkEvent   = ParkEvent::Allocate(this);
122 
123 #ifdef CHECK_UNHANDLED_OOPS
124   if (CheckUnhandledOops) {
125     _unhandled_oops = new UnhandledOops(this);
126   }
127 #endif // CHECK_UNHANDLED_OOPS
128 
129   // Notify the barrier set that a thread is being created. The initial
130   // thread is created before the barrier set is available.  The call to
131   // BarrierSet::on_thread_create() for this thread is therefore deferred
132   // to BarrierSet::set_barrier_set().
133   BarrierSet* const barrier_set = BarrierSet::barrier_set();
134   if (barrier_set != nullptr) {
135     barrier_set->on_thread_create(this);
136   } else {
137     // Only the main thread should be created before the barrier set
138     // and that happens just before Thread::current is set. No other thread
139     // can attach as the VM is not created yet, so they can't execute this code.
140     // If the main thread creates other threads before the barrier set that is an error.
141     assert(Thread::current_or_null() == nullptr, "creating thread before barrier set");
142   }
143 
144   MACOS_AARCH64_ONLY(DEBUG_ONLY(_wx_init = false));
145 
146   _profile_vm_locks = false;
147   _profile_vm_calls = false;
148   _profile_vm_ops   = false;
149   _profile_rt_calls = false;
150   _profile_upcalls  = false;
151 
152   _all_bc_counter_value = 0;
153   _clinit_bc_counter_value = 0;
154 
155   _current_rt_call_timer = nullptr;
156 }
157 
158 #ifdef ASSERT
159 address Thread::stack_base() const {
160   // Note: can't report Thread::name() here as that can require a ResourceMark which we
161   // can't use because this gets called too early in the thread initialization.
162   assert(_stack_base != nullptr, "Stack base not yet set for thread id:%d (0 if not set)",
163          osthread() != nullptr ? osthread()->thread_id() : 0);
164   return _stack_base;
165 }
166 #endif
167 
168 void Thread::initialize_tlab() {
169   if (UseTLAB) {
170     tlab().initialize();
171   }
172 }
173 
174 void Thread::initialize_thread_current() {
175 #ifndef USE_LIBRARY_BASED_TLS_ONLY
176   assert(_thr_current == nullptr, "Thread::current already initialized");
177   _thr_current = this;
178 #endif
179   assert(ThreadLocalStorage::thread() == nullptr, "ThreadLocalStorage::thread already initialized");
180   ThreadLocalStorage::set_thread(this);
181   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
182 }
183 
184 void Thread::clear_thread_current() {
185   assert(Thread::current() == ThreadLocalStorage::thread(), "TLS mismatch!");
186 #ifndef USE_LIBRARY_BASED_TLS_ONLY
187   _thr_current = nullptr;
188 #endif
189   ThreadLocalStorage::set_thread(nullptr);
190 }
191 
192 void Thread::record_stack_base_and_size() {
193   // Note: at this point, Thread object is not yet initialized. Do not rely on
194   // any members being initialized. Do not rely on Thread::current() being set.
195   // If possible, refrain from doing anything which may crash or assert since
196   // quite probably those crash dumps will be useless.
197   address base;
198   size_t size;
199   os::current_stack_base_and_size(&base, &size);
200   set_stack_base(base);
201   set_stack_size(size);
202 
203   // Set stack limits after thread is initialized.
204   if (is_Java_thread()) {
205     JavaThread::cast(this)->stack_overflow_state()->initialize(stack_base(), stack_end());
206   }
207 }
208 
209 void Thread::register_thread_stack_with_NMT() {
210   MemTracker::record_thread_stack(stack_end(), stack_size());
211 }
212 
213 void Thread::unregister_thread_stack_with_NMT() {
214   MemTracker::release_thread_stack(stack_end(), stack_size());
215 }
216 
217 void Thread::call_run() {
218   DEBUG_ONLY(_run_state = CALL_RUN;)
219 
220   // At this point, Thread object should be fully initialized and
221   // Thread::current() should be set.
222 
223   assert(Thread::current_or_null() != nullptr, "current thread is unset");
224   assert(Thread::current_or_null() == this, "current thread is wrong");
225 
226   // Perform common initialization actions
227 
228   MACOS_AARCH64_ONLY(this->init_wx());
229 
230   register_thread_stack_with_NMT();
231 
232   JFR_ONLY(Jfr::on_thread_start(this);)
233 
234   log_debug(os, thread)("Thread " UINTX_FORMAT " stack dimensions: "
235     PTR_FORMAT "-" PTR_FORMAT " (" SIZE_FORMAT "k).",
236     os::current_thread_id(), p2i(stack_end()),
237     p2i(stack_base()), stack_size()/1024);
238 
239   // Perform <ChildClass> initialization actions
240   DEBUG_ONLY(_run_state = PRE_RUN;)
241   this->pre_run();
242 
243   // Invoke <ChildClass>::run()
244   DEBUG_ONLY(_run_state = RUN;)
245   this->run();
246   // Returned from <ChildClass>::run(). Thread finished.
247 
248   // Perform common tear-down actions
249 
250   assert(Thread::current_or_null() != nullptr, "current thread is unset");
251   assert(Thread::current_or_null() == this, "current thread is wrong");
252 
253   // Perform <ChildClass> tear-down actions
254   DEBUG_ONLY(_run_state = POST_RUN;)
255   this->post_run();
256 
257   // Note: at this point the thread object may already have deleted itself,
258   // so from here on do not dereference *this*. Not all thread types currently
259   // delete themselves when they terminate. But no thread should ever be deleted
260   // asynchronously with respect to its termination - that is what _run_state can
261   // be used to check.
262 
263   assert(Thread::current_or_null() == nullptr, "current thread still present");
264 }
265 
266 Thread::~Thread() {
267 
268   // Attached threads will remain in PRE_CALL_RUN, as will threads that don't actually
269   // get started due to errors etc. Any active thread should at least reach post_run
270   // before it is deleted (usually in post_run()).
271   assert(_run_state == PRE_CALL_RUN ||
272          _run_state == POST_RUN, "Active Thread deleted before post_run(): "
273          "_run_state=%d", (int)_run_state);
274 
275   // Notify the barrier set that a thread is being destroyed. Note that a barrier
276   // set might not be available if we encountered errors during bootstrapping.
277   BarrierSet* const barrier_set = BarrierSet::barrier_set();
278   if (barrier_set != nullptr) {
279     barrier_set->on_thread_destroy(this);
280   }
281 
282   // deallocate data structures
283   delete resource_area();
284   // since the handle marks are using the handle area, we have to deallocated the root
285   // handle mark before deallocating the thread's handle area,
286   assert(last_handle_mark() != nullptr, "check we have an element");
287   delete last_handle_mark();
288   assert(last_handle_mark() == nullptr, "check we have reached the end");
289 
290   ParkEvent::Release(_ParkEvent);
291   // Set to null as a termination indicator for has_terminated().
292   Atomic::store(&_ParkEvent, (ParkEvent*)nullptr);
293 
294   delete handle_area();
295   delete metadata_handles();
296 
297   // osthread() can be null, if creation of thread failed.
298   if (osthread() != nullptr) os::free_thread(osthread());
299 
300   // Clear Thread::current if thread is deleting itself and it has not
301   // already been done. This must be done before the memory is deallocated.
302   // Needed to ensure JNI correctly detects non-attached threads.
303   if (this == Thread::current_or_null()) {
304     Thread::clear_thread_current();
305   }
306 
307   CHECK_UNHANDLED_OOPS_ONLY(if (CheckUnhandledOops) delete unhandled_oops();)
308 }
309 
310 #ifdef ASSERT
311 // A JavaThread is considered dangling if it not handshake-safe with respect to
312 // the current thread, it is not on a ThreadsList, or not at safepoint.
313 void Thread::check_for_dangling_thread_pointer(Thread *thread) {
314   assert(!thread->is_Java_thread() ||
315          JavaThread::cast(thread)->is_handshake_safe_for(Thread::current()) ||
316          !JavaThread::cast(thread)->on_thread_list() ||
317          SafepointSynchronize::is_at_safepoint() ||
318          ThreadsSMRSupport::is_a_protected_JavaThread_with_lock(JavaThread::cast(thread)),
319          "possibility of dangling Thread pointer");
320 }
321 #endif
322 
323 // Is the target JavaThread protected by the calling Thread or by some other
324 // mechanism?
325 //
326 bool Thread::is_JavaThread_protected(const JavaThread* target) {
327   Thread* current_thread = Thread::current();
328 
329   // Do the simplest check first:
330   if (SafepointSynchronize::is_at_safepoint()) {
331     // The target is protected since JavaThreads cannot exit
332     // while we're at a safepoint.
333     return true;
334   }
335 
336   // If the target hasn't been started yet then it is trivially
337   // "protected". We assume the caller is the thread that will do
338   // the starting.
339   if (target->osthread() == nullptr || target->osthread()->get_state() <= INITIALIZED) {
340     return true;
341   }
342 
343   // Now make the simple checks based on who the caller is:
344   if (current_thread == target || Threads_lock->owner() == current_thread) {
345     // Target JavaThread is self or calling thread owns the Threads_lock.
346     // Second check is the same as Threads_lock->owner_is_self(),
347     // but we already have the current thread so check directly.
348     return true;
349   }
350 
351   // Check the ThreadsLists associated with the calling thread (if any)
352   // to see if one of them protects the target JavaThread:
353   if (is_JavaThread_protected_by_TLH(target)) {
354     return true;
355   }
356 
357   // Use this debug code with -XX:+UseNewCode to diagnose locations that
358   // are missing a ThreadsListHandle or other protection mechanism:
359   // guarantee(!UseNewCode, "current_thread=" INTPTR_FORMAT " is not protecting target="
360   //           INTPTR_FORMAT, p2i(current_thread), p2i(target));
361 
362   // Note: Since 'target' isn't protected by a TLH, the call to
363   // target->is_handshake_safe_for() may crash, but we have debug bits so
364   // we'll be able to figure out what protection mechanism is missing.
365   assert(target->is_handshake_safe_for(current_thread), "JavaThread=" INTPTR_FORMAT
366          " is not protected and not handshake safe.", p2i(target));
367 
368   // The target JavaThread is not protected so it is not safe to query:
369   return false;
370 }
371 
372 // Is the target JavaThread protected by a ThreadsListHandle (TLH) associated
373 // with the calling Thread?
374 //
375 bool Thread::is_JavaThread_protected_by_TLH(const JavaThread* target) {
376   Thread* current_thread = Thread::current();
377 
378   // Check the ThreadsLists associated with the calling thread (if any)
379   // to see if one of them protects the target JavaThread:
380   for (SafeThreadsListPtr* stlp = current_thread->_threads_list_ptr;
381        stlp != nullptr; stlp = stlp->previous()) {
382     if (stlp->list()->includes(target)) {
383       // The target JavaThread is protected by this ThreadsList:
384       return true;
385     }
386   }
387 
388   // The target JavaThread is not protected by a TLH so it is not safe to query:
389   return false;
390 }
391 
392 void Thread::set_priority(Thread* thread, ThreadPriority priority) {
393   debug_only(check_for_dangling_thread_pointer(thread);)
394   // Can return an error!
395   (void)os::set_priority(thread, priority);
396 }
397 
398 
399 void Thread::start(Thread* thread) {
400   // Start is different from resume in that its safety is guaranteed by context or
401   // being called from a Java method synchronized on the Thread object.
402   if (thread->is_Java_thread()) {
403     // Initialize the thread state to RUNNABLE before starting this thread.
404     // Can not set it after the thread started because we do not know the
405     // exact thread state at that time. It could be in MONITOR_WAIT or
406     // in SLEEPING or some other state.
407     java_lang_Thread::set_thread_status(JavaThread::cast(thread)->threadObj(),
408                                         JavaThreadStatus::RUNNABLE);
409   }
410   os::start_thread(thread);
411 }
412 
413 // GC Support
414 bool Thread::claim_par_threads_do(uintx claim_token) {
415   uintx token = _threads_do_token;
416   if (token != claim_token) {
417     uintx res = Atomic::cmpxchg(&_threads_do_token, token, claim_token);
418     if (res == token) {
419       return true;
420     }
421     guarantee(res == claim_token, "invariant");
422   }
423   return false;
424 }
425 
426 void Thread::oops_do_no_frames(OopClosure* f, NMethodClosure* cf) {
427   // Do oop for ThreadShadow
428   f->do_oop((oop*)&_pending_exception);
429   handle_area()->oops_do(f);
430 }
431 
432 // If the caller is a NamedThread, then remember, in the current scope,
433 // the given JavaThread in its _processed_thread field.
434 class RememberProcessedThread: public StackObj {
435   NamedThread* _cur_thr;
436 public:
437   RememberProcessedThread(Thread* thread) {
438     Thread* self = Thread::current();
439     if (self->is_Named_thread()) {
440       _cur_thr = (NamedThread *)self;
441       assert(_cur_thr->processed_thread() == nullptr, "nesting not supported");
442       _cur_thr->set_processed_thread(thread);
443     } else {
444       _cur_thr = nullptr;
445     }
446   }
447 
448   ~RememberProcessedThread() {
449     if (_cur_thr) {
450       assert(_cur_thr->processed_thread() != nullptr, "nesting not supported");
451       _cur_thr->set_processed_thread(nullptr);
452     }
453   }
454 };
455 
456 void Thread::oops_do(OopClosure* f, NMethodClosure* cf) {
457   // Record JavaThread to GC thread
458   RememberProcessedThread rpt(this);
459   oops_do_no_frames(f, cf);
460   oops_do_frames(f, cf);
461 }
462 
463 void Thread::metadata_handles_do(void f(Metadata*)) {
464   // Only walk the Handles in Thread.
465   if (metadata_handles() != nullptr) {
466     for (int i = 0; i< metadata_handles()->length(); i++) {
467       f(metadata_handles()->at(i));
468     }
469   }
470 }
471 
472 void Thread::print_on(outputStream* st, bool print_extended_info) const {
473   // get_priority assumes osthread initialized
474   if (osthread() != nullptr) {
475     int os_prio;
476     if (os::get_native_priority(this, &os_prio) == OS_OK) {
477       st->print("os_prio=%d ", os_prio);
478     }
479 
480     st->print("cpu=%.2fms ",
481               (double)os::thread_cpu_time(const_cast<Thread*>(this), true) / 1000000.0
482               );
483     st->print("elapsed=%.2fs ",
484               (double)_statistical_info.getElapsedTime() / 1000.0
485               );
486     if (is_Java_thread() && (PrintExtendedThreadInfo || print_extended_info)) {
487       size_t allocated_bytes = (size_t) const_cast<Thread*>(this)->cooked_allocated_bytes();
488       st->print("allocated=" SIZE_FORMAT "%s ",
489                 byte_size_in_proper_unit(allocated_bytes),
490                 proper_unit_for_byte_size(allocated_bytes)
491                 );
492       st->print("defined_classes=" INT64_FORMAT " ", _statistical_info.getDefineClassCount());
493     }
494 
495     st->print("tid=" INTPTR_FORMAT " ", p2i(this));
496     if (!is_Java_thread() || !JavaThread::cast(this)->is_vthread_mounted()) {
497       osthread()->print_on(st);
498     }
499   }
500   ThreadsSMRSupport::print_info_on(this, st);
501   st->print(" ");
502   debug_only(if (WizardMode) print_owned_locks_on(st);)
503 }
504 
505 void Thread::print() const { print_on(tty); }
506 
507 // Thread::print_on_error() is called by fatal error handler. Don't use
508 // any lock or allocate memory.
509 void Thread::print_on_error(outputStream* st, char* buf, int buflen) const {
510   assert(!(is_Compiler_thread() || is_Java_thread()), "Can't call name() here if it allocates");
511 
512   st->print("%s \"%s\"", type_name(), name());
513 
514   OSThread* os_thr = osthread();
515   if (os_thr != nullptr) {
516     st->fill_to(67);
517     if (os_thr->get_state() != ZOMBIE) {
518       // Use raw field members for stack base/size as this could be
519       // called before a thread has run enough to initialize them.
520       st->print(" [id=%d, stack(" PTR_FORMAT "," PTR_FORMAT ") (" PROPERFMT ")]",
521                 osthread()->thread_id(), p2i(_stack_base - _stack_size), p2i(_stack_base),
522                 PROPERFMTARGS(_stack_size));
523     } else {
524       st->print(" terminated");
525     }
526   } else {
527     st->print(" unknown state (no osThread)");
528   }
529   ThreadsSMRSupport::print_info_on(this, st);
530 }
531 
532 void Thread::print_value_on(outputStream* st) const {
533   if (is_Named_thread()) {
534     st->print(" \"%s\" ", name());
535   }
536   st->print(INTPTR_FORMAT, p2i(this));   // print address
537 }
538 
539 #ifdef ASSERT
540 void Thread::print_owned_locks_on(outputStream* st) const {
541   Mutex* cur = _owned_locks;
542   if (cur == nullptr) {
543     st->print(" (no locks) ");
544   } else {
545     st->print_cr(" Locks owned:");
546     while (cur) {
547       cur->print_on(st);
548       cur = cur->next();
549     }
550   }
551 }
552 #endif // ASSERT
553 
554 bool Thread::set_as_starting_thread() {
555   assert(_starting_thread == nullptr, "already initialized: "
556          "_starting_thread=" INTPTR_FORMAT, p2i(_starting_thread));
557   // NOTE: this must be called inside the main thread.
558   DEBUG_ONLY(_starting_thread = this;)
559   return os::create_main_thread(JavaThread::cast(this));
560 }
561 
562 // Ad-hoc mutual exclusion primitives: SpinLock
563 //
564 // We employ SpinLocks _only for low-contention, fixed-length
565 // short-duration critical sections where we're concerned
566 // about native mutex_t or HotSpot Mutex:: latency.
567 //
568 // TODO-FIXME: ListLock should be of type SpinLock.
569 // We should make this a 1st-class type, integrated into the lock
570 // hierarchy as leaf-locks.  Critically, the SpinLock structure
571 // should have sufficient padding to avoid false-sharing and excessive
572 // cache-coherency traffic.
573 
574 
575 typedef volatile int SpinLockT;
576 
577 void Thread::SpinAcquire(volatile int * adr, const char * LockName) {
578   if (Atomic::cmpxchg(adr, 0, 1) == 0) {
579     return;   // normal fast-path return
580   }
581 
582   // Slow-path : We've encountered contention -- Spin/Yield/Block strategy.
583   int ctr = 0;
584   int Yields = 0;
585   for (;;) {
586     while (*adr != 0) {
587       ++ctr;
588       if ((ctr & 0xFFF) == 0 || !os::is_MP()) {
589         if (Yields > 5) {
590           os::naked_short_sleep(1);
591         } else {
592           os::naked_yield();
593           ++Yields;
594         }
595       } else {
596         SpinPause();
597       }
598     }
599     if (Atomic::cmpxchg(adr, 0, 1) == 0) return;
600   }
601 }
602 
603 void Thread::SpinRelease(volatile int * adr) {
604   assert(*adr != 0, "invariant");
605   OrderAccess::fence();      // guarantee at least release consistency.
606   // Roach-motel semantics.
607   // It's safe if subsequent LDs and STs float "up" into the critical section,
608   // but prior LDs and STs within the critical section can't be allowed
609   // to reorder or float past the ST that releases the lock.
610   // Loads and stores in the critical section - which appear in program
611   // order before the store that releases the lock - must also appear
612   // before the store that releases the lock in memory visibility order.
613   // Conceptually we need a #loadstore|#storestore "release" MEMBAR before
614   // the ST of 0 into the lock-word which releases the lock, so fence
615   // more than covers this on all platforms.
616   *adr = 0;
617 }
618 
619 const char* ProfileVMCallContext::name(PerfTraceTime* t) {
620   return t->name();
621 }
622 
623 int ProfileVMCallContext::_perf_nested_runtime_calls_count = 0;
624 
625 void ProfileVMCallContext::notify_nested_rt_call(PerfTraceTime* outer_timer, PerfTraceTime* inner_timer) {
626   log_debug(init)("Nested runtime call: inner=%s outer=%s", inner_timer->name(), outer_timer->name());
627   Atomic::inc(&ProfileVMCallContext::_perf_nested_runtime_calls_count);
628 }
629