2007-12-01 00:00:00 +00:00
|
|
|
/*
|
2015-03-02 16:31:25 -08:00
|
|
|
* Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
|
2007-12-01 00:00:00 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2010-05-27 19:08:38 -07:00
|
|
|
* 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.
|
2007-12-01 00:00:00 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-11-23 13:22:55 -08:00
|
|
|
#ifndef SHARE_VM_RUNTIME_TASK_HPP
|
|
|
|
#define SHARE_VM_RUNTIME_TASK_HPP
|
|
|
|
|
2016-04-12 09:53:43 +02:00
|
|
|
#include "memory/allocation.hpp"
|
|
|
|
#include "runtime/timer.hpp"
|
2010-11-23 13:22:55 -08:00
|
|
|
|
2007-12-01 00:00:00 +00:00
|
|
|
// A PeriodicTask has the sole purpose of executing its task
|
|
|
|
// function with regular intervals.
|
|
|
|
// Usage:
|
|
|
|
// PeriodicTask pf(10);
|
|
|
|
// pf.enroll();
|
|
|
|
// ...
|
|
|
|
// pf.disenroll();
|
|
|
|
|
2012-06-28 17:03:16 -04:00
|
|
|
class PeriodicTask: public CHeapObj<mtInternal> {
|
2007-12-01 00:00:00 +00:00
|
|
|
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:
|
2012-10-04 14:55:57 +02:00
|
|
|
int _counter;
|
|
|
|
const int _interval;
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
static int _num_tasks;
|
|
|
|
static PeriodicTask* _tasks[PeriodicTask::max_tasks];
|
2015-03-02 16:31:25 -08:00
|
|
|
// Can only be called by the WatcherThread
|
2012-10-04 14:55:57 +02:00
|
|
|
static void real_time_tick(int delay_time);
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
#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
|
2012-10-04 14:55:57 +02:00
|
|
|
// For dynamic enrollment at the time T, the task will execute somewhere
|
|
|
|
// between T and T + interval_time.
|
2007-12-01 00:00:00 +00:00
|
|
|
void enroll();
|
|
|
|
|
|
|
|
// Make the task deactive
|
|
|
|
void disenroll();
|
|
|
|
|
2012-10-04 14:55:57 +02:00
|
|
|
void execute_if_pending(int delay_time) {
|
|
|
|
// make sure we don't overflow
|
|
|
|
jlong tmp = (jlong) _counter + (jlong) delay_time;
|
|
|
|
|
|
|
|
if (tmp >= (jlong) _interval) {
|
2007-12-01 00:00:00 +00:00
|
|
|
_counter = 0;
|
|
|
|
task();
|
2012-10-04 14:55:57 +02:00
|
|
|
} else {
|
|
|
|
_counter += delay_time;
|
2007-12-01 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns how long (time in milliseconds) before the next time we should
|
|
|
|
// execute this task.
|
2012-10-04 14:55:57 +02:00
|
|
|
int time_to_next_interval() const {
|
2007-12-01 00:00:00 +00:00
|
|
|
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.
|
2015-03-02 16:31:25 -08:00
|
|
|
// Requires the PeriodicTask_lock.
|
2012-10-04 14:55:57 +02:00
|
|
|
static int time_to_wait();
|
2007-12-01 00:00:00 +00:00
|
|
|
|
|
|
|
// The task to perform at each period
|
|
|
|
virtual void task() = 0;
|
|
|
|
};
|
2010-11-23 13:22:55 -08:00
|
|
|
|
|
|
|
#endif // SHARE_VM_RUNTIME_TASK_HPP
|