1 /*
   2  * Copyright (c) 2012, 2018, 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/orderAccess.inline.hpp"
  30 #include "runtime/os.inline.hpp"
  31 #include "runtime/thread.hpp"
  32 
  33 inline void Thread::set_suspend_flag(SuspendFlags f) {
  34   uint32_t flags;
  35   do {
  36     flags = _suspend_flags;
  37   }
  38   while (Atomic::cmpxchg((flags | f), &_suspend_flags, flags) != flags);
  39 }
  40 inline void Thread::clear_suspend_flag(SuspendFlags f) {
  41   uint32_t flags;
  42   do {
  43     flags = _suspend_flags;
  44   }
  45   while (Atomic::cmpxchg((flags & ~f), &_suspend_flags, flags) != flags);
  46 }
  47 
  48 inline void Thread::set_has_async_exception() {
  49   set_suspend_flag(_has_async_exception);
  50 }
  51 inline void Thread::clear_has_async_exception() {
  52   clear_suspend_flag(_has_async_exception);
  53 }
  54 inline void Thread::set_critical_native_unlock() {
  55   set_suspend_flag(_critical_native_unlock);
  56 }
  57 inline void Thread::clear_critical_native_unlock() {
  58   clear_suspend_flag(_critical_native_unlock);
  59 }
  60 inline void Thread::set_trace_flag() {
  61   set_suspend_flag(_trace_flag);
  62 }
  63 inline void Thread::clear_trace_flag() {
  64   clear_suspend_flag(_trace_flag);
  65 }
  66 
  67 inline jlong Thread::cooked_allocated_bytes() {
  68   jlong allocated_bytes = OrderAccess::load_acquire(&_allocated_bytes);
  69   if (UseTLAB) {
  70     size_t used_bytes = tlab().used_bytes();
  71     if (used_bytes <= ThreadLocalAllocBuffer::max_size_in_bytes()) {
  72       // Comparing used_bytes with the maximum allowed size will ensure
  73       // that we don't add the used bytes from a semi-initialized TLAB
  74       // ending up with incorrect values. There is still a race between
  75       // incrementing _allocated_bytes and clearing the TLAB, that might
  76       // cause double counting in rare cases.
  77       return allocated_bytes + used_bytes;
  78     }
  79   }
  80   return allocated_bytes;
  81 }
  82 
  83 inline ThreadsList* Thread::cmpxchg_threads_hazard_ptr(ThreadsList* exchange_value, ThreadsList* compare_value) {
  84   return (ThreadsList*)Atomic::cmpxchg(exchange_value, &_threads_hazard_ptr, compare_value);
  85 }
  86 
  87 inline ThreadsList* Thread::get_threads_hazard_ptr() {
  88   return (ThreadsList*)OrderAccess::load_acquire(&_threads_hazard_ptr);
  89 }
  90 
  91 inline void Thread::set_threads_hazard_ptr(ThreadsList* new_list) {
  92   OrderAccess::release_store_fence(&_threads_hazard_ptr, new_list);
  93 }
  94 
  95 inline void JavaThread::set_ext_suspended() {
  96   set_suspend_flag (_ext_suspended);
  97 }
  98 inline void JavaThread::clear_ext_suspended() {
  99   clear_suspend_flag(_ext_suspended);
 100 }
 101 
 102 inline void JavaThread::set_external_suspend() {
 103   set_suspend_flag(_external_suspend);
 104 }
 105 inline void JavaThread::clear_external_suspend() {
 106   clear_suspend_flag(_external_suspend);
 107 }
 108 
 109 inline void JavaThread::set_deopt_suspend() {
 110   set_suspend_flag(_deopt_suspend);
 111 }
 112 inline void JavaThread::clear_deopt_suspend() {
 113   clear_suspend_flag(_deopt_suspend);
 114 }
 115 
 116 inline void JavaThread::set_pending_async_exception(oop e) {
 117   _pending_async_exception = e;
 118   _special_runtime_exit_condition = _async_exception;
 119   set_has_async_exception();
 120 }
 121 
 122 #if defined(PPC64) || defined (AARCH64)
 123 inline JavaThreadState JavaThread::thread_state() const    {
 124   return (JavaThreadState) OrderAccess::load_acquire((volatile jint*)&_thread_state);
 125 }
 126 
 127 inline void JavaThread::set_thread_state(JavaThreadState s) {
 128   OrderAccess::release_store((volatile jint*)&_thread_state, (jint)s);
 129 }
 130 #endif
 131 
 132 inline void JavaThread::set_done_attaching_via_jni() {
 133   _jni_attach_state = _attached_via_jni;
 134   OrderAccess::fence();
 135 }
 136 
 137 inline bool JavaThread::stack_guard_zone_unused() {
 138   return _stack_guard_state == stack_guard_unused;
 139 }
 140 
 141 inline bool JavaThread::stack_yellow_reserved_zone_disabled() {
 142   return _stack_guard_state == stack_guard_yellow_reserved_disabled;
 143 }
 144 
 145 inline bool JavaThread::stack_reserved_zone_disabled() {
 146   return _stack_guard_state == stack_guard_reserved_disabled;
 147 }
 148 
 149 inline size_t JavaThread::stack_available(address cur_sp) {
 150   // This code assumes java stacks grow down
 151   address low_addr; // Limit on the address for deepest stack depth
 152   if (_stack_guard_state == stack_guard_unused) {
 153     low_addr = stack_end();
 154   } else {
 155     low_addr = stack_reserved_zone_base();
 156   }
 157   return cur_sp > low_addr ? cur_sp - low_addr : 0;
 158 }
 159 
 160 inline bool JavaThread::stack_guards_enabled() {
 161 #ifdef ASSERT
 162   if (os::uses_stack_guard_pages() &&
 163       !(DisablePrimordialThreadGuardPages && os::is_primordial_thread())) {
 164     assert(_stack_guard_state != stack_guard_unused, "guard pages must be in use");
 165   }
 166 #endif
 167   return _stack_guard_state == stack_guard_enabled;
 168 }
 169 
 170 // The release make sure this store is done after storing the handshake
 171 // operation or global state
 172 inline void JavaThread::set_polling_page(void* poll_value) {
 173   OrderAccess::release_store(polling_page_addr(), poll_value);
 174 }
 175 
 176 // The aqcquire make sure reading of polling page is done before
 177 // the reading the handshake operation or the global state
 178 inline volatile void* JavaThread::get_polling_page() {
 179   return OrderAccess::load_acquire(polling_page_addr());
 180 }
 181 
 182 inline bool JavaThread::is_exiting() const {
 183   // Use load-acquire so that setting of _terminated by
 184   // JavaThread::exit() is seen more quickly.
 185   TerminatedTypes l_terminated = (TerminatedTypes)
 186       OrderAccess::load_acquire((volatile jint *) &_terminated);
 187   return l_terminated == _thread_exiting || check_is_terminated(l_terminated);
 188 }
 189 
 190 inline bool JavaThread::is_terminated() const {
 191   // Use load-acquire so that setting of _terminated by
 192   // JavaThread::exit() is seen more quickly.
 193   TerminatedTypes l_terminated = (TerminatedTypes)
 194       OrderAccess::load_acquire((volatile jint *) &_terminated);
 195   return check_is_terminated(l_terminated);
 196 }
 197 
 198 inline void JavaThread::set_terminated(TerminatedTypes t) {
 199   // use release-store so the setting of _terminated is seen more quickly
 200   OrderAccess::release_store((volatile jint *) &_terminated, (jint) t);
 201 }
 202 
 203 // special for Threads::remove() which is static:
 204 inline void JavaThread::set_terminated_value() {
 205   // use release-store so the setting of _terminated is seen more quickly
 206   OrderAccess::release_store((volatile jint *) &_terminated, (jint) _thread_terminated);
 207 }
 208 
 209 #endif // SHARE_VM_RUNTIME_THREAD_INLINE_HPP