duke@435: /* duke@435: * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // A PeriodicTask has the sole purpose of executing its task duke@435: // function with regular intervals. duke@435: // Usage: duke@435: // PeriodicTask pf(10); duke@435: // pf.enroll(); duke@435: // ... duke@435: // pf.disenroll(); duke@435: duke@435: class PeriodicTask: public CHeapObj { duke@435: public: duke@435: // Useful constants. duke@435: // The interval constants are used to ensure the declared interval duke@435: // is appropriate; it must be between min_interval and max_interval, duke@435: // and have a granularity of interval_gran (all in millis). duke@435: enum { max_tasks = 10, // Max number of periodic tasks in system duke@435: interval_gran = 10, duke@435: min_interval = 10, duke@435: max_interval = 10000 }; duke@435: duke@435: static int num_tasks() { return _num_tasks; } duke@435: duke@435: private: duke@435: size_t _counter; duke@435: const size_t _interval; duke@435: duke@435: static int _num_tasks; duke@435: static PeriodicTask* _tasks[PeriodicTask::max_tasks]; duke@435: static void real_time_tick(size_t delay_time); duke@435: duke@435: #ifndef PRODUCT duke@435: static elapsedTimer _timer; // measures time between ticks duke@435: static int _ticks; // total number of ticks duke@435: static int _intervalHistogram[max_interval]; // to check spacing of timer interrupts duke@435: public: duke@435: static void print_intervals(); duke@435: #endif duke@435: // Only the WatcherThread can cause us to execute PeriodicTasks duke@435: friend class WatcherThread; duke@435: public: duke@435: PeriodicTask(size_t interval_time); // interval is in milliseconds of elapsed time duke@435: ~PeriodicTask(); duke@435: duke@435: // Tells whether is enrolled duke@435: bool is_enrolled() const; duke@435: duke@435: // Make the task active duke@435: // NOTE: this may only be called before the WatcherThread has been started duke@435: void enroll(); duke@435: duke@435: // Make the task deactive duke@435: // NOTE: this may only be called either while the WatcherThread is duke@435: // inactive or by a task from within its task() method. One-shot or duke@435: // several-shot tasks may be implemented this way. duke@435: void disenroll(); duke@435: duke@435: void execute_if_pending(size_t delay_time) { duke@435: _counter += delay_time; duke@435: if (_counter >= _interval) { duke@435: _counter = 0; duke@435: task(); duke@435: } duke@435: } duke@435: duke@435: // Returns how long (time in milliseconds) before the next time we should duke@435: // execute this task. duke@435: size_t time_to_next_interval() const { duke@435: assert(_interval > _counter, "task counter greater than interval?"); duke@435: return _interval - _counter; duke@435: } duke@435: duke@435: // Calculate when the next periodic task will fire. duke@435: // Called by the WatcherThread's run method. duke@435: // This assumes that periodic tasks aren't entering the system duke@435: // dynamically, except for during startup. duke@435: static size_t time_to_wait() { duke@435: if (_num_tasks == 0) { duke@435: // Don't wait any more; shut down the thread since we don't duke@435: // currently support dynamic enrollment. duke@435: return 0; duke@435: } duke@435: duke@435: size_t delay = _tasks[0]->time_to_next_interval(); duke@435: for (int index = 1; index < _num_tasks; index++) { duke@435: delay = MIN2(delay, _tasks[index]->time_to_next_interval()); duke@435: } duke@435: return delay; duke@435: } duke@435: duke@435: // The task to perform at each period duke@435: virtual void task() = 0; duke@435: };