1 /*
   2  * Copyright (c) 2012, 2016, 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.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 (used_bytes <= ThreadLocalAllocBuffer::max_size_in_bytes()) {
  75       // Comparing used_bytes with the maximum allowed size will ensure
  76       // that we don't add the used bytes from a semi-initialized TLAB
  77       // ending up with incorrect values. There is still a race between
  78       // incrementing _allocated_bytes and clearing the TLAB, that might
  79       // cause double counting in rare cases.
  80       return allocated_bytes + used_bytes;
  81     }
  82   }
  83   return allocated_bytes;
  84 }
  85 
  86 inline void JavaThread::set_ext_suspended() {
  87   set_suspend_flag (_ext_suspended);
  88 }
  89 inline void JavaThread::clear_ext_suspended() {
  90   clear_suspend_flag(_ext_suspended);
  91 }
  92 
  93 inline void JavaThread::set_external_suspend() {
  94   set_suspend_flag(_external_suspend);
  95 }
  96 inline void JavaThread::clear_external_suspend() {
  97   clear_suspend_flag(_external_suspend);
  98 }
  99 
 100 inline void JavaThread::set_deopt_suspend() {
 101   set_suspend_flag(_deopt_suspend);
 102 }
 103 inline void JavaThread::clear_deopt_suspend() {
 104   clear_suspend_flag(_deopt_suspend);
 105 }
 106 
 107 inline void JavaThread::set_pending_async_exception(oop e) {
 108   _pending_async_exception = e;
 109   _special_runtime_exit_condition = _async_exception;
 110   set_has_async_exception();
 111 }
 112 
 113 #if defined(PPC64) || defined (AARCH64)
 114 inline JavaThreadState JavaThread::thread_state() const    {
 115   return (JavaThreadState) OrderAccess::load_acquire((volatile jint*)&_thread_state);
 116 }
 117 
 118 inline void JavaThread::set_thread_state(JavaThreadState s) {
 119   OrderAccess::release_store((volatile jint*)&_thread_state, (jint)s);
 120 }
 121 #endif
 122 
 123 inline void JavaThread::set_done_attaching_via_jni() {
 124   _jni_attach_state = _attached_via_jni;
 125   OrderAccess::fence();
 126 }
 127 
 128 inline bool JavaThread::stack_guard_zone_unused() {
 129   return _stack_guard_state == stack_guard_unused;
 130 }
 131 
 132 inline bool JavaThread::stack_yellow_reserved_zone_disabled() {
 133   return _stack_guard_state == stack_guard_yellow_reserved_disabled;
 134 }
 135 
 136 inline bool JavaThread::stack_reserved_zone_disabled() {
 137   return _stack_guard_state == stack_guard_reserved_disabled;
 138 }
 139 
 140 inline size_t JavaThread::stack_available(address cur_sp) {
 141   // This code assumes java stacks grow down
 142   address low_addr; // Limit on the address for deepest stack depth
 143   if (_stack_guard_state == stack_guard_unused) {
 144     low_addr = stack_end();
 145   } else {
 146     low_addr = stack_reserved_zone_base();
 147   }
 148   return cur_sp > low_addr ? cur_sp - low_addr : 0;
 149 }
 150 
 151 inline bool JavaThread::stack_guards_enabled() {
 152 #ifdef ASSERT
 153   if (os::uses_stack_guard_pages()) {
 154     assert(_stack_guard_state != stack_guard_unused, "guard pages must be in use");
 155   }
 156 #endif
 157   return _stack_guard_state == stack_guard_enabled;
 158 }
 159 
 160 #endif // SHARE_VM_RUNTIME_THREAD_INLINE_HPP