public class SuperClock extends Clock
Vona's utils: accelerateable, stoppable clock with extended sleep semantics.
Copyright (C) 2004 Marsette A. Vona, III
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program 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 for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Modifier and Type | Class and Description |
---|---|
protected static class |
SuperClock.SleepState
Encapsulates thread-specific sleep state.
|
Modifier and Type | Field and Description |
---|---|
protected float |
accel
The time acceleration factor.
|
protected boolean |
accumulateSleep
Whether to accumulate sleep, default
DEFAULT_ACCUMULATE_SLEEP . |
private static java.lang.String |
cvsid
CVS id.
|
static boolean |
DEFAULT_ACCUMULATE_SLEEP
Whether to accumulate sleep by default.
|
static boolean |
DEFAULT_SLEEP_TO_DEADLINE
Whether to sleep to deadline by default.
|
protected double |
lastWallTime
The last wall time.
|
static double |
MIN_SLEEP
Minimum accumulated sleep.
|
protected boolean |
paused
Whethere we've been
pause() d. |
protected double |
pausedAt
The wall time at which we were paused in seconds if
paused . |
protected java.util.Vector<PeriodicThread> |
periodicThreads
All current active
PeriodicThread s, lazily consed. |
static double |
SLEEP_GRANULARITY
Real-world sleep granularity, see
CompatTime.getSleepGranularity() . |
protected static java.lang.ThreadLocal |
sleepState
Holds thread-specific
SuperClock.SleepState . |
protected boolean |
sleepToDeadline
Whether we're sleeping to deadline, default
DEFAULT_SLEEP_TO_DEADLINE . |
protected double |
time
The current (accel) time.
|
Constructor and Description |
---|
SuperClock()
|
SuperClock(double accel)
Create a SuperClock with
DEFAULT_ACCUMULATE_SLEEP and DEFAULT_SLEEP_TO_DEADLINE . |
SuperClock(double accel,
boolean accumulateSleep,
boolean sleepToDeadline)
Create a SuperClock.
|
Modifier and Type | Method and Description |
---|---|
double |
getAccel()
Get the time acceleration factor.
|
double |
getCurrentTime()
Get the acceltime in accelseconds relative to an arbitrary epoch.
|
protected double |
getWallTime()
Get the wall-clock time in seconds relative to an arbitrary epoch.
|
boolean |
isAccumulateSleep()
Check whether we're accumulating sleep.
|
boolean |
isPaused()
Check if this clock is currently paused.
|
boolean |
isSleepToDeadline()
Check whether we're sleeping to deadline.
|
void |
pause()
Pause.
|
protected void |
periodicThreadEnding(PeriodicThread periodicThread)
Hook called just before a
PeriodicThread ends. |
protected void |
periodicThreadStarting(PeriodicThread periodicThread)
Hook called just after a
PeriodicThread starts. |
void |
setAccel(double accel)
Set the time acceleration factor.
|
void |
setAccumulateSleep(boolean accumulateSleep)
Set whether to accumulate sleep.
|
void |
setSleepToDeadline(boolean sleepToDeadline)
Set whether to sleep to deadline.
|
void |
sleep(double seconds)
Sleep the current thread for the requested number of accelseconds.
|
void |
unPause()
Un-
pause() . |
private static final java.lang.String cvsid
CVS id.
public static final double SLEEP_GRANULARITY
Real-world sleep granularity, see CompatTime.getSleepGranularity()
.
public static final double MIN_SLEEP
Minimum accumulated sleep.
public static final boolean DEFAULT_ACCUMULATE_SLEEP
Whether to accumulate sleep by default.
public static final boolean DEFAULT_SLEEP_TO_DEADLINE
Whether to sleep to deadline by default.
protected float accel
The time acceleration factor.
32 bits so we don't have to synchronize to read and write it.
protected double lastWallTime
The last wall time.
protected double time
The current (accel) time.
protected java.util.Vector<PeriodicThread> periodicThreads
All current active PeriodicThread
s, lazily consed.
protected boolean paused
Whethere we've been pause()
d.
protected double pausedAt
The wall time at which we were paused in seconds if paused
.
protected boolean accumulateSleep
Whether to accumulate sleep, default DEFAULT_ACCUMULATE_SLEEP
.
protected boolean sleepToDeadline
Whether we're sleeping to deadline, default DEFAULT_SLEEP_TO_DEADLINE
.
protected static java.lang.ThreadLocal sleepState
Holds thread-specific SuperClock.SleepState
.
public SuperClock(double accel, boolean accumulateSleep, boolean sleepToDeadline)
Create a SuperClock.
accel
- the time acceleration factoraccumulateSleep
- whether to accumulate sleep time (see sleep(double)
)sleepToDeadline
- whether to sleep to deadline only (see sleep(double)
)public SuperClock(double accel)
Create a SuperClock with DEFAULT_ACCUMULATE_SLEEP
and DEFAULT_SLEEP_TO_DEADLINE
.
accel
- the time acceleration factorpublic SuperClock()
Create a SuperClock with accel
1.0, DEFAULT_ACCUMULATE_SLEEP
and DEFAULT_SLEEP_TO_DEADLINE
.
public void setAccel(double accel)
Set the time acceleration factor.
A factor of 2.0 means time runs twice as fast as it would in reality.
accel
- the time acceleration factorpublic double getAccel()
Get the time acceleration factor.
public void pause()
Pause.
Has no effect if already paused.
Subsequent calls to getCurrentTime()
will appear as if time was
frozen when pause()
d. Calls to sleep(double)
will
block until unPause()
d, and any PeriodicThread
s will also
stop iterating until unPause()
d.
public void unPause()
Un-pause()
.
public boolean isPaused()
Check if this clock is currently paused.
public double getCurrentTime()
Get the acceltime in accelseconds relative to an arbitrary epoch.
getCurrentTime
in class Clock
protected double getWallTime()
Get the wall-clock time in seconds relative to an arbitrary epoch.
public void sleep(double seconds) throws java.lang.InterruptedException
Sleep the current thread for the requested number of accelseconds.
If we are pause()
d before the sleep then we won't start sleeping
until after we're unPause()
d, and we'll wait for that in
(real-world time) increments of SLEEP_GRANULARITY
seconds.
If sleep accumulation (see setAccumulateSleep(boolean)
) is turned on,
then an actual sleep will only occur after enough sleep accelseconds have
accumulated to require a real-world sleep of at least
MIN_SLEEP
seconds. Separate accumulators are maintained for
all calling threads.
If sleep to deadline (see setSleepToDeadline(boolean)
) is turned on
then the we'll try to sleep only until the actual deadline, where the
deadline is the acceltime at call (or since the beginning of the current
sleep accumulation for the calling thread, if accumulating) plus the accel
seconds
according to the current accel.
public void setAccumulateSleep(boolean accumulateSleep)
Set whether to accumulate sleep.
See sleep(double)
.
Has no effect if the current mode is as requested.
accumulateSleep
- whether to accumulate sleeppublic boolean isAccumulateSleep()
Check whether we're accumulating sleep.
See sleep(double)
.
public void setSleepToDeadline(boolean sleepToDeadline)
Set whether to sleep to deadline.
See sleep(double)
.
Has no effect if the current mode is as requested.
sleepToDeadline
- whether to sleep to deadlinepublic boolean isSleepToDeadline()
Check whether we're sleeping to deadline.
See sleep(double)
.
protected void periodicThreadStarting(PeriodicThread periodicThread)
Hook called just after a PeriodicThread
starts.
This impl adds periodicThread
to periodicThreads
.
periodicThreadStarting
in class Clock
periodicThread
- the periodic threadprotected void periodicThreadEnding(PeriodicThread periodicThread)
Hook called just before a PeriodicThread
ends.
This impl removes periodicThread
from periodicThreads
.
periodicThreadEnding
in class Clock
periodicThread
- the periodic thread