315 316 // setup_interruptible saves the thread state before going into an 317 // interruptible system call. 318 // The saved state is used to restore the thread to 319 // its former state whether or not an interrupt is received. 320 // Used by classloader os::read 321 // os::restartable_read calls skip this layer and stay in _thread_in_native 322 323 void os::Solaris::setup_interruptible(JavaThread* thread) { 324 325 JavaThreadState thread_state = thread->thread_state(); 326 327 assert(thread_state != _thread_blocked, "Coming from the wrong thread"); 328 assert(thread_state != _thread_in_native, "Native threads skip setup_interruptible"); 329 OSThread* osthread = thread->osthread(); 330 osthread->set_saved_interrupt_thread_state(thread_state); 331 thread->frame_anchor()->make_walkable(thread); 332 ThreadStateTransition::transition(thread, thread_state, _thread_blocked); 333 } 334 335 // Version of setup_interruptible() for threads that are already in 336 // _thread_blocked. Used by os_sleep(). 337 void os::Solaris::setup_interruptible_already_blocked(JavaThread* thread) { 338 thread->frame_anchor()->make_walkable(thread); 339 } 340 341 JavaThread* os::Solaris::setup_interruptible() { 342 JavaThread* thread = (JavaThread*)ThreadLocalStorage::thread(); 343 setup_interruptible(thread); 344 return thread; 345 } 346 347 void os::Solaris::try_enable_extended_io() { 348 typedef int (*enable_extended_FILE_stdio_t)(int, int); 349 350 if (!UseExtendedFileIO) { 351 return; 352 } 353 354 enable_extended_FILE_stdio_t enabler = 355 (enable_extended_FILE_stdio_t) dlsym(RTLD_DEFAULT, 356 "enable_extended_FILE_stdio"); 357 if (enabler) { 358 enabler(-1, -1); 359 } 360 } 3406 3407 bool os::release_memory_special(char* base, size_t bytes) { 3408 fatal("os::release_memory_special should not be called on Solaris."); 3409 return false; 3410 } 3411 3412 size_t os::large_page_size() { 3413 return _large_page_size; 3414 } 3415 3416 // MPSS allows application to commit large page memory on demand; with ISM 3417 // the entire memory region must be allocated as shared memory. 3418 bool os::can_commit_large_page_memory() { 3419 return true; 3420 } 3421 3422 bool os::can_execute_large_page_memory() { 3423 return true; 3424 } 3425 3426 static int os_sleep(jlong millis, bool interruptible) { 3427 const jlong limit = INT_MAX; 3428 jlong prevtime; 3429 int res; 3430 3431 while (millis > limit) { 3432 if ((res = os_sleep(limit, interruptible)) != OS_OK) 3433 return res; 3434 millis -= limit; 3435 } 3436 3437 // Restart interrupted polls with new parameters until the proper delay 3438 // has been completed. 3439 3440 prevtime = getTimeMillis(); 3441 3442 while (millis > 0) { 3443 jlong newtime; 3444 3445 if (!interruptible) { 3446 // Following assert fails for os::yield_all: 3447 // assert(!thread->is_Java_thread(), "must not be java thread"); 3448 res = poll(NULL, 0, millis); 3449 } else { 3450 JavaThread *jt = JavaThread::current(); 3451 3452 INTERRUPTIBLE_NORESTART_VM_ALWAYS(poll(NULL, 0, millis), res, jt, 3453 os::Solaris::clear_interrupted); 3454 } 3455 3456 // INTERRUPTIBLE_NORESTART_VM_ALWAYS returns res == OS_INTRPT for 3457 // thread.Interrupt. 3458 3459 // See c/r 6751923. Poll can return 0 before time 3460 // has elapsed if time is set via clock_settime (as NTP does). 3461 // res == 0 if poll timed out (see man poll RETURN VALUES) 3462 // using the logic below checks that we really did 3463 // sleep at least "millis" if not we'll sleep again. 3464 if( ( res == 0 ) || ((res == OS_ERR) && (errno == EINTR))) { 3465 newtime = getTimeMillis(); 3466 assert(newtime >= prevtime, "time moving backwards"); 3467 /* Doing prevtime and newtime in microseconds doesn't help precision, 3468 and trying to round up to avoid lost milliseconds can result in a 3469 too-short delay. */ 3470 millis -= newtime - prevtime; 3471 if(millis <= 0) 3472 return OS_OK; 3473 prevtime = newtime; 3474 } else 3475 return res; 3476 } 3477 3478 return OS_OK; 3479 } 3480 3481 // Read calls from inside the vm need to perform state transitions 3482 size_t os::read(int fd, void *buf, unsigned int nBytes) { 3483 INTERRUPTIBLE_RETURN_INT_VM(::read(fd, buf, nBytes), os::Solaris::clear_interrupted); 3484 } 3485 3486 size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) { 3487 INTERRUPTIBLE_RETURN_INT(::read(fd, buf, nBytes), os::Solaris::clear_interrupted); 3488 } 3489 3490 int os::sleep(Thread* thread, jlong millis, bool interruptible) { 3491 assert(thread == Thread::current(), "thread consistency check"); 3492 3493 // TODO-FIXME: this should be removed. 3494 // On Solaris machines (especially 2.5.1) we found that sometimes the VM gets into a live lock 3495 // situation with a JavaThread being starved out of a lwp. The kernel doesn't seem to generate 3496 // a SIGWAITING signal which would enable the threads library to create a new lwp for the starving 3497 // thread. We suspect that because the Watcher thread keeps waking up at periodic intervals the kernel 3498 // is fooled into believing that the system is making progress. In the code below we block the 3499 // the watcher thread while safepoint is in progress so that it would not appear as though the 3500 // system is making progress. 3501 if (!Solaris::T2_libthread() && 3502 thread->is_Watcher_thread() && SafepointSynchronize::is_synchronizing() && !Arguments::has_profile()) { 3503 // We now try to acquire the threads lock. Since this lock is held by the VM thread during 3504 // the entire safepoint, the watcher thread will line up here during the safepoint. 3505 Threads_lock->lock_without_safepoint_check(); 3506 Threads_lock->unlock(); 3507 } 3508 3509 if (thread->is_Java_thread()) { 3510 // This is a JavaThread so we honor the _thread_blocked protocol 3511 // even for sleeps of 0 milliseconds. This was originally done 3512 // as a workaround for bug 4338139. However, now we also do it 3513 // to honor the suspend-equivalent protocol. 3514 3515 JavaThread *jt = (JavaThread *) thread; 3516 ThreadBlockInVM tbivm(jt); 3517 3518 jt->set_suspend_equivalent(); 3519 // cleared by handle_special_suspend_equivalent_condition() or 3520 // java_suspend_self() via check_and_wait_while_suspended() 3521 3522 int ret_code; 3523 if (millis <= 0) { 3524 thr_yield(); 3525 ret_code = 0; 3526 } else { 3527 // The original sleep() implementation did not create an 3528 // OSThreadWaitState helper for sleeps of 0 milliseconds. 3529 // I'm preserving that decision for now. 3530 OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */); 3531 3532 ret_code = os_sleep(millis, interruptible); 3533 } 3534 3535 // were we externally suspended while we were waiting? 3536 jt->check_and_wait_while_suspended(); 3537 3538 return ret_code; 3539 } 3540 3541 // non-JavaThread from this point on: 3542 3543 if (millis <= 0) { 3544 thr_yield(); 3545 return 0; 3546 } 3547 3548 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); 3549 3550 return os_sleep(millis, interruptible); 3551 } 3552 3553 void os::naked_short_sleep(jlong ms) { 3554 assert(ms < 1000, "Un-interruptable sleep, short time use only"); 3555 3556 // usleep is deprecated and removed from POSIX, in favour of nanosleep, but 3557 // Solaris requires -lrt for this. 3558 usleep((ms * 1000)); 3559 3560 return; 3561 } 3562 3563 // Sleep forever; naked call to OS-specific sleep; use with CAUTION 3564 void os::infinite_sleep() { 3565 while (true) { // sleep forever ... 3566 ::sleep(100); // ... 100 seconds at a time 3567 } 3568 } 3569 3570 // Used to convert frequent JVM_Yield() to nops 3571 bool os::dont_yield() { 3572 if (DontYieldALot) { 4175 } 4176 4177 } else if (state == os::SuspendResume::SR_RUNNING) { 4178 // request was cancelled, continue 4179 } else { 4180 ShouldNotReachHere(); 4181 } 4182 4183 resume_clear_context(osthread); 4184 } else if (current == os::SuspendResume::SR_RUNNING) { 4185 // request was cancelled, continue 4186 } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) { 4187 // ignore 4188 } else { 4189 // ignore 4190 } 4191 4192 errno = old_errno; 4193 } 4194 4195 4196 void os::interrupt(Thread* thread) { 4197 assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer"); 4198 4199 OSThread* osthread = thread->osthread(); 4200 4201 int isInterrupted = osthread->interrupted(); 4202 if (!isInterrupted) { 4203 osthread->set_interrupted(true); 4204 OrderAccess::fence(); 4205 // os::sleep() is implemented with either poll (NULL,0,timeout) or 4206 // by parking on _SleepEvent. If the former, thr_kill will unwedge 4207 // the sleeper by SIGINTR, otherwise the unpark() will wake the sleeper. 4208 ParkEvent * const slp = thread->_SleepEvent ; 4209 if (slp != NULL) slp->unpark() ; 4210 } 4211 4212 // For JSR166: unpark after setting status but before thr_kill -dl 4213 if (thread->is_Java_thread()) { 4214 ((JavaThread*)thread)->parker()->unpark(); 4215 } 4216 4217 // Handle interruptible wait() ... 4218 ParkEvent * const ev = thread->_ParkEvent ; 4219 if (ev != NULL) ev->unpark() ; 4220 4221 // When events are used everywhere for os::sleep, then this thr_kill 4222 // will only be needed if UseVMInterruptibleIO is true. 4223 4224 if (!isInterrupted) { 4225 int status = thr_kill(osthread->thread_id(), os::Solaris::SIGinterrupt()); 4226 assert_status(status == 0, status, "thr_kill"); 4227 4228 // Bump thread interruption counter 4229 RuntimeService::record_thread_interrupt_signaled_count(); 4230 } 4231 } 4232 4233 4234 bool os::is_interrupted(Thread* thread, bool clear_interrupted) { 4235 assert(Thread::current() == thread || Threads_lock->owned_by_self(), "possibility of dangling Thread pointer"); 4236 4237 OSThread* osthread = thread->osthread(); 4238 4239 bool res = osthread->interrupted(); 4240 4241 // NOTE that since there is no "lock" around these two operations, 4242 // there is the possibility that the interrupted flag will be 4243 // "false" but that the interrupt event will be set. This is 4244 // intentional. The effect of this is that Object.wait() will appear 4245 // to have a spurious wakeup, which is not harmful, and the 4246 // possibility is so rare that it is not worth the added complexity 4247 // to add yet another lock. It has also been recommended not to put 4248 // the interrupted flag into the os::Solaris::Event structure, 4249 // because it hides the issue. 4250 if (res && clear_interrupted) { 4251 osthread->set_interrupted(false); 4252 } 4253 return res; 4254 } 4255 4256 4257 void os::print_statistics() { 4258 } 4259 4260 int os::message_box(const char* title, const char* message) { 4261 int i; 4262 fdStream err(defaultStream::error_fd()); 4263 for (i = 0; i < 78; i++) err.print_raw("="); 4264 err.cr(); 4265 err.print_raw_cr(title); 4266 for (i = 0; i < 78; i++) err.print_raw("-"); 4267 err.cr(); 4268 err.print_raw_cr(message); 4269 for (i = 0; i < 78; i++) err.print_raw("="); 4270 err.cr(); 4271 4272 char buf[16]; 4273 // Prevent process from exiting upon "read error" without consuming all CPU 4274 while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); } 4275 4276 return buf[0] == 'y' || buf[0] == 'Y'; | 315 316 // setup_interruptible saves the thread state before going into an 317 // interruptible system call. 318 // The saved state is used to restore the thread to 319 // its former state whether or not an interrupt is received. 320 // Used by classloader os::read 321 // os::restartable_read calls skip this layer and stay in _thread_in_native 322 323 void os::Solaris::setup_interruptible(JavaThread* thread) { 324 325 JavaThreadState thread_state = thread->thread_state(); 326 327 assert(thread_state != _thread_blocked, "Coming from the wrong thread"); 328 assert(thread_state != _thread_in_native, "Native threads skip setup_interruptible"); 329 OSThread* osthread = thread->osthread(); 330 osthread->set_saved_interrupt_thread_state(thread_state); 331 thread->frame_anchor()->make_walkable(thread); 332 ThreadStateTransition::transition(thread, thread_state, _thread_blocked); 333 } 334 335 JavaThread* os::Solaris::setup_interruptible() { 336 JavaThread* thread = (JavaThread*)ThreadLocalStorage::thread(); 337 setup_interruptible(thread); 338 return thread; 339 } 340 341 void os::Solaris::try_enable_extended_io() { 342 typedef int (*enable_extended_FILE_stdio_t)(int, int); 343 344 if (!UseExtendedFileIO) { 345 return; 346 } 347 348 enable_extended_FILE_stdio_t enabler = 349 (enable_extended_FILE_stdio_t) dlsym(RTLD_DEFAULT, 350 "enable_extended_FILE_stdio"); 351 if (enabler) { 352 enabler(-1, -1); 353 } 354 } 3400 3401 bool os::release_memory_special(char* base, size_t bytes) { 3402 fatal("os::release_memory_special should not be called on Solaris."); 3403 return false; 3404 } 3405 3406 size_t os::large_page_size() { 3407 return _large_page_size; 3408 } 3409 3410 // MPSS allows application to commit large page memory on demand; with ISM 3411 // the entire memory region must be allocated as shared memory. 3412 bool os::can_commit_large_page_memory() { 3413 return true; 3414 } 3415 3416 bool os::can_execute_large_page_memory() { 3417 return true; 3418 } 3419 3420 // Read calls from inside the vm need to perform state transitions 3421 size_t os::read(int fd, void *buf, unsigned int nBytes) { 3422 INTERRUPTIBLE_RETURN_INT_VM(::read(fd, buf, nBytes), os::Solaris::clear_interrupted); 3423 } 3424 3425 size_t os::restartable_read(int fd, void *buf, unsigned int nBytes) { 3426 INTERRUPTIBLE_RETURN_INT(::read(fd, buf, nBytes), os::Solaris::clear_interrupted); 3427 } 3428 3429 void os::naked_short_sleep(jlong ms) { 3430 assert(ms < 1000, "Un-interruptable sleep, short time use only"); 3431 3432 // usleep is deprecated and removed from POSIX, in favour of nanosleep, but 3433 // Solaris requires -lrt for this. 3434 usleep((ms * 1000)); 3435 3436 return; 3437 } 3438 3439 // Sleep forever; naked call to OS-specific sleep; use with CAUTION 3440 void os::infinite_sleep() { 3441 while (true) { // sleep forever ... 3442 ::sleep(100); // ... 100 seconds at a time 3443 } 3444 } 3445 3446 // Used to convert frequent JVM_Yield() to nops 3447 bool os::dont_yield() { 3448 if (DontYieldALot) { 4051 } 4052 4053 } else if (state == os::SuspendResume::SR_RUNNING) { 4054 // request was cancelled, continue 4055 } else { 4056 ShouldNotReachHere(); 4057 } 4058 4059 resume_clear_context(osthread); 4060 } else if (current == os::SuspendResume::SR_RUNNING) { 4061 // request was cancelled, continue 4062 } else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) { 4063 // ignore 4064 } else { 4065 // ignore 4066 } 4067 4068 errno = old_errno; 4069 } 4070 4071 void os::print_statistics() { 4072 } 4073 4074 int os::message_box(const char* title, const char* message) { 4075 int i; 4076 fdStream err(defaultStream::error_fd()); 4077 for (i = 0; i < 78; i++) err.print_raw("="); 4078 err.cr(); 4079 err.print_raw_cr(title); 4080 for (i = 0; i < 78; i++) err.print_raw("-"); 4081 err.cr(); 4082 err.print_raw_cr(message); 4083 for (i = 0; i < 78; i++) err.print_raw("="); 4084 err.cr(); 4085 4086 char buf[16]; 4087 // Prevent process from exiting upon "read error" without consuming all CPU 4088 while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); } 4089 4090 return buf[0] == 'y' || buf[0] == 'Y'; |