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