src/share/vm/runtime/task.hpp

changeset 435
a61af66fc99e
child 496
5a76ab815e34
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/runtime/task.hpp	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,125 @@
     1.4 +/*
     1.5 + * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.24 + * have any questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +// A PeriodicTask has the sole purpose of executing its task
    1.29 +// function with regular intervals.
    1.30 +// Usage:
    1.31 +//   PeriodicTask pf(10);
    1.32 +//   pf.enroll();
    1.33 +//   ...
    1.34 +//   pf.disenroll();
    1.35 +
    1.36 +class PeriodicTask: public CHeapObj {
    1.37 + public:
    1.38 +  // Useful constants.
    1.39 +  // The interval constants are used to ensure the declared interval
    1.40 +  // is appropriate;  it must be between min_interval and max_interval,
    1.41 +  // and have a granularity of interval_gran (all in millis).
    1.42 +  enum { max_tasks     = 10,       // Max number of periodic tasks in system
    1.43 +         interval_gran = 10,
    1.44 +         min_interval  = 10,
    1.45 +         max_interval  = 10000 };
    1.46 +
    1.47 +  static int num_tasks()   { return _num_tasks; }
    1.48 +
    1.49 + private:
    1.50 +  size_t _counter;
    1.51 +  const size_t _interval;
    1.52 +
    1.53 +  static int _num_tasks;
    1.54 +  static PeriodicTask* _tasks[PeriodicTask::max_tasks];
    1.55 +  static void real_time_tick(size_t delay_time);
    1.56 +
    1.57 +#ifndef PRODUCT
    1.58 +  static elapsedTimer _timer;                      // measures time between ticks
    1.59 +  static int _ticks;                               // total number of ticks
    1.60 +  static int _intervalHistogram[max_interval];     // to check spacing of timer interrupts
    1.61 + public:
    1.62 +  static void print_intervals();
    1.63 +#endif
    1.64 +  // Only the WatcherThread can cause us to execute PeriodicTasks
    1.65 +  friend class WatcherThread;
    1.66 + public:
    1.67 +  PeriodicTask(size_t interval_time); // interval is in milliseconds of elapsed time
    1.68 +  ~PeriodicTask();
    1.69 +
    1.70 +  // Tells whether is enrolled
    1.71 +  bool is_enrolled() const;
    1.72 +
    1.73 +  // Make the task active
    1.74 +  // NOTE: this may only be called before the WatcherThread has been started
    1.75 +  void enroll();
    1.76 +
    1.77 +  // Make the task deactive
    1.78 +  // NOTE: this may only be called either while the WatcherThread is
    1.79 +  // inactive or by a task from within its task() method. One-shot or
    1.80 +  // several-shot tasks may be implemented this way.
    1.81 +  void disenroll();
    1.82 +
    1.83 +  void execute_if_pending(size_t delay_time) {
    1.84 +    _counter += delay_time;
    1.85 +    if (_counter >= _interval) {
    1.86 +      _counter = 0;
    1.87 +      task();
    1.88 +    }
    1.89 +  }
    1.90 +
    1.91 +  // Returns how long (time in milliseconds) before the next time we should
    1.92 +  // execute this task.
    1.93 +  size_t time_to_next_interval() const {
    1.94 +    assert(_interval > _counter,  "task counter greater than interval?");
    1.95 +    return _interval - _counter;
    1.96 +  }
    1.97 +
    1.98 +  // Calculate when the next periodic task will fire.
    1.99 +  // Called by the WatcherThread's run method.
   1.100 +  // This assumes that periodic tasks aren't entering the system
   1.101 +  // dynamically, except for during startup.
   1.102 +  static size_t time_to_wait() {
   1.103 +    if (_num_tasks == 0) {
   1.104 +      // Don't wait any more; shut down the thread since we don't
   1.105 +      // currently support dynamic enrollment.
   1.106 +      return 0;
   1.107 +    }
   1.108 +
   1.109 +    size_t delay = _tasks[0]->time_to_next_interval();
   1.110 +    for (int index = 1; index < _num_tasks; index++) {
   1.111 +      delay = MIN2(delay, _tasks[index]->time_to_next_interval());
   1.112 +    }
   1.113 +    return delay;
   1.114 +  }
   1.115 +
   1.116 +  // The task to perform at each period
   1.117 +  virtual void task() = 0;
   1.118 +};
   1.119 +
   1.120 +class TimeMillisUpdateTask : public PeriodicTask {
   1.121 + private:
   1.122 +  static TimeMillisUpdateTask* _task;
   1.123 + public:
   1.124 +  TimeMillisUpdateTask(int interval) : PeriodicTask(interval) {}
   1.125 +  void task();
   1.126 +  static void engage();
   1.127 +  static void disengage();
   1.128 +};

mercurial