tasks.c 3.83 KB
Newer Older
ram's avatar
ram committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* tasks.c */

#include "tasks.h"
#include "richards.h"

/*
// TaskControlBlock
*/

TaskControlBlock::TaskControlBlock(TaskControlBlock *l, Identity id, int prio,
                         Packet *initialWork, TaskState *initialState,
                         void *privateData)
{
    link = l; ident = id; priority = prio; input = initialWork;
    packetPending = initialState->IsPacketPending();
    taskWaiting = initialState->IsTaskWaiting();
    taskHolding = initialState->IsTaskHolding();
    handle = privateData;
}


TaskControlBlock *TaskControlBlock::AddPacket(Packet *p,
                                              TaskControlBlock *oldTask)
{
    if (input == NoWork) {
         input = p;
         packetPending = TRUE;
         if (priority > oldTask->priority) return this;
    }
    else AddToList(input, p);
    return oldTask;
}


TaskControlBlock *TaskControlBlock::RunTask()
{   Packet *msg;

    if (IsWaitingWithPacket()) {
         msg = input;
         input = input->Link();
         if (input == NoWork)
              Running();
         else PacketPending();
    }
    else msg = NoWork;
    return this->ActionFunc(msg, handle);
}


/*
// action functions
*/

TaskControlBlock *TaskControlBlock::ActionFunc(Packet *p, void *handle)
{
    p, handle, 0;
    printf("***error: virtual TaskControlBlock.ActionFunc called!\n");
    return NoTask;
}


TaskControlBlock *DeviceTCB::ActionFunc(Packet *p, void *handle)
{   DeviceTaskRec *data = (DeviceTaskRec *)handle;

    if (p == NoWork) {
         if ((p = data->Pending()) == NoWork)
              return bm.Wait();
         else {
              data->SetPending(NoWork);
              return bm.QueuePacket(p);
         }
    }
    else {
         data->SetPending(p);
         if (bm.tracing) bm.Trace(p->Datum());
         return bm.HoldSelf();
    }
}


TaskControlBlock *HandlerTCB::ActionFunc(Packet *p, void *handle)
{   HandlerTaskRec *data = (HandlerTaskRec *)handle;
    Packet *work, *devicePacket;
    int count;

    if (p != NoWork) {
         if (p->Kind() == WorkPacket)
              data->WorkInAdd(p);
         else data->DeviceInAdd(p);
    }
    if ((work = data->WorkIn()) == NoWork)
         return bm.Wait();
    else {
         count = work->Datum();
         if (count > 4) {
              data->SetWorkIn(work->Link());
              return bm.QueuePacket(work);
         }
         else {
              if ((devicePacket = data->DeviceIn()) == NoWork)
                   return bm.Wait();
              else {
                   data->SetDeviceIn(devicePacket->Link());
                   devicePacket->SetDatum(work->Data()[count-1]);
                   work->SetDatum(count + 1);
                   return bm.QueuePacket(devicePacket);
              }
         }
    }
}


TaskControlBlock *IdlerTCB::ActionFunc(Packet *p, void *handle)
/* p is not used here */
{   IdleTaskRec *data = (IdleTaskRec *)handle;

    p, 0;

    data->SetCount(data->Count() - 1);
    if (data->Count() == 0)
         return bm.HoldSelf();
    else if ((data->Control() & 1) == 0) {
              data->SetControl(data->Control() / 2);
              return bm.Release(DeviceA);
         }
         else {
              data->SetControl((data->Control() / 2) ^ 53256);
              return bm.Release(DeviceB);
         }
}


TaskControlBlock *WorkerTCB::ActionFunc(Packet *p, void *handle)
{   WorkerTaskRec *data = (WorkerTaskRec *)handle;
    Identity dest;

    if (p == NoWork)
         return bm.Wait();
    else {
         dest = (data->Destination() == HandlerA) ? HandlerB : HandlerA;
         data->SetDestination(dest);
         p->SetIdent(dest);
         p->SetDatum(1);
         for(int i = 0; i < 4; i++) {
              data->SetCount(data->Count() + 1);
              if (data->Count() > 26) data->SetCount(1);
              p->Data()[i] = 'A' + data->Count();
         }
         return bm.QueuePacket(p);
    }
}