1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_RUNTIME_THREAD_INLINE_HPP
  26 #define SHARE_VM_RUNTIME_THREAD_INLINE_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/os.inline.hpp"
  30 #include "runtime/thread.hpp"
  31 #include "runtime/threadSMR.hpp"
  32 
  33 inline void Thread::set_suspend_flag(SuspendFlags f) {
  34   assert(sizeof(jint) == sizeof(_suspend_flags), "size mismatch");
  35   uint32_t flags;
  36   do {
  37     flags = _suspend_flags;
  38   }
  39   while (Atomic::cmpxchg((jint)(flags | f),
  40                          (volatile jint*)&_suspend_flags,
  41                          (jint)flags) != (jint)flags);
  42 }
  43 inline void Thread::clear_suspend_flag(SuspendFlags f) {
  44   assert(sizeof(jint) == sizeof(_suspend_flags), "size mismatch");
  45   uint32_t flags;
  46   do {
  47     flags = _suspend_flags;
  48   }
  49   while (Atomic::cmpxchg((jint)(flags & ~f),
  50                          (volatile jint*)&_suspend_flags,
  51                          (jint)flags) != (jint)flags);
  52 }
  53 
  54 inline void Thread::set_has_async_exception() {
  55   set_suspend_flag(_has_async_exception);
  56 }
  57 inline void Thread::clear_has_async_exception() {
  58   clear_suspend_flag(_has_async_exception);
  59 }
  60 inline void Thread::set_critical_native_unlock() {
  61   set_suspend_flag(_critical_native_unlock);
  62 }
  63 inline void Thread::clear_critical_native_unlock() {
  64   clear_suspend_flag(_critical_native_unlock);
  65 }
  66 inline void Thread::set_trace_flag() {
  67   set_suspend_flag(_trace_flag);
  68 }
  69 inline void Thread::clear_trace_flag() {
  70   clear_suspend_flag(_trace_flag);
  71 }
  72 
  73 inline jlong Thread::cooked_allocated_bytes() {
  74   jlong allocated_bytes = OrderAccess::load_acquire(&_allocated_bytes);
  75   if (UseTLAB) {
  76     size_t used_bytes = tlab().used_bytes();
  77     if (used_bytes <= ThreadLocalAllocBuffer::max_size_in_bytes()) {
  78       // Comparing used_bytes with the maximum allowed size will ensure
  79       // that we don't add the used bytes from a semi-initialized TLAB
  80       // ending up with incorrect values. There is still a race between
  81       // incrementing _allocated_bytes and clearing the TLAB, that might
  82       // cause double counting in rare cases.
  83       return allocated_bytes + used_bytes;
  84     }
  85   }
  86   return allocated_bytes;
  87 }
  88 
  89 inline ThreadsList* Thread::cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value) {
  90   return (ThreadsList*)Atomic::cmpxchg(exchange_value, &_threads_hazard_ptr, compare_value);
  91 }
  92 
  93 inline ThreadsList* Thread::get_threads_hazard_ptr() {
  94   return (ThreadsList*)OrderAccess::load_acquire(&_threads_hazard_ptr);
  95 }
  96 
  97 inline void Thread::set_threads_hazard_ptr(ThreadsList* new_list) {
  98   OrderAccess::release_store_fence(&_threads_hazard_ptr, new_list);
  99 }
 100 
 101 inline void JavaThread::set_ext_suspended() {
 102   set_suspend_flag (_ext_suspended);
 103 }
 104 inline void JavaThread::clear_ext_suspended() {
 105   clear_suspend_flag(_ext_suspended);
 106 }
 107 
 108 inline void JavaThread::set_external_suspend() {
 109   set_suspend_flag(_external_suspend);
 110 }
 111 inline void JavaThread::clear_external_suspend() {
 112   clear_suspend_flag(_external_suspend);
 113 }
 114 
 115 inline void JavaThread::set_deopt_suspend() {
 116   set_suspend_flag(_deopt_suspend);
 117 }
 118 inline void JavaThread::clear_deopt_suspend() {
 119   clear_suspend_flag(_deopt_suspend);
 120 }
 121 
 122 inline void JavaThread::set_pending_async_exception(oop e) {
 123   _pending_async_exception = e;
 124   _special_runtime_exit_condition = _async_exception;
 125   set_has_async_exception();
 126 }
 127 
 128 #if defined(PPC64) || defined (AARCH64)
 129 inline JavaThreadState JavaThread::thread_state() const    {
 130   return (JavaThreadState) OrderAccess::load_acquire((volatile jint*)&_thread_state);
 131 }
 132 
 133 inline void JavaThread::set_thread_state(JavaThreadState s) {
 134   OrderAccess::release_store((volatile jint*)&_thread_state, (jint)s);
 135 }
 136 #endif
 137 
 138 inline void JavaThread::set_done_attaching_via_jni() {
 139   _jni_attach_state = _attached_via_jni;
 140   OrderAccess::fence();
 141 }
 142 
 143 inline bool JavaThread::stack_guard_zone_unused() {
 144   return _stack_guard_state == stack_guard_unused;
 145 }
 146 
 147 inline bool JavaThread::stack_yellow_reserved_zone_disabled() {
 148   return _stack_guard_state == stack_guard_yellow_reserved_disabled;
 149 }
 150 
 151 inline bool JavaThread::stack_reserved_zone_disabled() {
 152   return _stack_guard_state == stack_guard_reserved_disabled;
 153 }
 154 
 155 inline size_t JavaThread::stack_available(address cur_sp) {
 156   // This code assumes java stacks grow down
 157   address low_addr; // Limit on the address for deepest stack depth
 158   if (_stack_guard_state == stack_guard_unused) {
 159     low_addr = stack_end();
 160   } else {
 161     low_addr = stack_reserved_zone_base();
 162   }
 163   return cur_sp > low_addr ? cur_sp - low_addr : 0;
 164 }
 165 
 166 inline bool JavaThread::stack_guards_enabled() {
 167 #ifdef ASSERT
 168   if (os::uses_stack_guard_pages() &&
 169       !(DisablePrimordialThreadGuardPages && os::is_primordial_thread())) {
 170     assert(_stack_guard_state != stack_guard_unused, "guard pages must be in use");
 171   }
 172 #endif
 173   return _stack_guard_state == stack_guard_enabled;
 174 }
 175 
 176 // The release make sure this store is done after storing the handshake
 177 // operation or global state
 178 inline void JavaThread::set_polling_page(void* poll_value) {
 179   OrderAccess::release_store(polling_page_addr(), poll_value);
 180 }
 181 
 182 // The aqcquire make sure reading of polling page is done before
 183 // the reading the handshake operation or the global state
 184 inline volatile void* JavaThread::get_polling_page() {
 185   return OrderAccess::load_acquire(polling_page_addr());
 186 }
 187 
 188 inline bool JavaThread::is_exiting() const {
 189   // Use load-acquire so that setting of _terminated by
 190   // JavaThread::exit() is seen more quickly.
 191   TerminatedTypes l_terminated = (TerminatedTypes)
 192       OrderAccess::load_acquire((volatile jint *) &_terminated);
 193   return l_terminated == _thread_exiting || check_is_terminated(l_terminated);
 194 }
 195 
 196 inline bool JavaThread::is_terminated() const {
 197   // Use load-acquire so that setting of _terminated by
 198   // JavaThread::exit() is seen more quickly.
 199   TerminatedTypes l_terminated = (TerminatedTypes)
 200       OrderAccess::load_acquire((volatile jint *) &_terminated);
 201   return check_is_terminated(l_terminated);
 202 }
 203 
 204 inline void JavaThread::set_terminated(TerminatedTypes t) {
 205   // use release-store so the setting of _terminated is seen more quickly
 206   OrderAccess::release_store((volatile jint *) &_terminated, (jint) t);
 207 }
 208 
 209 // special for Threads::remove() which is static:
 210 inline void JavaThread::set_terminated_value() {
 211   // use release-store so the setting of _terminated is seen more quickly
 212   OrderAccess::release_store((volatile jint *) &_terminated, (jint) _thread_terminated);
 213 }
 214 
 215 inline void Threads::add_smr_tlh_times(uint add_value) {
 216   Atomic::add(add_value, &_smr_tlh_times);
 217 }
 218 
 219 inline void Threads::inc_smr_tlh_cnt() {
 220   Atomic::inc(&_smr_tlh_cnt);
 221 }
 222 
 223 inline void Threads::update_smr_tlh_time_max(uint new_value) {
 224   while (true) {
 225     uint cur_value = _smr_tlh_time_max;
 226     if (new_value <= cur_value) {
 227       // No need to update max value so we're done.
 228       break;
 229     }
 230     if (Atomic::cmpxchg(new_value, &_smr_tlh_time_max, cur_value) == cur_value) {
 231       // Updated max value so we're done. Otherwise try it all again.
 232       break;
 233     }
 234   }
 235 }
 236 
 237 #endif // SHARE_VM_RUNTIME_THREAD_INLINE_HPP