1 /* 2 * Copyright (c) 1998, 2010, 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 // no precompiled headers 26 #include "runtime/atomic.hpp" 27 #include "runtime/handles.inline.hpp" 28 #include "runtime/mutexLocker.hpp" 29 #include "runtime/os.hpp" 30 #include "runtime/osThread.hpp" 31 #include "runtime/safepoint.hpp" 32 #include "runtime/vmThread.hpp" 33 #ifdef TARGET_ARCH_x86 34 # include "assembler_x86.inline.hpp" 35 #endif 36 #ifdef TARGET_ARCH_sparc 37 # include "assembler_sparc.inline.hpp" 38 #endif 39 40 // do not include precompiled header file 41 # include <signal.h> 42 43 // *************************************************************** 44 // Platform dependent initialization and cleanup 45 // *************************************************************** 46 47 void OSThread::pd_initialize() { 48 _thread_id = 0; 49 sigemptyset(&_caller_sigmask); 50 51 _current_callback = NULL; 52 _current_callback_lock = VM_Version::supports_compare_and_exchange() ? NULL 53 : new Mutex(Mutex::suspend_resume, "Callback_lock", true); 54 55 _saved_interrupt_thread_state = _thread_new; 56 _vm_created_thread = false; 57 } 58 59 void OSThread::pd_destroy() { 60 } 61 62 // Synchronous interrupt support 63 // 64 // _current_callback == NULL no pending callback 65 // == 1 callback_in_progress 66 // == other value pointer to the pending callback 67 // 68 69 // CAS on v8 is implemented by using a global atomic_memory_operation_lock, 70 // which is shared by other atomic functions. It is OK for normal uses, but 71 // dangerous if used after some thread is suspended or if used in signal 72 // handlers. Instead here we use a special per-thread lock to synchronize 73 // updating _current_callback if we are running on v8. Note in general trying 74 // to grab locks after a thread is suspended is not safe, but it is safe for 75 // updating _current_callback, because synchronous interrupt callbacks are 76 // currently only used in: 77 // 1. GetThreadPC_Callback - used by WatcherThread to profile VM thread 78 // There is no overlap between the callbacks, which means we won't try to 79 // grab a thread's sync lock after the thread has been suspended while holding 80 // the same lock. 81 82 // used after a thread is suspended 83 static intptr_t compare_and_exchange_current_callback ( 84 intptr_t callback, intptr_t *addr, intptr_t compare_value, Mutex *sync) { 85 if (VM_Version::supports_compare_and_exchange()) { 86 return Atomic::cmpxchg_ptr(callback, addr, compare_value); 87 } else { 88 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag); 89 if (*addr == compare_value) { 90 *addr = callback; 91 return compare_value; 92 } else { 93 return callback; 94 } 95 } 96 } 97 98 // used in signal handler 99 static intptr_t exchange_current_callback(intptr_t callback, intptr_t *addr, Mutex *sync) { 100 if (VM_Version::supports_compare_and_exchange()) { 101 return Atomic::xchg_ptr(callback, addr); 102 } else { 103 MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag); 104 intptr_t cb = *addr; 105 *addr = callback; 106 return cb; 107 } 108 } 109 110 // one interrupt at a time. spin if _current_callback != NULL 111 int OSThread::set_interrupt_callback(Sync_Interrupt_Callback * cb) { 112 int count = 0; 113 while (compare_and_exchange_current_callback( 114 (intptr_t)cb, (intptr_t *)&_current_callback, (intptr_t)NULL, _current_callback_lock) != NULL) { 115 while (_current_callback != NULL) { 116 count++; 117 #ifdef ASSERT 118 if ((WarnOnStalledSpinLock > 0) && 119 (count % WarnOnStalledSpinLock == 0)) { 120 warning("_current_callback seems to be stalled: %p", _current_callback); 121 } 122 #endif 123 os::yield_all(count); 124 } 125 } 126 return 0; 127 } 128 129 // reset _current_callback, spin if _current_callback is callback_in_progress 130 void OSThread::remove_interrupt_callback(Sync_Interrupt_Callback * cb) { 131 int count = 0; 132 while (compare_and_exchange_current_callback( 133 (intptr_t)NULL, (intptr_t *)&_current_callback, (intptr_t)cb, _current_callback_lock) != (intptr_t)cb) { 134 #ifdef ASSERT 135 intptr_t p = (intptr_t)_current_callback; 136 assert(p == (intptr_t)callback_in_progress || 137 p == (intptr_t)cb, "wrong _current_callback value"); 138 #endif 139 while (_current_callback != cb) { 140 count++; 141 #ifdef ASSERT 142 if ((WarnOnStalledSpinLock > 0) && 143 (count % WarnOnStalledSpinLock == 0)) { 144 warning("_current_callback seems to be stalled: %p", _current_callback); 145 } 146 #endif 147 os::yield_all(count); 148 } 149 } 150 } 151 152 void OSThread::do_interrupt_callbacks_at_interrupt(InterruptArguments *args) { 153 Sync_Interrupt_Callback * cb; 154 cb = (Sync_Interrupt_Callback *)exchange_current_callback( 155 (intptr_t)callback_in_progress, (intptr_t *)&_current_callback, _current_callback_lock); 156 157 if (cb == NULL) { 158 // signal is delivered too late (thread is masking interrupt signal??). 159 // there is nothing we need to do because requesting thread has given up. 160 } else if ((intptr_t)cb == (intptr_t)callback_in_progress) { 161 fatal("invalid _current_callback state"); 162 } else { 163 assert(cb->target()->osthread() == this, "wrong target"); 164 cb->execute(args); 165 cb->leave_callback(); // notify the requester 166 } 167 168 // restore original _current_callback value 169 intptr_t p; 170 p = exchange_current_callback((intptr_t)cb, (intptr_t *)&_current_callback, _current_callback_lock); 171 assert(p == (intptr_t)callback_in_progress, "just checking"); 172 } 173 174 // Called by the requesting thread to send a signal to target thread and 175 // execute "this" callback from the signal handler. 176 int OSThread::Sync_Interrupt_Callback::interrupt(Thread * target, int timeout) { 177 // Let signals to the vm_thread go even if the Threads_lock is not acquired 178 assert(Threads_lock->owned_by_self() || (target == VMThread::vm_thread()), 179 "must have threads lock to call this"); 180 181 OSThread * osthread = target->osthread(); 182 183 // may block if target thread already has a pending callback 184 osthread->set_interrupt_callback(this); 185 186 _target = target; 187 188 int rslt = thr_kill(osthread->thread_id(), os::Solaris::SIGasync()); 189 assert(rslt == 0, "thr_kill != 0"); 190 191 bool status = false; 192 jlong t1 = os::javaTimeMillis(); 193 { // don't use safepoint check because we might be the watcher thread. 194 MutexLockerEx ml(_sync, Mutex::_no_safepoint_check_flag); 195 while (!is_done()) { 196 status = _sync->wait(Mutex::_no_safepoint_check_flag, timeout); 197 198 // status == true if timed out 199 if (status) break; 200 201 // update timeout 202 jlong t2 = os::javaTimeMillis(); 203 timeout -= t2 - t1; 204 t1 = t2; 205 } 206 } 207 208 // reset current_callback 209 osthread->remove_interrupt_callback(this); 210 211 return status; 212 } 213 214 void OSThread::Sync_Interrupt_Callback::leave_callback() { 215 if (!_sync->owned_by_self()) { 216 // notify requesting thread 217 MutexLockerEx ml(_sync, Mutex::_no_safepoint_check_flag); 218 _is_done = true; 219 _sync->notify_all(); 220 } else { 221 // Current thread is interrupted while it is holding the _sync lock, trying 222 // to grab it again will deadlock. The requester will timeout anyway, 223 // so just return. 224 _is_done = true; 225 } 226 } 227 228 // copied from synchronizer.cpp 229 230 void OSThread::handle_spinlock_contention(int tries) { 231 if (NoYieldsInMicrolock) return; 232 233 if (tries > 10) { 234 os::yield_all(tries); // Yield to threads of any priority 235 } else if (tries > 5) { 236 os::yield(); // Yield to threads of same or higher priority 237 } 238 }