src/share/vm/runtime/task.hpp

Mon, 24 Oct 2011 07:53:17 -0700

author
twisti
date
Mon, 24 Oct 2011 07:53:17 -0700
changeset 3238
b20d64f83668
parent 2314
f95d63e2154a
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

7090904: JSR 292: JRuby junit test crashes in PSScavengeRootsClosure::do_oop
Reviewed-by: kvn, never, jrose

duke@435 1 /*
stefank@2314 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #ifndef SHARE_VM_RUNTIME_TASK_HPP
stefank@2314 26 #define SHARE_VM_RUNTIME_TASK_HPP
stefank@2314 27
stefank@2314 28 #include "utilities/top.hpp"
stefank@2314 29
duke@435 30 // A PeriodicTask has the sole purpose of executing its task
duke@435 31 // function with regular intervals.
duke@435 32 // Usage:
duke@435 33 // PeriodicTask pf(10);
duke@435 34 // pf.enroll();
duke@435 35 // ...
duke@435 36 // pf.disenroll();
duke@435 37
duke@435 38 class PeriodicTask: public CHeapObj {
duke@435 39 public:
duke@435 40 // Useful constants.
duke@435 41 // The interval constants are used to ensure the declared interval
duke@435 42 // is appropriate; it must be between min_interval and max_interval,
duke@435 43 // and have a granularity of interval_gran (all in millis).
duke@435 44 enum { max_tasks = 10, // Max number of periodic tasks in system
duke@435 45 interval_gran = 10,
duke@435 46 min_interval = 10,
duke@435 47 max_interval = 10000 };
duke@435 48
duke@435 49 static int num_tasks() { return _num_tasks; }
duke@435 50
duke@435 51 private:
duke@435 52 size_t _counter;
duke@435 53 const size_t _interval;
duke@435 54
duke@435 55 static int _num_tasks;
duke@435 56 static PeriodicTask* _tasks[PeriodicTask::max_tasks];
duke@435 57 static void real_time_tick(size_t delay_time);
duke@435 58
duke@435 59 #ifndef PRODUCT
duke@435 60 static elapsedTimer _timer; // measures time between ticks
duke@435 61 static int _ticks; // total number of ticks
duke@435 62 static int _intervalHistogram[max_interval]; // to check spacing of timer interrupts
duke@435 63 public:
duke@435 64 static void print_intervals();
duke@435 65 #endif
duke@435 66 // Only the WatcherThread can cause us to execute PeriodicTasks
duke@435 67 friend class WatcherThread;
duke@435 68 public:
duke@435 69 PeriodicTask(size_t interval_time); // interval is in milliseconds of elapsed time
duke@435 70 ~PeriodicTask();
duke@435 71
duke@435 72 // Tells whether is enrolled
duke@435 73 bool is_enrolled() const;
duke@435 74
duke@435 75 // Make the task active
duke@435 76 // NOTE: this may only be called before the WatcherThread has been started
duke@435 77 void enroll();
duke@435 78
duke@435 79 // Make the task deactive
duke@435 80 // NOTE: this may only be called either while the WatcherThread is
duke@435 81 // inactive or by a task from within its task() method. One-shot or
duke@435 82 // several-shot tasks may be implemented this way.
duke@435 83 void disenroll();
duke@435 84
duke@435 85 void execute_if_pending(size_t delay_time) {
duke@435 86 _counter += delay_time;
duke@435 87 if (_counter >= _interval) {
duke@435 88 _counter = 0;
duke@435 89 task();
duke@435 90 }
duke@435 91 }
duke@435 92
duke@435 93 // Returns how long (time in milliseconds) before the next time we should
duke@435 94 // execute this task.
duke@435 95 size_t time_to_next_interval() const {
duke@435 96 assert(_interval > _counter, "task counter greater than interval?");
duke@435 97 return _interval - _counter;
duke@435 98 }
duke@435 99
duke@435 100 // Calculate when the next periodic task will fire.
duke@435 101 // Called by the WatcherThread's run method.
duke@435 102 // This assumes that periodic tasks aren't entering the system
duke@435 103 // dynamically, except for during startup.
duke@435 104 static size_t time_to_wait() {
duke@435 105 if (_num_tasks == 0) {
duke@435 106 // Don't wait any more; shut down the thread since we don't
duke@435 107 // currently support dynamic enrollment.
duke@435 108 return 0;
duke@435 109 }
duke@435 110
duke@435 111 size_t delay = _tasks[0]->time_to_next_interval();
duke@435 112 for (int index = 1; index < _num_tasks; index++) {
duke@435 113 delay = MIN2(delay, _tasks[index]->time_to_next_interval());
duke@435 114 }
duke@435 115 return delay;
duke@435 116 }
duke@435 117
duke@435 118 // The task to perform at each period
duke@435 119 virtual void task() = 0;
duke@435 120 };
stefank@2314 121
stefank@2314 122 #endif // SHARE_VM_RUNTIME_TASK_HPP

mercurial