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