1 /* |
1 /* |
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
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. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
992 } |
992 } |
993 #endif |
993 #endif |
994 |
994 |
995 |
995 |
996 jlong os::javaTimeNanos() { |
996 jlong os::javaTimeNanos() { |
997 if (Bsd::supports_monotonic_clock()) { |
997 if (os::supports_monotonic_clock()) { |
998 struct timespec tp; |
998 struct timespec tp; |
999 int status = Bsd::clock_gettime(CLOCK_MONOTONIC, &tp); |
999 int status = Bsd::clock_gettime(CLOCK_MONOTONIC, &tp); |
1000 assert(status == 0, "gettime error"); |
1000 assert(status == 0, "gettime error"); |
1001 jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec); |
1001 jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec); |
1002 return result; |
1002 return result; |
1008 return 1000 * usecs; |
1008 return 1000 * usecs; |
1009 } |
1009 } |
1010 } |
1010 } |
1011 |
1011 |
1012 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) { |
1012 void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) { |
1013 if (Bsd::supports_monotonic_clock()) { |
1013 if (os::supports_monotonic_clock()) { |
1014 info_ptr->max_value = ALL_64_BITS; |
1014 info_ptr->max_value = ALL_64_BITS; |
1015 |
1015 |
1016 // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past |
1016 // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past |
1017 info_ptr->may_skip_backward = false; // not subject to resetting or drifting |
1017 info_ptr->may_skip_backward = false; // not subject to resetting or drifting |
1018 info_ptr->may_skip_forward = false; // not subject to resetting or drifting |
1018 info_ptr->may_skip_forward = false; // not subject to resetting or drifting |
2557 |
2557 |
2558 size_t os::read(int fd, void *buf, unsigned int nBytes) { |
2558 size_t os::read(int fd, void *buf, unsigned int nBytes) { |
2559 RESTARTABLE_RETURN_INT(::read(fd, buf, nBytes)); |
2559 RESTARTABLE_RETURN_INT(::read(fd, buf, nBytes)); |
2560 } |
2560 } |
2561 |
2561 |
2562 // TODO-FIXME: reconcile Solaris' os::sleep with the bsd variation. |
|
2563 // Solaris uses poll(), bsd uses park(). |
|
2564 // Poll() is likely a better choice, assuming that Thread.interrupt() |
|
2565 // generates a SIGUSRx signal. Note that SIGUSR1 can interfere with |
|
2566 // SIGSEGV, see 4355769. |
|
2567 |
|
2568 int os::sleep(Thread* thread, jlong millis, bool interruptible) { |
|
2569 assert(thread == Thread::current(), "thread consistency check"); |
|
2570 |
|
2571 ParkEvent * const slp = thread->_SleepEvent ; |
|
2572 slp->reset() ; |
|
2573 OrderAccess::fence() ; |
|
2574 |
|
2575 if (interruptible) { |
|
2576 jlong prevtime = javaTimeNanos(); |
|
2577 |
|
2578 for (;;) { |
|
2579 if (os::is_interrupted(thread, true)) { |
|
2580 return OS_INTRPT; |
|
2581 } |
|
2582 |
|
2583 jlong newtime = javaTimeNanos(); |
|
2584 |
|
2585 if (newtime - prevtime < 0) { |
|
2586 // time moving backwards, should only happen if no monotonic clock |
|
2587 // not a guarantee() because JVM should not abort on kernel/glibc bugs |
|
2588 assert(!Bsd::supports_monotonic_clock(), "time moving backwards"); |
|
2589 } else { |
|
2590 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; |
|
2591 } |
|
2592 |
|
2593 if(millis <= 0) { |
|
2594 return OS_OK; |
|
2595 } |
|
2596 |
|
2597 prevtime = newtime; |
|
2598 |
|
2599 { |
|
2600 assert(thread->is_Java_thread(), "sanity check"); |
|
2601 JavaThread *jt = (JavaThread *) thread; |
|
2602 ThreadBlockInVM tbivm(jt); |
|
2603 OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */); |
|
2604 |
|
2605 jt->set_suspend_equivalent(); |
|
2606 // cleared by handle_special_suspend_equivalent_condition() or |
|
2607 // java_suspend_self() via check_and_wait_while_suspended() |
|
2608 |
|
2609 slp->park(millis); |
|
2610 |
|
2611 // were we externally suspended while we were waiting? |
|
2612 jt->check_and_wait_while_suspended(); |
|
2613 } |
|
2614 } |
|
2615 } else { |
|
2616 OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */); |
|
2617 jlong prevtime = javaTimeNanos(); |
|
2618 |
|
2619 for (;;) { |
|
2620 // It'd be nice to avoid the back-to-back javaTimeNanos() calls on |
|
2621 // the 1st iteration ... |
|
2622 jlong newtime = javaTimeNanos(); |
|
2623 |
|
2624 if (newtime - prevtime < 0) { |
|
2625 // time moving backwards, should only happen if no monotonic clock |
|
2626 // not a guarantee() because JVM should not abort on kernel/glibc bugs |
|
2627 assert(!Bsd::supports_monotonic_clock(), "time moving backwards"); |
|
2628 } else { |
|
2629 millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC; |
|
2630 } |
|
2631 |
|
2632 if(millis <= 0) break ; |
|
2633 |
|
2634 prevtime = newtime; |
|
2635 slp->park(millis); |
|
2636 } |
|
2637 return OS_OK ; |
|
2638 } |
|
2639 } |
|
2640 |
|
2641 void os::naked_short_sleep(jlong ms) { |
2562 void os::naked_short_sleep(jlong ms) { |
2642 struct timespec req; |
2563 struct timespec req; |
2643 |
2564 |
2644 assert(ms < 1000, "Un-interruptable sleep, short time use only"); |
2565 assert(ms < 1000, "Un-interruptable sleep, short time use only"); |
2645 req.tv_sec = 0; |
2566 req.tv_sec = 0; |
3029 ShouldNotReachHere(); |
2950 ShouldNotReachHere(); |
3030 } |
2951 } |
3031 } |
2952 } |
3032 |
2953 |
3033 guarantee(osthread->sr.is_running(), "Must be running!"); |
2954 guarantee(osthread->sr.is_running(), "Must be running!"); |
3034 } |
|
3035 |
|
3036 //////////////////////////////////////////////////////////////////////////////// |
|
3037 // interrupt support |
|
3038 |
|
3039 void os::interrupt(Thread* thread) { |
|
3040 assert(Thread::current() == thread || Threads_lock->owned_by_self(), |
|
3041 "possibility of dangling Thread pointer"); |
|
3042 |
|
3043 OSThread* osthread = thread->osthread(); |
|
3044 |
|
3045 if (!osthread->interrupted()) { |
|
3046 osthread->set_interrupted(true); |
|
3047 // More than one thread can get here with the same value of osthread, |
|
3048 // resulting in multiple notifications. We do, however, want the store |
|
3049 // to interrupted() to be visible to other threads before we execute unpark(). |
|
3050 OrderAccess::fence(); |
|
3051 ParkEvent * const slp = thread->_SleepEvent ; |
|
3052 if (slp != NULL) slp->unpark() ; |
|
3053 } |
|
3054 |
|
3055 // For JSR166. Unpark even if interrupt status already was set |
|
3056 if (thread->is_Java_thread()) |
|
3057 ((JavaThread*)thread)->parker()->unpark(); |
|
3058 |
|
3059 ParkEvent * ev = thread->_ParkEvent ; |
|
3060 if (ev != NULL) ev->unpark() ; |
|
3061 |
|
3062 } |
|
3063 |
|
3064 bool os::is_interrupted(Thread* thread, bool clear_interrupted) { |
|
3065 assert(Thread::current() == thread || Threads_lock->owned_by_self(), |
|
3066 "possibility of dangling Thread pointer"); |
|
3067 |
|
3068 OSThread* osthread = thread->osthread(); |
|
3069 |
|
3070 bool interrupted = osthread->interrupted(); |
|
3071 |
|
3072 if (interrupted && clear_interrupted) { |
|
3073 osthread->set_interrupted(false); |
|
3074 // consider thread->_SleepEvent->reset() ... optional optimization |
|
3075 } |
|
3076 |
|
3077 return interrupted; |
|
3078 } |
2955 } |
3079 |
2956 |
3080 /////////////////////////////////////////////////////////////////////////////////// |
2957 /////////////////////////////////////////////////////////////////////////////////// |
3081 // signal handling (except suspend/resume) |
2958 // signal handling (except suspend/resume) |
3082 |
2959 |