src/os/linux/vm/os_linux.cpp

Print this page


   1 /*
   2  * Copyright (c) 1999, 2013, 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  *


3775 
3776   for (int j = 0; j < i; ++j) {
3777     if (base[j] != NULL) {
3778       unmap_memory(base[j], size[j]);
3779     }
3780   }
3781 
3782   if (i < max_tries) {
3783     _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
3784     return requested_addr;
3785   } else {
3786     _highest_vm_reserved_address = old_highest;
3787     return NULL;
3788   }
3789 }
3790 
3791 size_t os::read(int fd, void *buf, unsigned int nBytes) {
3792   return ::read(fd, buf, nBytes);
3793 }
3794 
3795 // TODO-FIXME: reconcile Solaris' os::sleep with the linux variation.
3796 // Solaris uses poll(), linux uses park().
3797 // Poll() is likely a better choice, assuming that Thread.interrupt()
3798 // generates a SIGUSRx signal. Note that SIGUSR1 can interfere with
3799 // SIGSEGV, see 4355769.
3800 
3801 int os::sleep(Thread* thread, jlong millis, bool interruptible) {
3802   assert(thread == Thread::current(),  "thread consistency check");
3803 
3804   ParkEvent * const slp = thread->_SleepEvent ;
3805   slp->reset() ;
3806   OrderAccess::fence() ;
3807 
3808   if (interruptible) {
3809     jlong prevtime = javaTimeNanos();
3810 
3811     for (;;) {
3812       if (os::is_interrupted(thread, true)) {
3813         return OS_INTRPT;
3814       }
3815 
3816       jlong newtime = javaTimeNanos();
3817 
3818       if (newtime - prevtime < 0) {
3819         // time moving backwards, should only happen if no monotonic clock
3820         // not a guarantee() because JVM should not abort on kernel/glibc bugs
3821         assert(!Linux::supports_monotonic_clock(), "time moving backwards");
3822       } else {
3823         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
3824       }
3825 
3826       if(millis <= 0) {
3827         return OS_OK;
3828       }
3829 
3830       prevtime = newtime;
3831 
3832       {
3833         assert(thread->is_Java_thread(), "sanity check");
3834         JavaThread *jt = (JavaThread *) thread;
3835         ThreadBlockInVM tbivm(jt);
3836         OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
3837 
3838         jt->set_suspend_equivalent();
3839         // cleared by handle_special_suspend_equivalent_condition() or
3840         // java_suspend_self() via check_and_wait_while_suspended()
3841 
3842         slp->park(millis);
3843 
3844         // were we externally suspended while we were waiting?
3845         jt->check_and_wait_while_suspended();
3846       }
3847     }
3848   } else {
3849     OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
3850     jlong prevtime = javaTimeNanos();
3851 
3852     for (;;) {
3853       // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
3854       // the 1st iteration ...
3855       jlong newtime = javaTimeNanos();
3856 
3857       if (newtime - prevtime < 0) {
3858         // time moving backwards, should only happen if no monotonic clock
3859         // not a guarantee() because JVM should not abort on kernel/glibc bugs
3860         assert(!Linux::supports_monotonic_clock(), "time moving backwards");
3861       } else {
3862         millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;
3863       }
3864 
3865       if(millis <= 0) break ;
3866 
3867       prevtime = newtime;
3868       slp->park(millis);
3869     }
3870     return OS_OK ;
3871   }
3872 }
3873 
3874 int os::naked_sleep() {
3875   // %% make the sleep time an integer flag. for now use 1 millisec.
3876   return os::sleep(Thread::current(), 1, false);
3877 }
3878 
3879 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3880 void os::infinite_sleep() {
3881   while (true) {    // sleep forever ...
3882     ::sleep(100);   // ... 100 seconds at a time
3883   }
3884 }
3885 
3886 // Used to convert frequent JVM_Yield() to nops
3887 bool os::dont_yield() {
3888   return DontYieldALot;
3889 }
3890 
3891 void os::yield() {
3892   sched_yield();
3893 }
3894 
3895 os::YieldResult os::NakedYield() { sched_yield(); return os::YIELD_UNKNOWN ;}
3896 
3897 void os::yield_all(int attempts) {
3898   // Yields to all threads, including threads with lower priorities


4182     // failed to switch to WAKEUP_REQUEST
4183     ShouldNotReachHere();
4184     return;
4185   }
4186 
4187   while (true) {
4188     if (sr_notify(osthread) == 0) {
4189       if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
4190         if (osthread->sr.is_running()) {
4191           return;
4192         }
4193       }
4194     } else {
4195       ShouldNotReachHere();
4196     }
4197   }
4198 
4199   guarantee(osthread->sr.is_running(), "Must be running!");
4200 }
4201 
4202 ////////////////////////////////////////////////////////////////////////////////
4203 // interrupt support
4204 
4205 void os::interrupt(Thread* thread) {
4206   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
4207     "possibility of dangling Thread pointer");
4208 
4209   OSThread* osthread = thread->osthread();
4210 
4211   if (!osthread->interrupted()) {
4212     osthread->set_interrupted(true);
4213     // More than one thread can get here with the same value of osthread,
4214     // resulting in multiple notifications.  We do, however, want the store
4215     // to interrupted() to be visible to other threads before we execute unpark().
4216     OrderAccess::fence();
4217     ParkEvent * const slp = thread->_SleepEvent ;
4218     if (slp != NULL) slp->unpark() ;
4219   }
4220 
4221   // For JSR166. Unpark even if interrupt status already was set
4222   if (thread->is_Java_thread())
4223     ((JavaThread*)thread)->parker()->unpark();
4224 
4225   ParkEvent * ev = thread->_ParkEvent ;
4226   if (ev != NULL) ev->unpark() ;
4227 
4228 }
4229 
4230 bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
4231   assert(Thread::current() == thread || Threads_lock->owned_by_self(),
4232     "possibility of dangling Thread pointer");
4233 
4234   OSThread* osthread = thread->osthread();
4235 
4236   bool interrupted = osthread->interrupted();
4237 
4238   if (interrupted && clear_interrupted) {
4239     osthread->set_interrupted(false);
4240     // consider thread->_SleepEvent->reset() ... optional optimization
4241   }
4242 
4243   return interrupted;
4244 }
4245 
4246 ///////////////////////////////////////////////////////////////////////////////////
4247 // signal handling (except suspend/resume)
4248 
4249 // This routine may be used by user applications as a "hook" to catch signals.
4250 // The user-defined signal handler must pass unrecognized signals to this
4251 // routine, and if it returns true (non-zero), then the signal handler must
4252 // return immediately.  If the flag "abort_if_unrecognized" is true, then this
4253 // routine will never retun false (zero), but instead will execute a VM panic
4254 // routine kill the process.
4255 //
4256 // If this routine returns false, it is OK to call it again.  This allows
4257 // the user-defined signal handler to perform checks either before or after
4258 // the VM performs its own checks.  Naturally, the user code would be making
4259 // a serious error if it tried to handle an exception (such as a null check
4260 // or breakpoint) that the VM was generating for its own correct operation.
4261 //
4262 // This routine may recognize any of the following kinds of signals:
4263 //    SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.
4264 // It should be consulted by handlers for any of those signals.
4265 //


   1 /*
   2  * Copyright (c) 1999, 2014, 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  *


3775 
3776   for (int j = 0; j < i; ++j) {
3777     if (base[j] != NULL) {
3778       unmap_memory(base[j], size[j]);
3779     }
3780   }
3781 
3782   if (i < max_tries) {
3783     _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
3784     return requested_addr;
3785   } else {
3786     _highest_vm_reserved_address = old_highest;
3787     return NULL;
3788   }
3789 }
3790 
3791 size_t os::read(int fd, void *buf, unsigned int nBytes) {
3792   return ::read(fd, buf, nBytes);
3793 }
3794 




















































































3795 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
3796 void os::infinite_sleep() {
3797   while (true) {    // sleep forever ...
3798     ::sleep(100);   // ... 100 seconds at a time
3799   }
3800 }
3801 
3802 // Used to convert frequent JVM_Yield() to nops
3803 bool os::dont_yield() {
3804   return DontYieldALot;
3805 }
3806 
3807 void os::yield() {
3808   sched_yield();
3809 }
3810 
3811 os::YieldResult os::NakedYield() { sched_yield(); return os::YIELD_UNKNOWN ;}
3812 
3813 void os::yield_all(int attempts) {
3814   // Yields to all threads, including threads with lower priorities


4098     // failed to switch to WAKEUP_REQUEST
4099     ShouldNotReachHere();
4100     return;
4101   }
4102 
4103   while (true) {
4104     if (sr_notify(osthread) == 0) {
4105       if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {
4106         if (osthread->sr.is_running()) {
4107           return;
4108         }
4109       }
4110     } else {
4111       ShouldNotReachHere();
4112     }
4113   }
4114 
4115   guarantee(osthread->sr.is_running(), "Must be running!");
4116 }
4117 












































4118 ///////////////////////////////////////////////////////////////////////////////////
4119 // signal handling (except suspend/resume)
4120 
4121 // This routine may be used by user applications as a "hook" to catch signals.
4122 // The user-defined signal handler must pass unrecognized signals to this
4123 // routine, and if it returns true (non-zero), then the signal handler must
4124 // return immediately.  If the flag "abort_if_unrecognized" is true, then this
4125 // routine will never retun false (zero), but instead will execute a VM panic
4126 // routine kill the process.
4127 //
4128 // If this routine returns false, it is OK to call it again.  This allows
4129 // the user-defined signal handler to perform checks either before or after
4130 // the VM performs its own checks.  Naturally, the user code would be making
4131 // a serious error if it tried to handle an exception (such as a null check
4132 // or breakpoint) that the VM was generating for its own correct operation.
4133 //
4134 // This routine may recognize any of the following kinds of signals:
4135 //    SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.
4136 // It should be consulted by handlers for any of those signals.
4137 //