annotate src/share/vm/runtime/task.hpp @ 196:d1605aabd0a1

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