| 1 | // ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL
 | 
|---|
| 2 | //                               Eric Aubourg
 | 
|---|
| 3 | //                               Christophe Magneville
 | 
|---|
| 4 | //                               Reza Ansari
 | 
|---|
| 5 | // $Id: ringprocessor.cc,v 1.3 2003-05-28 21:03:52 aubourg Exp $
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "ringprocessor.h"
 | 
|---|
| 8 | #include "ringpipe.h"
 | 
|---|
| 9 | #include "toimanager.h"
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #include <iostream.h>
 | 
|---|
| 12 | #include <typeinfo>        // ajout pour linux
 | 
|---|
| 13 | #ifdef HAVE_VALUES_H
 | 
|---|
| 14 | #include <values.h>
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | #ifndef MAXINT
 | 
|---|
| 17 | #define MAXINT 2147483647
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #ifdef HAVE_STDINT_H
 | 
|---|
| 21 | #include <stdint.h>
 | 
|---|
| 22 | #endif
 | 
|---|
| 23 | 
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #ifdef WITH_SOPHYA
 | 
|---|
| 26 | #include "pexceptions.h"
 | 
|---|
| 27 | #else
 | 
|---|
| 28 | #include "apexceptions.h"
 | 
|---|
| 29 | #endif
 | 
|---|
| 30 | 
 | 
|---|
| 31 | RingProcessor::RingProcessor() {
 | 
|---|
| 32 |   outRings = NULL;
 | 
|---|
| 33 |   inRings = NULL;
 | 
|---|
| 34 |   inited = false;
 | 
|---|
| 35 | 
 | 
|---|
| 36 |   ringBegin = 0;
 | 
|---|
| 37 |   ringEnd = MAXINT;
 | 
|---|
| 38 | 
 | 
|---|
| 39 |   wontNeedRing = -1;
 | 
|---|
| 40 |   neededRingHistory = 3;
 | 
|---|
| 41 |   lastAWNR = -1;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |   TOIManager::getManager()->registerProcessor(this);
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | RingProcessor::~RingProcessor() {
 | 
|---|
| 47 |   delete[] inRings;
 | 
|---|
| 48 |   delete[] outRings;
 | 
|---|
| 49 | }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | 
 | 
|---|
| 52 | void RingProcessor::init()
 | 
|---|
| 53 | {}
 | 
|---|
| 54 | 
 | 
|---|
| 55 | void RingProcessor::afterinit() {
 | 
|---|
| 56 |   inRings = new (RingPipe*[inRingIx.size()]);
 | 
|---|
| 57 |   {for (int i=0; i<inRingIx.size(); i++)
 | 
|---|
| 58 |     inRings[i] = NULL;}
 | 
|---|
| 59 |   outRings = new (RingPipe*[outRingIx.size()]);
 | 
|---|
| 60 |   {for (int i=0; i<outRingIx.size(); i++)
 | 
|---|
| 61 |     outRings[i] = NULL;}
 | 
|---|
| 62 | }
 | 
|---|
| 63 | 
 | 
|---|
| 64 | void RingProcessor::setNeededRingHistory(int nr) {
 | 
|---|
| 65 |   neededRingHistory = nr;
 | 
|---|
| 66 | }
 | 
|---|
| 67 | 
 | 
|---|
| 68 | int RingProcessor::declareRingInput(string ring) {
 | 
|---|
| 69 |   if (inRingIx.find(ring) != inRingIx.end())
 | 
|---|
| 70 |     throw DuplicateIdExc("RingProcessor::declareRingInput : "+ring+
 | 
|---|
| 71 |                          " already declared");
 | 
|---|
| 72 |   int i = inRingIx.size();
 | 
|---|
| 73 |   inRingIx[ring] = i;
 | 
|---|
| 74 |   return i;
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | 
 | 
|---|
| 78 | int RingProcessor::declareRingOutput(string ring) {
 | 
|---|
| 79 |   if (outRingIx.find(ring) != outRingIx.end())
 | 
|---|
| 80 |     throw DuplicateIdExc("RingProcessor::declareRingOutput : "+ring+
 | 
|---|
| 81 |                          " already declared");
 | 
|---|
| 82 |   int i = outRingIx.size();
 | 
|---|
| 83 |   outRingIx[ring] = i;
 | 
|---|
| 84 |   return i;
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | void RingProcessor::addRingInput(string name, RingPipe* ring) {
 | 
|---|
| 88 |   chkinit();
 | 
|---|
| 89 |   map<string, int>::iterator i = inRingIx.find(name);
 | 
|---|
| 90 |   if (i == inRingIx.end()) throw NotFoundExc("RingProcessor::addInput "+
 | 
|---|
| 91 |                                          name+" not declared");
 | 
|---|
| 92 |   inRings[(*i).second] = ring;
 | 
|---|
| 93 |   ring->addConsumer(this);  
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | void RingProcessor::addRingOutput(string name, RingPipe* ring) {
 | 
|---|
| 97 |   chkinit();
 | 
|---|
| 98 |   map<string, int>::iterator i = outRingIx.find(name);
 | 
|---|
| 99 |   if (i == outRingIx.end()) throw NotFoundExc("RingProcessor::addOutput "+
 | 
|---|
| 100 |                                           name+" not declared");
 | 
|---|
| 101 |   ring->setProducer(this);
 | 
|---|
| 102 |   outRings[(*i).second] = ring;
 | 
|---|
| 103 | }
 | 
|---|
| 104 | 
 | 
|---|
| 105 | void RingProcessor::run()
 | 
|---|
| 106 | {}
 | 
|---|
| 107 | 
 | 
|---|
| 108 | void* RingProcessor::ThreadStart(void* arg) {
 | 
|---|
| 109 |   RingProcessor* r = (RingProcessor*) arg;
 | 
|---|
| 110 |   try {
 | 
|---|
| 111 |     r->run();
 | 
|---|
| 112 |   }
 | 
|---|
| 113 |   catch (PThrowable & exc) {
 | 
|---|
| 114 |     cerr << "\n RingProcessor::ThreadStart() Catched Exception RingProcessor@" 
 | 
|---|
| 115 |          << hex << r << dec << "\n"  
 | 
|---|
| 116 |          << (string)typeid(exc).name() 
 | 
|---|
| 117 |          << " - Msg= " << exc.Msg() << endl;
 | 
|---|
| 118 |   }
 | 
|---|
| 119 |   catch (const std::exception & sex) {
 | 
|---|
| 120 |     cerr << "\n RingProcessor::ThreadStart() Catched std::exception RingProcessor@" 
 | 
|---|
| 121 |          << hex << r << dec << "\n"  
 | 
|---|
| 122 |          << (string)typeid(sex).name() << endl; 
 | 
|---|
| 123 |   }
 | 
|---|
| 124 |   catch (...) {
 | 
|---|
| 125 |     cerr << "\n RingProcessor::ThreadStart() Catched ... exception RingProcessor@" 
 | 
|---|
| 126 |          << hex << r << dec << endl;
 | 
|---|
| 127 |   }
 | 
|---|
| 128 |   //  r->warnPutDone();
 | 
|---|
| 129 |   pthread_exit(NULL);
 | 
|---|
| 130 |   return NULL;
 | 
|---|
| 131 | }
 | 
|---|
| 132 | 
 | 
|---|
| 133 | 
 | 
|---|
| 134 | void RingProcessor::start() {
 | 
|---|
| 135 |   pthread_create(&thread, NULL, ThreadStart, this);
 | 
|---|
| 136 | }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | RingPipe* RingProcessor::getInputRing(int index) {
 | 
|---|
| 139 |   if (index >= inRingIx.size())
 | 
|---|
| 140 |     throw RangeCheckError("RingProcessor::getInputRing() out of bound");
 | 
|---|
| 141 |   RingPipe* ring = inRings[index];
 | 
|---|
| 142 |   if (ring == NULL)
 | 
|---|
| 143 |     throw NullPtrError("RingProcessor::getInputRing() - Not assigned Ring !");
 | 
|---|
| 144 |   return(ring);
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | RingPipe* RingProcessor::getOutputRing(int index) {
 | 
|---|
| 148 |   if (index >= outRingIx.size())
 | 
|---|
| 149 |     throw RangeCheckError("RingProcessor::getOutputRing() out of bound");
 | 
|---|
| 150 |   RingPipe* ring = outRings[index];
 | 
|---|
| 151 |   if (ring == NULL)
 | 
|---|
| 152 |     throw NullPtrError("RingProcessor::getOutputRing() - Not assigned Ring !");
 | 
|---|
| 153 |   return(ring);
 | 
|---|
| 154 | }
 | 
|---|
| 155 | 
 | 
|---|
| 156 | RingPipe* RingProcessor::getOutRing(string out)
 | 
|---|
| 157 | {
 | 
|---|
| 158 |   // recherche du nom de la sortie et verification si le toi existe deja
 | 
|---|
| 159 |   map<string, int>::iterator i = outRingIx.find(out);
 | 
|---|
| 160 |   if (i == outRingIx.end()) {
 | 
|---|
| 161 |     return NULL;
 | 
|---|
| 162 |   } else {
 | 
|---|
| 163 |     return outRings[(*i).second];
 | 
|---|
| 164 |   }
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | 
 | 
|---|
| 168 | 
 | 
|---|
| 169 | Ring const* RingProcessor::getRing(int index, int i) {
 | 
|---|
| 170 |   RingPipe* pipe = getInputRing(index);
 | 
|---|
| 171 |   pipe->waitForRing(i);
 | 
|---|
| 172 |   return pipe->getRing(i);
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | 
 | 
|---|
| 176 | void RingProcessor::putRing(int index, int i, Ring const* ring) {
 | 
|---|
| 177 |   RingPipe* pipe = getOutputRing(index);
 | 
|---|
| 178 |   if (pipe == NULL) return;
 | 
|---|
| 179 |   pipe->putRing(i, ring);
 | 
|---|
| 180 | }
 | 
|---|
| 181 | 
 | 
|---|
| 182 | void RingProcessor::wontNeedRingBefore(int i) {
 | 
|---|
| 183 |   if (i<wontNeedRing) return;
 | 
|---|
| 184 |   wontNeedRing = i;
 | 
|---|
| 185 |   for (int j=0; j< (int) inRingIx.size();  j++) {
 | 
|---|
| 186 |     if (inRings[j])  inRings[j]->wontNeedRingBefore(i);
 | 
|---|
| 187 |   }
 | 
|---|
| 188 | }
 | 
|---|
| 189 | 
 | 
|---|
| 190 | void RingProcessor::autoWontNeedRing(int iCur) {
 | 
|---|
| 191 |   if (neededRingHistory <=0) return;
 | 
|---|
| 192 |   if (iCur <= lastAWNR + neededRingHistory/10) return;
 | 
|---|
| 193 |   lastAWNR = iCur;
 | 
|---|
| 194 |   wontNeedRingBefore(iCur-neededRingHistory);
 | 
|---|
| 195 | }
 | 
|---|
| 196 | 
 | 
|---|
| 197 | 
 | 
|---|
| 198 | void RingProcessor::getRingRange(int& min, int&max) {
 | 
|---|
| 199 |   if (min < ringBegin) min = ringBegin;
 | 
|---|
| 200 |   if (max > ringEnd)   max = ringEnd;
 | 
|---|
| 201 | 
 | 
|---|
| 202 |   for (int i=0; i<inRingIx.size(); i++)
 | 
|---|
| 203 |     inRings[i]->getRingRange(min, max);
 | 
|---|
| 204 | 
 | 
|---|
| 205 |   ringBegin = min;
 | 
|---|
| 206 |   ringEnd   = max;
 | 
|---|
| 207 | }
 | 
|---|
| 208 | 
 | 
|---|