|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Object | +--scorej.OmarClock
A clock object that manages the simulation time for a ScoreJ simulation. OmarClock maintains the current simulation time and is in charge of advancing time. A static OmarClock object is a field of S.java, that is accessible to the whole simulation. This static OmarClock object is crated by Scenario.initialize, which also starts a corresponding Thread which executes OmarClock's run() method. A clock can also be started by calling the S functions S.createClock(), S.setClock(), and S.runClock().
OmarClock is an abstract class. There are two implemetation classes, SimulationClock and NonSimulationClock. Each implementation class has a different implementation of sleeping, signal processor control, and the main clock thread.
Synchronization done using Doug Lea's util.concurrent package, specifically, the Semaphore class.
All procedures that get executed as part of a ScoreJ scenario must be registered with the clock using the addThread method. The OmarClock keeps a hashtable of executing Threads and Process objects. A Process object holds that Thread's current state, and a Semaphore used to wake and suspend the Thread. A Thread can be in one of 7 states:
The OmarClock can be in one of 5 states:
SimulationClock,
| Nested Class Summary | |
protected class |
OmarClock.Process
Internal class that holds the status of a process in state, and a Semaphore to sync on |
protected class |
OmarClock.StatusObj
Holder class that stores the Status of the OmarClock and provides symchronized accessor functions. |
| Field Summary | |
protected EDU.oswego.cs.dl.util.concurrent.Semaphore |
_clockSync
|
protected EDU.oswego.cs.dl.util.concurrent.Mutex |
_isSleepingSync
|
protected EDU.oswego.cs.dl.util.concurrent.Semaphore |
_pausedSync
|
protected long |
_pauseTime
|
protected boolean |
_realTime
|
protected double |
_realTimeFactor
|
protected java.util.HashMap |
_resourceMap
|
protected SignalProcessor |
_sigProc
|
protected boolean |
_sleeping
|
protected EDU.oswego.cs.dl.util.concurrent.Mutex |
_stateChangedSync
|
protected OmarClock.StatusObj |
_status
|
protected java.util.Hashtable |
_statusTable
|
protected EDU.oswego.cs.dl.util.concurrent.Mutex |
_statusTableSync
|
protected java.lang.Thread |
_thread
|
protected double |
_time
|
protected long |
_zeroTime
|
static double |
DEAD
|
static int |
INITIALIZED
|
static int |
KILLED
|
static int |
PAUSED
|
static int |
RUNNING
|
static double |
SUSPENDED
|
static double |
TRANSITION
|
static int |
UNINITIALIZED
|
static double |
WAITING_FOR_FUTURE
|
static double |
WAITING_FOR_RESOURCE
|
static double |
WAITING_FOR_SIGNAL
|
static double |
WORKING
|
| Constructor Summary | |
OmarClock()
Create an OmarClock object. |
|
OmarClock(boolean rt,
double rtFactor)
Create an OmarClock object. |
|
| Method Summary | |
void |
addThread(java.lang.Thread t)
Add a new thread to the status Table. |
void |
cleanoutStatusTable()
Cleans out the status table. |
void |
clockThreadSleep(long msecs)
Clock thread sleep. |
static java.lang.String |
getClockStateString(double state)
Converts a double representing a clock thread's status into a String. |
java.lang.Double |
getLastSuspendedTime(java.lang.Thread t)
Returns the most recent time at which thread t was suspended. |
java.lang.Double |
getPreviousThreadState(java.lang.Thread t)
Return the prior state of the Thread as stored in the statusTable |
double |
getRealTimeFactor()
|
java.util.HashMap |
getResourceMap()
Get a copy of the resourceMap. |
int |
getStatus()
Returns the Status of the clock as an integer, derived from the status field. |
java.util.Hashtable |
getStatusTable()
Get a copy of the statusTable. |
java.lang.Thread |
getThread()
Returns the clock threads Thread object |
java.lang.Double |
getThreadState(java.lang.Thread t)
Return the status of the Thread as stored in the statusTable WORKING : Currently executing code WAITING_FOR_SIGNAL : Waiting for a withSignal to match WAITING_FOR_FUTURE : Waiting for a Future to complete WAITING_FOR_RESOURCE : Waiting for a resource to become free SLEEPING : Waiting until a certain simulation time SUSPENDED : Waiting for resource or method conflict to end. |
static java.lang.String |
getThreadStateString(double state)
Converts a double representing a clock thread's status into a String. |
java.lang.Double |
getThreadStateUnsafe(java.lang.Thread t)
Similar to getThreadState(Thread) except that it does not guarantee that the operation will execute atomically. |
double |
getTime()
Getter for the time field of the clock. |
void |
interruptClock()
interruptClock: If the clock thread is in a Thread.sleep() to advance the time, use this function to cause it to wake early. |
boolean |
isRealTime()
|
void |
killClock()
Kills the clock and waits for the clock Thread to die. |
boolean |
lockStatusTable()
Acquire the semaphore for the status Table |
void |
pauseClock()
Pause clock convience function. |
void |
pauseClock(boolean autoPauseP)
Pause the clock. |
void |
printStatusTable()
Print out the status table to System.out. |
void |
removeThread(java.lang.Thread t)
Remove a thread from the status Table. |
void |
resetClock()
Resets simulation time to 0.0, interrupts all executing Threads, and clears out the statusTable. |
void |
resume(java.lang.Thread t)
Resumes a previosuly suspended thread. |
abstract void |
run()
Main run method, called when the clock Thread is started by Scenario.initialize. |
void |
runClock()
Continue the clock from the paused state. |
boolean |
setAcquiredResource(java.lang.Thread t,
double state,
java.lang.String resource,
boolean isAcquired)
Tells OmarClock the name of the resource that thread t either acquired or released. |
void |
setRealTime(boolean rt,
double rtFactor)
Set the mode of a clock that has already been created. |
void |
setThreadState(java.lang.Thread t,
double state)
Sets the status of an executing Thread. |
void |
setThreadStateUnsafe(java.lang.Thread t,
double state)
Similar to setThreadState(Thread, double) except that it does not guarantee that the operation will execute atomically. |
protected boolean |
setTime(double time)
Advance the simulation time. |
boolean |
setWaitingForResource(java.lang.Thread t,
java.lang.String resource)
Tells OmarClock the name of the resource that thread t is waiting on. |
abstract void |
sleep(java.lang.Thread t,
double sleepTime)
Sleep method. |
void |
startClock()
Start the clock from the Initialized state. |
boolean |
suspend(java.lang.Thread t)
Suspends a thread. |
void |
unlockStatusTable()
Release the semaphore for the status table |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final int UNINITIALIZED
public static final int INITIALIZED
public static final int RUNNING
public static final int PAUSED
public static final int KILLED
public static final double WORKING
public static final double WAITING_FOR_SIGNAL
public static final double WAITING_FOR_FUTURE
public static final double SUSPENDED
public static final double TRANSITION
public static final double WAITING_FOR_RESOURCE
public static final double DEAD
protected double _time
protected java.util.Hashtable _statusTable
protected java.lang.Thread _thread
protected EDU.oswego.cs.dl.util.concurrent.Semaphore _clockSync
protected EDU.oswego.cs.dl.util.concurrent.Semaphore _pausedSync
protected EDU.oswego.cs.dl.util.concurrent.Mutex _statusTableSync
protected EDU.oswego.cs.dl.util.concurrent.Mutex _stateChangedSync
protected EDU.oswego.cs.dl.util.concurrent.Mutex _isSleepingSync
protected boolean _realTime
protected double _realTimeFactor
protected long _zeroTime
protected long _pauseTime
protected boolean _sleeping
protected SignalProcessor _sigProc
protected java.util.HashMap _resourceMap
protected OmarClock.StatusObj _status
| Constructor Detail |
public OmarClock()
public OmarClock(boolean rt,
double rtFactor)
rt - true = real-time mode, false = fast-time modertFactor - = one tick of simulation time = 1/rtFactor real-time seconds| Method Detail |
public void setRealTime(boolean rt,
double rtFactor)
rt - true = real-time mode, false = fast-time modertFactor - = one tick of simulation time = 1/rtFactor real-time secondspublic boolean isRealTime()
public double getRealTimeFactor()
public abstract void run()
run in interface java.lang.Runnable
public abstract void sleep(java.lang.Thread t,
double sleepTime)
t - The thread that wants to sleep.sleepTime - How long the thread wants to sleep forpublic void pauseClock(boolean autoPauseP)
public void pauseClock()
public void startClock()
public void runClock()
public void killClock()
public void setThreadState(java.lang.Thread t,
double state)
throws java.lang.InterruptedException
t - Thread object, if it is not in the statusTable, put a new
entry for it in.state - Status of the Thread [WORKING | WAITING_FOR_SIGNAL |
WAITING_FOR_FUTURE | WAITING_FOR_RESOURCE |
SUSPENDED | TRANSITION | java.lang.InterruptedException
public boolean setWaitingForResource(java.lang.Thread t,
java.lang.String resource)
t - The threadresource - The String resource name that the thread is waiting on
public boolean setAcquiredResource(java.lang.Thread t,
double state,
java.lang.String resource,
boolean isAcquired)
t - The threadresource - The String resource name that the thread acquired or
released.isAcquired - true if thread acquired resource, false if it
released it.
The method does not do anything if it returns false.
public void setThreadStateUnsafe(java.lang.Thread t,
double state)
t - Thread object, if it is not in the statusTable, put a new
entry for it in.state - Status of the Thread [WORKING | WAITING_FOR_SIGNAL |
WAITING_FOR_FUTURE | WAITING_FOR_RESOURCE |
SUSPENDED | TRANSITION | public boolean suspend(java.lang.Thread t)
t - thread to be suspended
public void resume(java.lang.Thread t)
t - thread to be resumed.public java.lang.Double getLastSuspendedTime(java.lang.Thread t)
t - thread object.
public boolean lockStatusTable()
public void unlockStatusTable()
public void addThread(java.lang.Thread t)
public void removeThread(java.lang.Thread t)
public void interruptClock()
public double getTime()
public void clockThreadSleep(long msecs)
throws java.lang.InterruptedException
java.lang.InterruptedExceptionprotected boolean setTime(double time)
time - Time to advance to
public int getStatus()
public java.util.Hashtable getStatusTable()
public java.util.HashMap getResourceMap()
public java.lang.Double getThreadState(java.lang.Thread t)
public java.lang.Double getThreadStateUnsafe(java.lang.Thread t)
public java.lang.Double getPreviousThreadState(java.lang.Thread t)
public java.lang.Thread getThread()
public static java.lang.String getThreadStateString(double state)
public static java.lang.String getClockStateString(double state)
public void cleanoutStatusTable()
public void printStatusTable()
public void resetClock()
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||