1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/os/solaris/vm/osThread_solaris.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,224 @@ 1.4 +/* 1.5 + * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +// do not include precompiled header file 1.29 +# include "incls/_osThread_solaris.cpp.incl" 1.30 +# include <signal.h> 1.31 + 1.32 + // *************************************************************** 1.33 + // Platform dependent initialization and cleanup 1.34 + // *************************************************************** 1.35 + 1.36 +void OSThread::pd_initialize() { 1.37 + _thread_id = 0; 1.38 + sigemptyset(&_caller_sigmask); 1.39 + 1.40 + _current_callback = NULL; 1.41 + _current_callback_lock = VM_Version::supports_compare_and_exchange() ? NULL 1.42 + : new Mutex(Mutex::suspend_resume, "Callback_lock", true); 1.43 + 1.44 + _saved_interrupt_thread_state = _thread_new; 1.45 + _vm_created_thread = false; 1.46 +} 1.47 + 1.48 +void OSThread::pd_destroy() { 1.49 +} 1.50 + 1.51 +// Synchronous interrupt support 1.52 +// 1.53 +// _current_callback == NULL no pending callback 1.54 +// == 1 callback_in_progress 1.55 +// == other value pointer to the pending callback 1.56 +// 1.57 + 1.58 +// CAS on v8 is implemented by using a global atomic_memory_operation_lock, 1.59 +// which is shared by other atomic functions. It is OK for normal uses, but 1.60 +// dangerous if used after some thread is suspended or if used in signal 1.61 +// handlers. Instead here we use a special per-thread lock to synchronize 1.62 +// updating _current_callback if we are running on v8. Note in general trying 1.63 +// to grab locks after a thread is suspended is not safe, but it is safe for 1.64 +// updating _current_callback, because synchronous interrupt callbacks are 1.65 +// currently only used in: 1.66 +// 1. GetThreadPC_Callback - used by WatcherThread to profile VM thread 1.67 +// There is no overlap between the callbacks, which means we won't try to 1.68 +// grab a thread's sync lock after the thread has been suspended while holding 1.69 +// the same lock. 1.70 + 1.71 +// used after a thread is suspended 1.72 +static intptr_t compare_and_exchange_current_callback ( 1.73 + intptr_t callback, intptr_t *addr, intptr_t compare_value, Mutex *sync) { 1.74 + if (VM_Version::supports_compare_and_exchange()) { 1.75 + return Atomic::cmpxchg_ptr(callback, addr, compare_value); 1.76 + } else { 1.77 + MutexLockerEx(sync, Mutex::_no_safepoint_check_flag); 1.78 + if (*addr == compare_value) { 1.79 + *addr = callback; 1.80 + return compare_value; 1.81 + } else { 1.82 + return callback; 1.83 + } 1.84 + } 1.85 +} 1.86 + 1.87 +// used in signal handler 1.88 +static intptr_t exchange_current_callback(intptr_t callback, intptr_t *addr, Mutex *sync) { 1.89 + if (VM_Version::supports_compare_and_exchange()) { 1.90 + return Atomic::xchg_ptr(callback, addr); 1.91 + } else { 1.92 + MutexLockerEx(sync, Mutex::_no_safepoint_check_flag); 1.93 + intptr_t cb = *addr; 1.94 + *addr = callback; 1.95 + return cb; 1.96 + } 1.97 +} 1.98 + 1.99 +// one interrupt at a time. spin if _current_callback != NULL 1.100 +int OSThread::set_interrupt_callback(Sync_Interrupt_Callback * cb) { 1.101 + int count = 0; 1.102 + while (compare_and_exchange_current_callback( 1.103 + (intptr_t)cb, (intptr_t *)&_current_callback, (intptr_t)NULL, _current_callback_lock) != NULL) { 1.104 + while (_current_callback != NULL) { 1.105 + count++; 1.106 +#ifdef ASSERT 1.107 + if ((WarnOnStalledSpinLock > 0) && 1.108 + (count % WarnOnStalledSpinLock == 0)) { 1.109 + warning("_current_callback seems to be stalled: %p", _current_callback); 1.110 + } 1.111 +#endif 1.112 + os::yield_all(count); 1.113 + } 1.114 + } 1.115 + return 0; 1.116 +} 1.117 + 1.118 +// reset _current_callback, spin if _current_callback is callback_in_progress 1.119 +void OSThread::remove_interrupt_callback(Sync_Interrupt_Callback * cb) { 1.120 + int count = 0; 1.121 + while (compare_and_exchange_current_callback( 1.122 + (intptr_t)NULL, (intptr_t *)&_current_callback, (intptr_t)cb, _current_callback_lock) != (intptr_t)cb) { 1.123 +#ifdef ASSERT 1.124 + intptr_t p = (intptr_t)_current_callback; 1.125 + assert(p == (intptr_t)callback_in_progress || 1.126 + p == (intptr_t)cb, "wrong _current_callback value"); 1.127 +#endif 1.128 + while (_current_callback != cb) { 1.129 + count++; 1.130 +#ifdef ASSERT 1.131 + if ((WarnOnStalledSpinLock > 0) && 1.132 + (count % WarnOnStalledSpinLock == 0)) { 1.133 + warning("_current_callback seems to be stalled: %p", _current_callback); 1.134 + } 1.135 +#endif 1.136 + os::yield_all(count); 1.137 + } 1.138 + } 1.139 +} 1.140 + 1.141 +void OSThread::do_interrupt_callbacks_at_interrupt(InterruptArguments *args) { 1.142 + Sync_Interrupt_Callback * cb; 1.143 + cb = (Sync_Interrupt_Callback *)exchange_current_callback( 1.144 + (intptr_t)callback_in_progress, (intptr_t *)&_current_callback, _current_callback_lock); 1.145 + 1.146 + if (cb == NULL) { 1.147 + // signal is delivered too late (thread is masking interrupt signal??). 1.148 + // there is nothing we need to do because requesting thread has given up. 1.149 + } else if ((intptr_t)cb == (intptr_t)callback_in_progress) { 1.150 + fatal("invalid _current_callback state"); 1.151 + } else { 1.152 + assert(cb->target()->osthread() == this, "wrong target"); 1.153 + cb->execute(args); 1.154 + cb->leave_callback(); // notify the requester 1.155 + } 1.156 + 1.157 + // restore original _current_callback value 1.158 + intptr_t p; 1.159 + p = exchange_current_callback((intptr_t)cb, (intptr_t *)&_current_callback, _current_callback_lock); 1.160 + assert(p == (intptr_t)callback_in_progress, "just checking"); 1.161 +} 1.162 + 1.163 +// Called by the requesting thread to send a signal to target thread and 1.164 +// execute "this" callback from the signal handler. 1.165 +int OSThread::Sync_Interrupt_Callback::interrupt(Thread * target, int timeout) { 1.166 + // Let signals to the vm_thread go even if the Threads_lock is not acquired 1.167 + assert(Threads_lock->owned_by_self() || (target == VMThread::vm_thread()), 1.168 + "must have threads lock to call this"); 1.169 + 1.170 + OSThread * osthread = target->osthread(); 1.171 + 1.172 + // may block if target thread already has a pending callback 1.173 + osthread->set_interrupt_callback(this); 1.174 + 1.175 + _target = target; 1.176 + 1.177 + int rslt = thr_kill(osthread->thread_id(), os::Solaris::SIGasync()); 1.178 + assert(rslt == 0, "thr_kill != 0"); 1.179 + 1.180 + bool status = false; 1.181 + jlong t1 = os::javaTimeMillis(); 1.182 + { // don't use safepoint check because we might be the watcher thread. 1.183 + MutexLockerEx ml(_sync, Mutex::_no_safepoint_check_flag); 1.184 + while (!is_done()) { 1.185 + status = _sync->wait(Mutex::_no_safepoint_check_flag, timeout); 1.186 + 1.187 + // status == true if timed out 1.188 + if (status) break; 1.189 + 1.190 + // update timeout 1.191 + jlong t2 = os::javaTimeMillis(); 1.192 + timeout -= t2 - t1; 1.193 + t1 = t2; 1.194 + } 1.195 + } 1.196 + 1.197 + // reset current_callback 1.198 + osthread->remove_interrupt_callback(this); 1.199 + 1.200 + return status; 1.201 +} 1.202 + 1.203 +void OSThread::Sync_Interrupt_Callback::leave_callback() { 1.204 + if (!_sync->owned_by_self()) { 1.205 + // notify requesting thread 1.206 + MutexLockerEx ml(_sync, Mutex::_no_safepoint_check_flag); 1.207 + _is_done = true; 1.208 + _sync->notify_all(); 1.209 + } else { 1.210 + // Current thread is interrupted while it is holding the _sync lock, trying 1.211 + // to grab it again will deadlock. The requester will timeout anyway, 1.212 + // so just return. 1.213 + _is_done = true; 1.214 + } 1.215 +} 1.216 + 1.217 +// copied from synchronizer.cpp 1.218 + 1.219 +void OSThread::handle_spinlock_contention(int tries) { 1.220 + if (NoYieldsInMicrolock) return; 1.221 + 1.222 + if (tries > 10) { 1.223 + os::yield_all(tries); // Yield to threads of any priority 1.224 + } else if (tries > 5) { 1.225 + os::yield(); // Yield to threads of same or higher priority 1.226 + } 1.227 +}