Documente Academic
Documente Profesional
Documente Cultură
Threads;
import java.util.Vector;
import java.util.Enumeration;
import osp.Utilities.*;
import osp.IFLModules.*;
import osp.Tasks.*;
import osp.EventEngine.*;
import osp.Hardware.*;
import osp.Devices.*;
import osp.Memory.*;
import osp.Resources.*;
/**
This class is responsible for actions related to threads, including
creating, killing, dispatching, resuming, and suspending threads.
@OSPProject Threads
*/
public class ThreadCB extends IflThreadCB
{
private static GenericList readyQueue;
/**
The thread constructor. Must call
super();
@OSPProject Threads
*/
public ThreadCB()
{
super();
}
/**
This method will be called once at the beginning of the
simulation. The student can set up static variables here.
@OSPProject Threads
*/
public static void init()
{
readyQueue = new GenericList();
}
/**
Sets up a new thread and adds it to the given task.
The method must set the ready status
and attempt to add thread to task. If the latter fails
because there are already too many threads in this task,
so does this method, otherwise, the thread is appended
to the ready queue and dispatch() is called.
@OSPProject Threads
*/
static public ThreadCB do_create(TaskCB task)
{
if (task == null) {
dispatch();
return null;
}
newThread.setTask(task);
readyQueue.append(newThread);
MyOut.print("osp.Threads.ThreadCB",
"Successfully added "+newThread+" to "+task);
dispatch();
return newThread;
}
/**
Kills the specified thread.
@OSPProject Threads
*/
public void do_kill()
{
MyOut.print(this, "Entering do_kill(" +this + ")");
switch (getStatus()) {
case ThreadReady:
// Delete thread from ready queue.
readyQueue.remove(this);
break;
case ThreadRunning:
// Remove (preempt) thread from CPU.
if(this == MMU.getPTBR().getTask().getCurrentThread()) {
MMU.getPTBR().getTask().setCurrentThread(null);
}
break;
default:
}
dispatch();
if (this.getTask().getThreadCount()==0) {
MyOut.print(this,
"After destroying " + this + ": " + this.getTask()
+ " has no threads left; destroying the task");
this.getTask().kill();
}
}
/** Suspends the thread that is currenly on the processor on the
specified event.
@OSPProject Threads
*/
public void do_suspend(Event event)
{
int oldStatus = this.getStatus();
MyOut.print(this, "Entering suspend(" + this + "," + event + ")");
readyQueue.remove(this);
event.addThread(this);
@OSPProject Threads
*/
public void do_resume()
{
if(getStatus() < ThreadWaiting) {
MyOut.print(this,
"Attempt to resume "
+ this + ", which wasn't waiting");
return;
}
dispatch();
}
/**
Selects a thread from the run queue and dispatches it.
@OSPProject Threads
*/
public static int do_dispatch()
{
ThreadCB threadToDispatch=null;
ThreadCB runningThread=null;
TaskCB runningTask=null;
try {
runningTask = MMU.getPTBR().getTask();
runningThread = runningTask.getCurrentThread();
} catch(NullPointerException e) {}
MMU.setPTBR(null);
runningThread.setStatus(ThreadReady);
readyQueue.append(runningThread);
}
MyOut.print("osp.Threads.ThreadCB",
"Dispatching " + threadToDispatch);
HTimer.set(150);
return SUCCESS;
}
/**
Called by OSP after printing an error message. The student can
insert code here to print various tables and data structures in
their state just after the error happened. The body can be
left empty, if this feature is not used.
@OSPProject Threads
*/
public static void atError()
{
// any code
}
@OSPProject Threads
*/
public static void atWarning()
{
// any code
}
}