1 /*
   2  * Copyright (c) 2012, 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/thread.hpp"
  32 #ifdef TARGET_OS_FAMILY_linux
  33 # include "thread_linux.inline.hpp"
  34 #endif
  35 #ifdef TARGET_OS_FAMILY_solaris
  36 # include "thread_solaris.inline.hpp"
  37 #endif
  38 #ifdef TARGET_OS_FAMILY_windows
  39 # include "thread_windows.inline.hpp"
  40 #endif
  41 #ifdef TARGET_OS_FAMILY_aix
  42 # include "thread_aix.inline.hpp"
  43 #endif
  44 #ifdef TARGET_OS_FAMILY_bsd
  45 # include "thread_bsd.inline.hpp"
  46 #endif
  47 
  48 #undef SHARE_VM_RUNTIME_THREAD_INLINE_HPP_SCOPE
  49 
  50 inline void Thread::set_suspend_flag(SuspendFlags f) {
  51   assert(sizeof(jint) == sizeof(_suspend_flags), "size mismatch");
  52   uint32_t flags;
  53   do {
  54     flags = _suspend_flags;
  55   }
  56   while (Atomic::cmpxchg((jint)(flags | f),
  57                          (volatile jint*)&_suspend_flags,
  58                          (jint)flags) != (jint)flags);
  59 }
  60 inline void Thread::clear_suspend_flag(SuspendFlags f) {
  61   assert(sizeof(jint) == sizeof(_suspend_flags), "size mismatch");
  62   uint32_t flags;
  63   do {
  64     flags = _suspend_flags;
  65   }
  66   while (Atomic::cmpxchg((jint)(flags & ~f),
  67                          (volatile jint*)&_suspend_flags,
  68                          (jint)flags) != (jint)flags);
  69 }
  70 
  71 inline void Thread::set_has_async_exception() {
  72   set_suspend_flag(_has_async_exception);
  73 }
  74 inline void Thread::clear_has_async_exception() {
  75   clear_suspend_flag(_has_async_exception);
  76 }
  77 inline void Thread::set_critical_native_unlock() {
  78   set_suspend_flag(_critical_native_unlock);
  79 }
  80 inline void Thread::clear_critical_native_unlock() {
  81   clear_suspend_flag(_critical_native_unlock);
  82 }
  83 
  84 inline jlong Thread::cooked_allocated_bytes() {
  85   jlong allocated_bytes = OrderAccess::load_acquire(&_allocated_bytes);
  86   if (UseTLAB) {
  87     size_t used_bytes = tlab().used_bytes();
  88     if ((ssize_t)used_bytes > 0) {
  89       // More-or-less valid tlab. The load_acquire above should ensure
  90       // that the result of the add is <= the instantaneous value.
  91       return allocated_bytes + used_bytes;
  92     }
  93   }
  94   return allocated_bytes;
  95 }
  96 
  97 inline void JavaThread::set_ext_suspended() {
  98   set_suspend_flag (_ext_suspended);
  99 }
 100 inline void JavaThread::clear_ext_suspended() {
 101   clear_suspend_flag(_ext_suspended);
 102 }
 103 
 104 inline void JavaThread::set_external_suspend() {
 105   set_suspend_flag(_external_suspend);
 106 }
 107 inline void JavaThread::clear_external_suspend() {
 108   clear_suspend_flag(_external_suspend);
 109 }
 110 
 111 inline void JavaThread::set_deopt_suspend() {
 112   set_suspend_flag(_deopt_suspend);
 113 }
 114 inline void JavaThread::clear_deopt_suspend() {
 115   clear_suspend_flag(_deopt_suspend);
 116 }
 117 
 118 inline void JavaThread::set_pending_async_exception(oop e) {
 119   _pending_async_exception = e;
 120   _special_runtime_exit_condition = _async_exception;
 121   set_has_async_exception();
 122 }
 123 
 124 #ifdef PPC64
 125 inline JavaThreadState JavaThread::thread_state() const    {
 126   return (JavaThreadState) OrderAccess::load_acquire((volatile jint*)&_thread_state);
 127 }
 128 
 129 inline void JavaThread::set_thread_state(JavaThreadState s) {
 130   OrderAccess::release_store((volatile jint*)&_thread_state, (jint)s);
 131 }
 132 #endif
 133 
 134 inline void JavaThread::set_done_attaching_via_jni() {
 135   _jni_attach_state = _attached_via_jni;
 136   OrderAccess::fence();
 137 }
 138 
 139 #endif // SHARE_VM_RUNTIME_THREAD_INLINE_HPP