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