src/share/vm/runtime/task.hpp

Wed, 09 Apr 2008 15:10:22 -0700

author
rasbold
date
Wed, 09 Apr 2008 15:10:22 -0700
changeset 544
9f4457a14b58
parent 496
5a76ab815e34
child 631
d1605aabd0a1
permissions
-rw-r--r--

Merge

duke@435 1 /*
duke@435 2 * Copyright 1997-2007 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 // A PeriodicTask has the sole purpose of executing its task
duke@435 26 // function with regular intervals.
duke@435 27 // Usage:
duke@435 28 // PeriodicTask pf(10);
duke@435 29 // pf.enroll();
duke@435 30 // ...
duke@435 31 // pf.disenroll();
duke@435 32
duke@435 33 class PeriodicTask: public CHeapObj {
duke@435 34 public:
duke@435 35 // Useful constants.
duke@435 36 // The interval constants are used to ensure the declared interval
duke@435 37 // is appropriate; it must be between min_interval and max_interval,
duke@435 38 // and have a granularity of interval_gran (all in millis).
duke@435 39 enum { max_tasks = 10, // Max number of periodic tasks in system
duke@435 40 interval_gran = 10,
duke@435 41 min_interval = 10,
duke@435 42 max_interval = 10000 };
duke@435 43
duke@435 44 static int num_tasks() { return _num_tasks; }
duke@435 45
duke@435 46 private:
duke@435 47 size_t _counter;
duke@435 48 const size_t _interval;
duke@435 49
duke@435 50 static int _num_tasks;
duke@435 51 static PeriodicTask* _tasks[PeriodicTask::max_tasks];
duke@435 52 static void real_time_tick(size_t delay_time);
duke@435 53
duke@435 54 #ifndef PRODUCT
duke@435 55 static elapsedTimer _timer; // measures time between ticks
duke@435 56 static int _ticks; // total number of ticks
duke@435 57 static int _intervalHistogram[max_interval]; // to check spacing of timer interrupts
duke@435 58 public:
duke@435 59 static void print_intervals();
duke@435 60 #endif
duke@435 61 // Only the WatcherThread can cause us to execute PeriodicTasks
duke@435 62 friend class WatcherThread;
duke@435 63 public:
duke@435 64 PeriodicTask(size_t interval_time); // interval is in milliseconds of elapsed time
duke@435 65 ~PeriodicTask();
duke@435 66
duke@435 67 // Tells whether is enrolled
duke@435 68 bool is_enrolled() const;
duke@435 69
duke@435 70 // Make the task active
duke@435 71 // NOTE: this may only be called before the WatcherThread has been started
duke@435 72 void enroll();
duke@435 73
duke@435 74 // Make the task deactive
duke@435 75 // NOTE: this may only be called either while the WatcherThread is
duke@435 76 // inactive or by a task from within its task() method. One-shot or
duke@435 77 // several-shot tasks may be implemented this way.
duke@435 78 void disenroll();
duke@435 79
duke@435 80 void execute_if_pending(size_t delay_time) {
duke@435 81 _counter += delay_time;
duke@435 82 if (_counter >= _interval) {
duke@435 83 _counter = 0;
duke@435 84 task();
duke@435 85 }
duke@435 86 }
duke@435 87
duke@435 88 // Returns how long (time in milliseconds) before the next time we should
duke@435 89 // execute this task.
duke@435 90 size_t time_to_next_interval() const {
duke@435 91 assert(_interval > _counter, "task counter greater than interval?");
duke@435 92 return _interval - _counter;
duke@435 93 }
duke@435 94
duke@435 95 // Calculate when the next periodic task will fire.
duke@435 96 // Called by the WatcherThread's run method.
duke@435 97 // This assumes that periodic tasks aren't entering the system
duke@435 98 // dynamically, except for during startup.
duke@435 99 static size_t time_to_wait() {
duke@435 100 if (_num_tasks == 0) {
duke@435 101 // Don't wait any more; shut down the thread since we don't
duke@435 102 // currently support dynamic enrollment.
duke@435 103 return 0;
duke@435 104 }
duke@435 105
duke@435 106 size_t delay = _tasks[0]->time_to_next_interval();
duke@435 107 for (int index = 1; index < _num_tasks; index++) {
duke@435 108 delay = MIN2(delay, _tasks[index]->time_to_next_interval());
duke@435 109 }
duke@435 110 return delay;
duke@435 111 }
duke@435 112
duke@435 113 // The task to perform at each period
duke@435 114 virtual void task() = 0;
duke@435 115 };

mercurial