task.hpp 3.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29
#ifndef SHARE_VM_RUNTIME_TASK_HPP
#define SHARE_VM_RUNTIME_TASK_HPP

#include "utilities/top.hpp"

D
duke 已提交
30 31 32 33 34 35 36 37
// A PeriodicTask has the sole purpose of executing its task
// function with regular intervals.
// Usage:
//   PeriodicTask pf(10);
//   pf.enroll();
//   ...
//   pf.disenroll();

Z
zgu 已提交
38
class PeriodicTask: public CHeapObj<mtInternal> {
D
duke 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51
 public:
  // Useful constants.
  // The interval constants are used to ensure the declared interval
  // is appropriate;  it must be between min_interval and max_interval,
  // and have a granularity of interval_gran (all in millis).
  enum { max_tasks     = 10,       // Max number of periodic tasks in system
         interval_gran = 10,
         min_interval  = 10,
         max_interval  = 10000 };

  static int num_tasks()   { return _num_tasks; }

 private:
52 53
  int _counter;
  const int _interval;
D
duke 已提交
54 55 56

  static int _num_tasks;
  static PeriodicTask* _tasks[PeriodicTask::max_tasks];
57
  static void real_time_tick(int delay_time);
D
duke 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

#ifndef PRODUCT
  static elapsedTimer _timer;                      // measures time between ticks
  static int _ticks;                               // total number of ticks
  static int _intervalHistogram[max_interval];     // to check spacing of timer interrupts
 public:
  static void print_intervals();
#endif
  // Only the WatcherThread can cause us to execute PeriodicTasks
  friend class WatcherThread;
 public:
  PeriodicTask(size_t interval_time); // interval is in milliseconds of elapsed time
  ~PeriodicTask();

  // Make the task active
73 74
  // For dynamic enrollment at the time T, the task will execute somewhere
  // between T and T + interval_time.
D
duke 已提交
75 76 77 78 79
  void enroll();

  // Make the task deactive
  void disenroll();

80 81 82 83 84
  void execute_if_pending(int delay_time) {
    // make sure we don't overflow
    jlong tmp = (jlong) _counter + (jlong) delay_time;

    if (tmp >= (jlong) _interval) {
D
duke 已提交
85 86
      _counter = 0;
      task();
87 88
    } else {
      _counter += delay_time;
D
duke 已提交
89 90 91 92 93
    }
  }

  // Returns how long (time in milliseconds) before the next time we should
  // execute this task.
94
  int time_to_next_interval() const {
D
duke 已提交
95 96 97 98 99 100
    assert(_interval > _counter,  "task counter greater than interval?");
    return _interval - _counter;
  }

  // Calculate when the next periodic task will fire.
  // Called by the WatcherThread's run method.
101
  static int time_to_wait();
D
duke 已提交
102 103 104 105

  // The task to perform at each period
  virtual void task() = 0;
};
106 107

#endif // SHARE_VM_RUNTIME_TASK_HPP