| 1 | // ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL
 | 
|---|
| 2 | //                               Eric Aubourg
 | 
|---|
| 3 | //                               Christophe Magneville
 | 
|---|
| 4 | //                               Reza Ansari
 | 
|---|
| 5 | // $Id: toi.cc,v 1.12 2002-09-09 15:33:14 aubourg Exp $
 | 
|---|
| 6 | 
 | 
|---|
| 7 | #include "toiprocessor.h"
 | 
|---|
| 8 | #include "toi.h"
 | 
|---|
| 9 | #include <pthread.h>
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifdef WITH_SOPHYA
 | 
|---|
| 12 | #include "pexceptions.h"
 | 
|---|
| 13 | #else
 | 
|---|
| 14 | #include "apexceptions.h"
 | 
|---|
| 15 | #endif
 | 
|---|
| 16 | 
 | 
|---|
| 17 | 
 | 
|---|
| 18 | TOI::TOI() {
 | 
|---|
| 19 |   TOIInit();
 | 
|---|
| 20 | }
 | 
|---|
| 21 | 
 | 
|---|
| 22 | TOI::TOI(string n) {
 | 
|---|
| 23 |   name = n;
 | 
|---|
| 24 |   TOIInit();
 | 
|---|
| 25 | }
 | 
|---|
| 26 | 
 | 
|---|
| 27 | void TOI::TOIInit() {
 | 
|---|
| 28 |   pthread_mutex_init(&mutex, NULL);
 | 
|---|
| 29 |   //   ----- Rajouts Reza 12/3/2001
 | 
|---|
| 30 |   pthread_cond_init(&condv, NULL);
 | 
|---|
| 31 |   fgwaitput = fgwaitget = false;
 | 
|---|
| 32 |   fgsigput = fgsigget = false;
 | 
|---|
| 33 |   countwaitput = countwaitget = 0;
 | 
|---|
| 34 |   //  Fin rajouts Reza 12/3/2001 ------
 | 
|---|
| 35 | //  pthread_mutex_setname_np(&mutex, (name + "_toi_mutex").c_str(), 0);
 | 
|---|
| 36 |   defaultValue = 0;
 | 
|---|
| 37 |   producer = NULL;
 | 
|---|
| 38 |   dbg = false;
 | 
|---|
| 39 |   syncOldWay = true;
 | 
|---|
| 40 | }
 | 
|---|
| 41 | 
 | 
|---|
| 42 | TOI::~TOI() {
 | 
|---|
| 43 |   pthread_mutex_destroy(&mutex);
 | 
|---|
| 44 | }
 | 
|---|
| 45 | 
 | 
|---|
| 46 | void TOI::PrintStatus(::ostream & os) const
 | 
|---|
| 47 | {
 | 
|---|
| 48 |   os << "TOI::PrintStatus() - Name=" << getName() << endl;
 | 
|---|
| 49 |   os << " WaitStatus: Put/" ;
 | 
|---|
| 50 |   if (isPutWaiting()) os << "Waiting " ;
 | 
|---|
| 51 |   else os << "Running ";
 | 
|---|
| 52 |   os << " PutCountWait= " << getCountWaitPut() << endl;
 | 
|---|
| 53 |   os << " WaitStatus: Get/" ;
 | 
|---|
| 54 |   if (isGetWaiting()) os << "Waiting " ;
 | 
|---|
| 55 |   else os << "Running ";
 | 
|---|
| 56 |   os << " GetCountWait= " << getCountWaitGet() << endl;
 | 
|---|
| 57 | }
 | 
|---|
| 58 | 
 | 
|---|
| 59 | 
 | 
|---|
| 60 | void TOI::setProducer(TOIProcessor* p) {
 | 
|---|
| 61 |   if (producer) 
 | 
|---|
| 62 |     throw DuplicateIdExc("TOI::setProducer : producer already defined");
 | 
|---|
| 63 |   producer = p;
 | 
|---|
| 64 | }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | void TOI::addConsumer(TOIProcessor* p) {
 | 
|---|
| 67 |   consumers.push_back(p);
 | 
|---|
| 68 | }
 | 
|---|
| 69 | 
 | 
|---|
| 70 | int TOI::getMinSn(){
 | 
|---|
| 71 |   return producer->getMinOut();
 | 
|---|
| 72 | }
 | 
|---|
| 73 | 
 | 
|---|
| 74 | int TOI::getMaxSn(){
 | 
|---|
| 75 |   return producer->getMaxOut();
 | 
|---|
| 76 | }
 | 
|---|
| 77 | 
 | 
|---|
| 78 | 
 | 
|---|
| 79 | // ajout vf 31/07/2002
 | 
|---|
| 80 | bool TOI::checkSampleLimits(long& Min, long& Max, int pass)
 | 
|---|
| 81 | {
 | 
|---|
| 82 |   // propagation de la sortie du TOI vers son entree pour test des limites
 | 
|---|
| 83 |   return producer->checkSampleLimits(Min, Max, pass);
 | 
|---|
| 84 | }
 | 
|---|
| 85 | 
 | 
|---|
| 86 | 
 | 
|---|
| 87 | // fin ajout vf
 | 
|---|
| 88 | 
 | 
|---|
| 89 | 
 | 
|---|
| 90 | 
 | 
|---|
| 91 | 
 | 
|---|
| 92 | 
 | 
|---|
| 93 | 
 | 
|---|
| 94 | 
 | 
|---|
| 95 | 
 | 
|---|
| 96 | /* 
 | 
|---|
| 97 |  RZCMV   ----- l'interface va etre modifiee, NE PAS UTILISER
 | 
|---|
| 98 | #ifndef NO_SOPHYA 
 | 
|---|
| 99 | Array TOI::getError(int iStart, int iEnd) {
 | 
|---|
| 100 |   if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
 | 
|---|
| 101 |   return errorTOI->getData(iStart, iEnd); 
 | 
|---|
| 102 | }
 | 
|---|
| 103 | Array TOI::getData(int iStart, int iEnd) {
 | 
|---|
| 104 |   lock();
 | 
|---|
| 105 |   Array a = doGetData(iStart, iEnd);
 | 
|---|
| 106 |   unlock();
 | 
|---|
| 107 |   if (fgsigput) { fgsigput = false; broadcast(); }
 | 
|---|
| 108 |   return a;
 | 
|---|
| 109 | }
 | 
|---|
| 110 | TArray<int_4> TOI::getFlag(int iStart, int iEnd) {
 | 
|---|
| 111 |   lock();
 | 
|---|
| 112 |   TArray<int_4> a = doGetFlag(iStart, iEnd);
 | 
|---|
| 113 |   unlock();
 | 
|---|
| 114 |   if (fgsigput) { fgsigput = false; broadcast(); }
 | 
|---|
| 115 |   return a;
 | 
|---|
| 116 | }
 | 
|---|
| 117 | #endif
 | 
|---|
| 118 |   l'interface va etre modifiee, NE PAS UTILISER ---- 
 | 
|---|
| 119 | */
 | 
|---|
| 120 | 
 | 
|---|
| 121 | 
 | 
|---|
| 122 | /* 
 | 
|---|
| 123 | RZCMV  -------   A revoir les getError() ...
 | 
|---|
| 124 | double TOI::getError(int i) {
 | 
|---|
| 125 |   if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
 | 
|---|
| 126 |   return errorTOI->getData(i);
 | 
|---|
| 127 | }
 | 
|---|
| 128 | 
 | 
|---|
| 129 | void TOI::putDataError(int i, double value, double error, int_4 flag) {
 | 
|---|
| 130 |   if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
 | 
|---|
| 131 |   putData(i, value, flag);
 | 
|---|
| 132 |   errorTOI->putData(i, value, flag);
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | */
 | 
|---|
| 136 | 
 | 
|---|
| 137 | double TOI::getData(int i) { /* deprecated, overriden in toisegment */
 | 
|---|
| 138 |   lock();
 | 
|---|
| 139 |   uint_8 flg;
 | 
|---|
| 140 |   double dat;
 | 
|---|
| 141 |   doGetData(i, dat, flg);
 | 
|---|
| 142 |   unlock();
 | 
|---|
| 143 |   if (fgsigput) { fgsigput = false; broadcast(); }
 | 
|---|
| 144 |   return dat;
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | void TOI::getData(int i, double &data, uint_8 &flag) { /* deprecated, overriden in toisegment */
 | 
|---|
| 148 |   lock();
 | 
|---|
| 149 |   doGetData(i, data, flag);
 | 
|---|
| 150 |   unlock();
 | 
|---|
| 151 |   if (fgsigput) { fgsigput = false; broadcast(); }
 | 
|---|
| 152 |   return;
 | 
|---|
| 153 | }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | void TOI::getData(int i, int n, double* data, uint_8* flg) {
 | 
|---|
| 156 |   cerr << "TOI::getData [double*] unimplemented" << endl;
 | 
|---|
| 157 |   exit(-1);
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | void TOI::putData(int i, int n, double const* var, uint_8 const* flg) {
 | 
|---|
| 161 |   cerr << "TOI::putData [double*] unimplemented" << endl;
 | 
|---|
| 162 |   exit(-1);
 | 
|---|
| 163 | }
 | 
|---|
| 164 | 
 | 
|---|
| 165 | void TOI::putData(int i, double value, uint_8 flag) { /* deprecated, overriden in toisegment */
 | 
|---|
| 166 |   lock();
 | 
|---|
| 167 |   doPutData(i, value, flag);
 | 
|---|
| 168 |   unlock();
 | 
|---|
| 169 |   if (fgsigget) { fgsigget = false; broadcast(); }
 | 
|---|
| 170 | }
 | 
|---|
| 171 | 
 | 
|---|
| 172 | void TOI::waitForData(int iStart, int iEnd) { /* deprecated, overriden in toisegment */
 | 
|---|
| 173 |   if (producer == NULL) throw NotFoundExc("TOI has no producer !");
 | 
|---|
| 174 |     
 | 
|---|
| 175 |   DataStatus s = isDataAvail(iStart, iEnd);
 | 
|---|
| 176 |   if (s == DATA_OK) {
 | 
|---|
| 177 |       return;
 | 
|---|
| 178 |   }
 | 
|---|
| 179 |   if (s == DATA_DELETED) {
 | 
|---|
| 180 |       throw NotFoundExc("Data has been purged !");
 | 
|---|
| 181 |   }
 | 
|---|
| 182 | 
 | 
|---|
| 183 |   producer->lock();
 | 
|---|
| 184 |   while (isDataAvailNL(iStart, iEnd) == DATA_NOT_YET) {
 | 
|---|
| 185 |     producer->wait();
 | 
|---|
| 186 |   }
 | 
|---|
| 187 |   producer->unlock();
 | 
|---|
| 188 |   return;
 | 
|---|
| 189 | }
 | 
|---|
| 190 | 
 | 
|---|
| 191 | void TOI::waitForData(int i) { /* deprecated, overriden in toisegment */
 | 
|---|
| 192 |   waitForData(i,i);
 | 
|---|
| 193 | }
 | 
|---|
| 194 | 
 | 
|---|
| 195 | void TOI::waitForAnyData() { /* deprecated, overriden in toisegment */
 | 
|---|
| 196 |   if (! hasSomeData()) {
 | 
|---|
| 197 |     producer->lock();
 | 
|---|
| 198 |     producer->wait();
 | 
|---|
| 199 |     producer->unlock();
 | 
|---|
| 200 |   }
 | 
|---|
| 201 | }
 | 
|---|
| 202 | 
 | 
|---|
| 203 | TOI::DataStatus TOI::isDataAvail(int i) {
 | 
|---|
| 204 |   lock();
 | 
|---|
| 205 |   DataStatus stat = isDataAvailNL(i);
 | 
|---|
| 206 |   unlock();
 | 
|---|
| 207 |   return stat;
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|
| 210 | TOI::DataStatus TOI::isDataAvail(int i, int j) {
 | 
|---|
| 211 |   lock();
 | 
|---|
| 212 |   DataStatus stat = isDataAvailNL(i,j);
 | 
|---|
| 213 |   unlock();
 | 
|---|
| 214 |   return stat;
 | 
|---|
| 215 | }
 | 
|---|
| 216 | 
 | 
|---|
| 217 | TOI::DataStatus TOI::isDataAvailNL(int i) {
 | 
|---|
| 218 |   return isDataAvailNL(i,i);
 | 
|---|
| 219 | }
 | 
|---|
| 220 | 
 | 
|---|
| 221 | void TOI::wontNeedBefore(int i) { /* deprecated, overriden in toisegment */
 | 
|---|
| 222 |   int j=i;
 | 
|---|
| 223 |   for (vector<TOIProcessor*>::iterator k = consumers.begin();
 | 
|---|
| 224 |        k != consumers.end(); k++) {
 | 
|---|
| 225 |     if ((*k)->wontNeedValue < j) j = (*k)->wontNeedValue;
 | 
|---|
| 226 |   }
 | 
|---|
| 227 |   lock();
 | 
|---|
| 228 |   doWontNeedBefore(j);
 | 
|---|
| 229 |   unlock();
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | void TOI::doWontNeedBefore(int i) {
 | 
|---|
| 233 | }
 | 
|---|
| 234 | 
 | 
|---|
| 235 | 
 | 
|---|
| 236 | 
 | 
|---|
| 237 | 
 | 
|---|
| 238 | 
 | 
|---|
| 239 | 
 | 
|---|
| 240 | 
 | 
|---|
| 241 | 
 | 
|---|
| 242 | 
 | 
|---|
| 243 | 
 | 
|---|
| 244 | 
 | 
|---|
| 245 | 
 | 
|---|
| 246 | 
 | 
|---|
| 247 | 
 | 
|---|
| 248 | 
 | 
|---|