1 /*
   2  * Copyright (c) 2012, 2014, 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 #define SHARE_VM_RUNTIME_THREAD_INLINE_HPP_SCOPE
  29 
  30 #include "runtime/atomic.inline.hpp"
  31 #include "runtime/os.inline.hpp"
  32 #include "runtime/thread.hpp"
  33 #ifdef TARGET_OS_FAMILY_linux
  34 # include "thread_linux.inline.hpp"
  35 #endif
  36 #ifdef TARGET_OS_FAMILY_solaris
  37 # include "thread_solaris.inline.hpp"
  38 #endif
  39 #ifdef TARGET_OS_FAMILY_windows
  40 # include "thread_windows.inline.hpp"
  41 #endif
  42 #ifdef TARGET_OS_FAMILY_aix
  43 # include "thread_aix.inline.hpp"
  44 #endif
  45 #ifdef TARGET_OS_FAMILY_bsd
  46 # include "thread_bsd.inline.hpp"
  47 #endif
  48 
  49 #undef SHARE_VM_RUNTIME_THREAD_INLINE_HPP_SCOPE
  50 
  51 inline void Thread::set_suspend_flag(SuspendFlags f) {
  52   assert(sizeof(jint) == sizeof(_suspend_flags), "size mismatch");
  53   uint32_t flags;
  54   do {
  55     flags = _suspend_flags;
  56   }
  57   while (Atomic::cmpxchg((jint)(flags | f),
  58                          (volatile jint*)&_suspend_flags,
  59                          (jint)flags) != (jint)flags);
  60 }
  61 inline void Thread::clear_suspend_flag(SuspendFlags f) {
  62   assert(sizeof(jint) == sizeof(_suspend_flags), "size mismatch");
  63   uint32_t flags;
  64   do {
  65     flags = _suspend_flags;
  66   }
  67   while (Atomic::cmpxchg((jint)(flags & ~f),
  68                          (volatile jint*)&_suspend_flags,
  69                          (jint)flags) != (jint)flags);
  70 }
  71 
  72 inline void Thread::set_has_async_exception() {
  73   set_suspend_flag(_has_async_exception);
  74 }
  75 inline void Thread::clear_has_async_exception() {
  76   clear_suspend_flag(_has_async_exception);
  77 }
  78 inline void Thread::set_critical_native_unlock() {
  79   set_suspend_flag(_critical_native_unlock);
  80 }
  81 inline void Thread::clear_critical_native_unlock() {
  82   clear_suspend_flag(_critical_native_unlock);
  83 }
  84 
  85 inline jlong Thread::cooked_allocated_bytes() {
  86   jlong allocated_bytes = OrderAccess::load_acquire(&_allocated_bytes);
  87   if (UseTLAB) {
  88     size_t used_bytes = tlab().used_bytes();
  89     if ((ssize_t)used_bytes > 0) {
  90       // More-or-less valid tlab. The load_acquire above should ensure
  91       // that the result of the add is <= the instantaneous value.
  92       return allocated_bytes + used_bytes;
  93     }
  94   }
  95   return allocated_bytes;
  96 }
  97 
  98 inline void JavaThread::set_ext_suspended() {
  99   set_suspend_flag (_ext_suspended);
 100 }
 101 inline void JavaThread::clear_ext_suspended() {
 102   clear_suspend_flag(_ext_suspended);
 103 }
 104 
 105 inline void JavaThread::set_external_suspend() {
 106   set_suspend_flag(_external_suspend);
 107 }
 108 inline void JavaThread::clear_external_suspend() {
 109   clear_suspend_flag(_external_suspend);
 110 }
 111 
 112 inline void JavaThread::set_deopt_suspend() {
 113   set_suspend_flag(_deopt_suspend);
 114 }
 115 inline void JavaThread::clear_deopt_suspend() {
 116   clear_suspend_flag(_deopt_suspend);
 117 }
 118 
 119 inline void JavaThread::set_pending_async_exception(oop e) {
 120   _pending_async_exception = e;
 121   _special_runtime_exit_condition = _async_exception;
 122   set_has_async_exception();
 123 }
 124 
 125 #if defined(PPC64) || defined (AARCH64)
 126 inline JavaThreadState JavaThread::thread_state() const    {
 127   return (JavaThreadState) OrderAccess::load_acquire((volatile jint*)&_thread_state);
 128 }
 129 
 130 inline void JavaThread::set_thread_state(JavaThreadState s) {
 131   OrderAccess::release_store((volatile jint*)&_thread_state, (jint)s);
 132 }
 133 #endif
 134 
 135 inline void JavaThread::set_done_attaching_via_jni() {
 136   _jni_attach_state = _attached_via_jni;
 137   OrderAccess::fence();
 138 }
 139 
 140 inline bool JavaThread::stack_guard_zone_unused() {
 141   return _stack_guard_state == stack_guard_unused;
 142 }
 143 
 144 inline bool JavaThread::stack_yellow_zone_disabled() {
 145   return _stack_guard_state == stack_guard_yellow_disabled;
 146 }
 147 
 148 inline size_t JavaThread::stack_available(address cur_sp) {
 149   // This code assumes java stacks grow down
 150   address low_addr; // Limit on the address for deepest stack depth
 151   if (_stack_guard_state == stack_guard_unused) {
 152     low_addr =  stack_base() - stack_size();
 153   } else {
 154     low_addr = stack_yellow_zone_base();
 155   }
 156   return cur_sp > low_addr ? cur_sp - low_addr : 0;
 157 }
 158 
 159 inline bool JavaThread::stack_yellow_zone_enabled() {
 160 #ifdef ASSERT
 161   if (os::uses_stack_guard_pages()) {
 162     assert(_stack_guard_state != stack_guard_unused, "guard pages must be in use");
 163   }
 164 #endif
 165   return _stack_guard_state == stack_guard_enabled;
 166 }
 167 
 168 #endif // SHARE_VM_RUNTIME_THREAD_INLINE_HPP