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