[1365] | 1 | #include "toiprocessor.h"
|
---|
| 2 | #include "toimanager.h"
|
---|
| 3 | #include <pthread.h>
|
---|
| 4 | #include <values.h>
|
---|
| 5 |
|
---|
| 6 | #ifdef WITH_SOPHYA
|
---|
| 7 | #include "pexceptions.h"
|
---|
| 8 | #else
|
---|
| 9 | #include "apexceptions.h"
|
---|
| 10 | #endif
|
---|
| 11 |
|
---|
| 12 | #define pthread_mutexattr_setkind_np pthread_mutexattr_settype
|
---|
| 13 |
|
---|
| 14 | TOIProcessor::TOIProcessor() {
|
---|
| 15 | //cout << "TOIProcessor::TOIProcessor" << endl;
|
---|
| 16 | outTOIs = NULL;
|
---|
| 17 | inTOIs = NULL;
|
---|
| 18 | inited=false;
|
---|
| 19 |
|
---|
| 20 | upExtra = 0;
|
---|
| 21 | lowExtra = 0;
|
---|
| 22 | minOut = -1;
|
---|
| 23 | maxOut = -1;
|
---|
| 24 | neededHistory = 1000;
|
---|
| 25 | lastAWN = 0;
|
---|
| 26 | wontNeedValue = -1;
|
---|
| 27 |
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | TOIProcessor::~TOIProcessor() {
|
---|
| 31 | delete[] outTOIs;
|
---|
| 32 | delete[] inTOIs;
|
---|
| 33 | //if (mutex)
|
---|
| 34 | pthread_mutex_destroy(&mutex);
|
---|
| 35 | //if (dataReady)
|
---|
| 36 | pthread_cond_destroy(&dataReady);
|
---|
| 37 | pthread_detach(thread);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | void TOIProcessor::init() {
|
---|
| 42 | //cout << "TOIProcessor::init" << endl;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | void TOIProcessor::afterinit() {
|
---|
[1437] | 46 | int i;
|
---|
[1365] | 47 | inTOIs = new (TOI*[inIx.size()]);
|
---|
[1437] | 48 | for(i=0; i<inIx.size(); i++)
|
---|
[1439] | 49 | inTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
[1365] | 50 | outTOIs = new (TOI*[outIx.size()]);
|
---|
[1437] | 51 | for(i=0; i<outIx.size(); i++)
|
---|
[1439] | 52 | outTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
[1365] | 53 | }
|
---|
| 54 |
|
---|
| 55 | int TOIProcessor::getMinOut() {
|
---|
| 56 | //cout << name << "minout" << endl;
|
---|
| 57 | if (minOut < 0) minOut = calcMinOut();
|
---|
| 58 | //cout << name << "minout=" << minOut << endl;
|
---|
| 59 | return minOut;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | int TOIProcessor::getMaxOut() {
|
---|
| 63 | //cout << name << "maxout" << endl;
|
---|
| 64 | if (maxOut < 0) maxOut = calcMaxOut();
|
---|
| 65 | //cout << name << "maxout=" << maxOut << endl;
|
---|
| 66 | return maxOut;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | int TOIProcessor::calcMinOut() {
|
---|
| 70 | return getMinIn() + lowExtra;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | int TOIProcessor::calcMaxOut() {
|
---|
| 74 | return getMaxIn() - upExtra;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | int TOIProcessor::getMinIn() {
|
---|
| 78 | int nIn = inIx.size();
|
---|
| 79 | int minIn = 0;
|
---|
| 80 | for (int i=0; i<nIn; i++) {
|
---|
| 81 | TOI* toi = inTOIs[i];
|
---|
[1439] | 82 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
[1365] | 83 | int x = toi->getMinSn();
|
---|
| 84 | if (x > minIn) minIn = x;
|
---|
| 85 | }
|
---|
| 86 | return minIn;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | int TOIProcessor::getMaxIn() {
|
---|
| 90 | int nIn = inIx.size();
|
---|
| 91 | int maxIn = MAXINT;
|
---|
| 92 | for (int i=0; i<nIn; i++) {
|
---|
| 93 | TOI* toi = inTOIs[i];
|
---|
[1439] | 94 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
[1365] | 95 | int x = toi->getMaxSn();
|
---|
| 96 | if (x < maxIn) maxIn = x;
|
---|
| 97 | }
|
---|
| 98 | return maxIn;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | int TOIProcessor::declareInput(string toi) {
|
---|
| 103 | if (inIx.find(toi) != inIx.end())
|
---|
| 104 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared");
|
---|
| 105 | int i = inIx.size();
|
---|
| 106 | inIx[toi] = i;
|
---|
| 107 | return i;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | int TOIProcessor::declareOutput(string toi) {
|
---|
| 111 | if (outIx.find(toi) != outIx.end())
|
---|
| 112 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared");
|
---|
| 113 | int i = outIx.size();
|
---|
| 114 | outIx[toi] = i;
|
---|
| 115 | return i;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | int TOIProcessor::getInputTOIIndex(string toi) {
|
---|
[1367] | 119 | chkinit();
|
---|
[1365] | 120 | map<string, int>::iterator i = inIx.find(toi);
|
---|
| 121 | if (i == inIx.end()) return -1;
|
---|
| 122 | return (*i).second;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | int TOIProcessor::getOutputTOIIndex(string toi) {
|
---|
[1367] | 126 | chkinit();
|
---|
[1365] | 127 | map<string, int>::iterator i = outIx.find(toi);
|
---|
| 128 | if (i == outIx.end()) return -1;
|
---|
| 129 | return (*i).second;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[1437] | 132 | // Methodes rajoutees par Reza 11/3/2001
|
---|
| 133 | TOI* TOIProcessor::getInputTOI(int toiIndex) {
|
---|
| 134 | // chkinit();
|
---|
| 135 | if (toiIndex >= inIx.size())
|
---|
| 136 | throw RangeCheckError("TOIProcessor::getInputTOI() out of bound toiIndex");
|
---|
| 137 | TOI* toi = inTOIs[toiIndex];
|
---|
| 138 | if (toi == NULL)
|
---|
| 139 | throw NullPtrError("TOIProcessor::getInputTOI() - Not assigned TOI !");
|
---|
| 140 | return(toi);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | TOI* TOIProcessor::getOutputTOI(int toiIndex) {
|
---|
| 144 | // chkinit();
|
---|
| 145 | if (toiIndex >= outIx.size())
|
---|
| 146 | throw RangeCheckError("TOIProcessor::getOutputTOI() out of bound toiIndex");
|
---|
| 147 | TOI* toi = outTOIs[toiIndex];
|
---|
| 148 | if (toi == NULL)
|
---|
| 149 | throw NullPtrError("TOIProcessor::getOutputTOI() - Not assigned TOI !");
|
---|
| 150 | return(toi);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | bool TOIProcessor::checkInputTOIIndex(int toiIndex) {
|
---|
| 154 | if (toiIndex >= inIx.size()) return false;
|
---|
| 155 | if (inTOIs[toiIndex] == NULL) return false;
|
---|
| 156 | return true;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | bool TOIProcessor::checkOutputTOIIndex(int toiIndex) {
|
---|
| 160 | if (toiIndex >= outIx.size()) return false;
|
---|
| 161 | if (outTOIs[toiIndex] == NULL) return false;
|
---|
| 162 | return true;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | void TOIProcessor::PrintStatus(ostream & os)
|
---|
| 166 | {
|
---|
[1439] | 167 | chkinit();
|
---|
[1437] | 168 | os << " TOIProcessor::PrintStatus() - Name= " << name
|
---|
| 169 | << " MinIn=" << getMinIn() << " MaxIn=" << getMaxIn() << endl;
|
---|
| 170 | os << " --- Inputs N= " << inIx.size() << endl;
|
---|
| 171 | int k;
|
---|
| 172 | for(k=0; k<inIx.size(); k++) {
|
---|
| 173 | os << "Input[" << k << "] : " << getInName(k) ;
|
---|
| 174 | if (inTOIs[k] != NULL)
|
---|
| 175 | os << " Connected TOI " << inTOIs[k]->getName() << endl;
|
---|
| 176 | else os << " NO TOI " << endl;
|
---|
| 177 | }
|
---|
| 178 | os << " --- Outputs N= " << outIx.size() << endl;
|
---|
| 179 | for(k=0; k<outIx.size(); k++) {
|
---|
| 180 | os << "Output[" << k << "] : " << getOutName(k) ;
|
---|
| 181 | if (outTOIs[k] != NULL)
|
---|
| 182 | os << " Connected TOI " << outTOIs[k]->getName() << endl;
|
---|
| 183 | else os << " NO TOI " << endl;
|
---|
| 184 | }
|
---|
| 185 | os << endl;
|
---|
| 186 | return;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | // Fin rajout Reza 11/3/2001
|
---|
| 190 |
|
---|
[1365] | 191 | void TOIProcessor::addInput(string name, TOI* toi) {
|
---|
| 192 | chkinit();
|
---|
| 193 | map<string, int>::iterator i = inIx.find(name);
|
---|
| 194 | if (i == inIx.end()) throw NotFoundExc("TOIProcessor::addInput "+
|
---|
| 195 | name+" not declared");
|
---|
[1439] | 196 | inTOIs[(*i).second] = toi;
|
---|
| 197 | toi->addConsumer(this); // $CHECK$ Reza 13/3/2001
|
---|
[1365] | 198 | }
|
---|
| 199 |
|
---|
| 200 | void TOIProcessor::addOutput(string name, TOI* toi) {
|
---|
| 201 | chkinit();
|
---|
| 202 | map<string, int>::iterator i = outIx.find(name);
|
---|
| 203 | if (i == outIx.end()) throw NotFoundExc("TOIProcessor::addOutput "+
|
---|
| 204 | name+" not declared");
|
---|
| 205 | toi->setProducer(this);
|
---|
| 206 | outTOIs[(*i).second] = toi;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[1367] | 209 | string TOIProcessor::getOutName(int i) {
|
---|
| 210 | if (i > outIx.size()) throw RangeCheckError("TOIProcessor::getOutName "
|
---|
| 211 | " out of bound");
|
---|
[1437] | 212 | map<string, int>::iterator j;
|
---|
| 213 | for(j=outIx.begin(); j!= outIx.end(); j++)
|
---|
| 214 | if ((*j).second == i) return (*j).first;
|
---|
| 215 |
|
---|
| 216 | throw RangeCheckError("TOIProcessor::getOutName Not found index !");
|
---|
[1367] | 217 | }
|
---|
| 218 |
|
---|
| 219 | string TOIProcessor::getInName(int i) {
|
---|
| 220 | if (i > inIx.size()) throw RangeCheckError("TOIProcessor::getInName "
|
---|
| 221 | " out of bound");
|
---|
[1437] | 222 | map<string, int>::iterator j;
|
---|
| 223 | for(j=inIx.begin(); j!= inIx.end(); j++)
|
---|
| 224 | if ((*j).second == i) return (*j).first;
|
---|
| 225 |
|
---|
| 226 | throw RangeCheckError("TOIProcessor::getOutName Not found index !");
|
---|
[1367] | 227 | }
|
---|
| 228 |
|
---|
[1365] | 229 | void TOIProcessor::run() {
|
---|
| 230 |
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | void* TOIProcessor::ThreadStart(void* arg) {
|
---|
| 234 | TOIProcessor* p = (TOIProcessor*) arg;
|
---|
| 235 | // cout << p->name << " new thread running " << pthread_self() << endl;
|
---|
| 236 | p->run();
|
---|
| 237 | // cout << p->name << " thread done " << pthread_self() << endl;
|
---|
| 238 | return NULL;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | #ifdef Linux
|
---|
| 242 | #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
|
---|
| 243 | #define PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK_NP
|
---|
| 244 | #define pthread_mutex_setname_np(a,b,c)
|
---|
| 245 | #define pthread_cond_setname_np(a,b,c)
|
---|
| 246 | #endif
|
---|
| 247 |
|
---|
| 248 | void TOIProcessor::start() {
|
---|
| 249 | pthread_cond_init(&dataReady, NULL);
|
---|
| 250 | pthread_mutexattr_init(&mutattr);
|
---|
| 251 | // pthread_mutexattr_settype(&mutattr, PTHREAD_MUTEX_ERRORCHECK);
|
---|
| 252 | pthread_mutex_init(&mutex, &mutattr);
|
---|
| 253 | //pthread_mutex_setname_np(&mutex, (name + "_proc_mutex").c_str(), 0);
|
---|
| 254 | //pthread_cond_setname_np(&dataReady, (name + "_proc_cond").c_str(), 0);
|
---|
| 255 | //cout << name << " starting thread " << &thread << endl;
|
---|
| 256 | pthread_create(&thread, NULL, ThreadStart, this);
|
---|
| 257 | TOIManager::getManager()->addThread(&thread);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | #ifndef NO_SOPHYA
|
---|
| 261 | Array TOIProcessor::getData(int toiIndex, int iStart, int iEnd) {
|
---|
[1437] | 262 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 263 | toi->waitForData(iStart, iEnd);
|
---|
| 264 | return toi->getData(iStart, iEnd);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | Array TOIProcessor::getError(int toiIndex, int iStart, int iEnd) {
|
---|
[1437] | 268 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 269 | toi->waitForData(iStart, iEnd);
|
---|
| 270 | return toi->getError(iStart, iEnd);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | TArray<int_4> TOIProcessor::getFlag(int toiIndex, int iStart, int iEnd) {
|
---|
[1437] | 274 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 275 | toi->waitForData(iStart, iEnd);
|
---|
| 276 | return toi->getFlag(iStart, iEnd);
|
---|
| 277 | }
|
---|
| 278 | #endif
|
---|
| 279 |
|
---|
| 280 | double TOIProcessor::getData(int toiIndex, int i) {
|
---|
[1437] | 281 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 282 | toi->waitForData(i);
|
---|
| 283 | return toi->getData(i);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | double TOIProcessor::getError(int toiIndex, int i) {
|
---|
[1437] | 287 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 288 | toi->waitForData(i);
|
---|
| 289 | return toi->getError(i);
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | int_4 TOIProcessor::getFlag(int toiIndex, int i) {
|
---|
[1437] | 293 | TOI* toi = getInputTOI(toiIndex);
|
---|
[1365] | 294 | toi->waitForData(i);
|
---|
| 295 | return toi->getFlag(i);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | void TOIProcessor::setNeededHistory(int nsamples) {
|
---|
| 299 | neededHistory = nsamples;
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | void TOIProcessor::wontNeedBefore(int i) {
|
---|
| 303 | if (i<wontNeedValue) return;
|
---|
| 304 | wontNeedValue = i;
|
---|
| 305 | for (int j=0; j< (int) inIx.size(); j++) {
|
---|
| 306 | inTOIs[j]->wontNeedBefore(i);
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | void TOIProcessor::autoWontNeed(int iCur) {
|
---|
| 311 | if (neededHistory <=0) return;
|
---|
| 312 | if (iCur < lastAWN + neededHistory) return;
|
---|
| 313 | lastAWN = iCur;
|
---|
| 314 | //cout << name << " wontNeedBefore " << iCur-neededHistory << endl;
|
---|
| 315 | wontNeedBefore(iCur-neededHistory);
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | void TOIProcessor::notify() {
|
---|
| 319 | lock();
|
---|
| 320 | /* if (mutex.__m_lock.__status == 0) {
|
---|
| 321 | cout << "wait without lock" << endl; abort();
|
---|
| 322 | }*/
|
---|
| 323 |
|
---|
| 324 | pthread_cond_broadcast(&dataReady);
|
---|
| 325 | unlock();
|
---|
| 326 | }
|
---|
| 327 |
|
---|
| 328 |
|
---|
| 329 | void TOIProcessor::putData(int toiIndex, int i, double value, int_4 flg) {
|
---|
[1437] | 330 | TOI* toi = getOutputTOI(toiIndex);
|
---|
[1365] | 331 | toi->putData(i, value, flg);
|
---|
| 332 | autoWontNeed(i);
|
---|
| 333 | notify();
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 |
|
---|
| 337 | void TOIProcessor::putDataError(int toiIndex, int i, double value,
|
---|
| 338 | double error, int_4 flg) {
|
---|
[1437] | 339 | TOI* toi = getOutputTOI(toiIndex);
|
---|
| 340 | if (toi == NULL)
|
---|
| 341 | throw NullPtrError("TOIProcessor::putDataError() - Not assigned TOI !");
|
---|
[1365] | 342 | toi->putDataError(i, value, error, flg);
|
---|
| 343 | autoWontNeed(i);
|
---|
| 344 | notify();
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 |
|
---|