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