| 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() { | 
|---|
| 46 | inTOIs = new (TOI*[inIx.size()]); | 
|---|
| 47 | outTOIs = new (TOI*[outIx.size()]); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | int TOIProcessor::getMinOut() { | 
|---|
| 51 | //cout << name << "minout" << endl; | 
|---|
| 52 | if (minOut < 0) minOut = calcMinOut(); | 
|---|
| 53 | //cout << name << "minout=" << minOut << endl; | 
|---|
| 54 | return minOut; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | int TOIProcessor::getMaxOut() { | 
|---|
| 58 | //cout << name << "maxout" << endl; | 
|---|
| 59 | if (maxOut < 0) maxOut = calcMaxOut(); | 
|---|
| 60 | //cout << name << "maxout=" << maxOut << endl; | 
|---|
| 61 | return maxOut; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | int TOIProcessor::calcMinOut() { | 
|---|
| 65 | return getMinIn() + lowExtra; | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | int TOIProcessor::calcMaxOut() { | 
|---|
| 69 | return getMaxIn() - upExtra; | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | int TOIProcessor::getMinIn() { | 
|---|
| 73 | int nIn = inIx.size(); | 
|---|
| 74 | int minIn = 0; | 
|---|
| 75 | for (int i=0; i<nIn; i++) { | 
|---|
| 76 | TOI* toi = inTOIs[i]; | 
|---|
| 77 | int x = toi->getMinSn(); | 
|---|
| 78 | if (x > minIn) minIn = x; | 
|---|
| 79 | } | 
|---|
| 80 | return minIn; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | int TOIProcessor::getMaxIn() { | 
|---|
| 84 | int nIn = inIx.size(); | 
|---|
| 85 | int maxIn = MAXINT; | 
|---|
| 86 | for (int i=0; i<nIn; i++) { | 
|---|
| 87 | TOI* toi = inTOIs[i]; | 
|---|
| 88 | int x = toi->getMaxSn(); | 
|---|
| 89 | if (x < maxIn) maxIn = x; | 
|---|
| 90 | } | 
|---|
| 91 | return maxIn; | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 |  | 
|---|
| 95 | int TOIProcessor::declareInput(string toi) { | 
|---|
| 96 | if (inIx.find(toi) != inIx.end()) | 
|---|
| 97 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared"); | 
|---|
| 98 | int i = inIx.size(); | 
|---|
| 99 | inIx[toi] = i; | 
|---|
| 100 | return i; | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | int  TOIProcessor::declareOutput(string toi) { | 
|---|
| 104 | if (outIx.find(toi) != outIx.end()) | 
|---|
| 105 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared"); | 
|---|
| 106 | int i = outIx.size(); | 
|---|
| 107 | outIx[toi] = i; | 
|---|
| 108 | return i; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | int TOIProcessor::getInputTOIIndex(string toi) { | 
|---|
| 112 | chkinit(); | 
|---|
| 113 | map<string, int>::iterator i = inIx.find(toi); | 
|---|
| 114 | if (i == inIx.end()) return -1; | 
|---|
| 115 | return (*i).second; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | int TOIProcessor::getOutputTOIIndex(string toi) { | 
|---|
| 119 | chkinit(); | 
|---|
| 120 | map<string, int>::iterator i = outIx.find(toi); | 
|---|
| 121 | if (i == outIx.end()) return -1; | 
|---|
| 122 | return (*i).second; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void TOIProcessor::addInput(string name, TOI* toi) { | 
|---|
| 126 | chkinit(); | 
|---|
| 127 | map<string, int>::iterator i = inIx.find(name); | 
|---|
| 128 | if (i == inIx.end()) throw NotFoundExc("TOIProcessor::addInput "+ | 
|---|
| 129 | name+" not declared"); | 
|---|
| 130 | inTOIs[(*i).second] = toi; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void TOIProcessor::addOutput(string name, TOI* toi) { | 
|---|
| 134 | chkinit(); | 
|---|
| 135 | map<string, int>::iterator i = outIx.find(name); | 
|---|
| 136 | if (i == outIx.end()) throw NotFoundExc("TOIProcessor::addOutput "+ | 
|---|
| 137 | name+" not declared"); | 
|---|
| 138 | toi->setProducer(this); | 
|---|
| 139 | outTOIs[(*i).second] = toi; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | string TOIProcessor::getOutName(int i) { | 
|---|
| 143 | if (i > outIx.size()) throw RangeCheckError("TOIProcessor::getOutName " | 
|---|
| 144 | " out of bound"); | 
|---|
| 145 | map<string, int>::iterator j = outIx.begin(); | 
|---|
| 146 | while (i) {i--; j++;}; | 
|---|
| 147 | return (*j).first; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | string TOIProcessor::getInName(int i) { | 
|---|
| 151 | if (i > inIx.size()) throw RangeCheckError("TOIProcessor::getInName " | 
|---|
| 152 | " out of bound"); | 
|---|
| 153 | map<string, int>::iterator j = inIx.begin(); | 
|---|
| 154 | while (i) {i--; j++;}; | 
|---|
| 155 | return (*j).first; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void TOIProcessor::run() { | 
|---|
| 159 |  | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void* TOIProcessor::ThreadStart(void* arg) { | 
|---|
| 163 | TOIProcessor* p = (TOIProcessor*) arg; | 
|---|
| 164 | //  cout << p->name << " new thread running " << pthread_self() << endl; | 
|---|
| 165 | p->run(); | 
|---|
| 166 | //  cout << p->name << " thread done " << pthread_self() << endl; | 
|---|
| 167 | return NULL; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | #ifdef Linux | 
|---|
| 171 | #define pthread_mutexattr_settype  pthread_mutexattr_setkind_np | 
|---|
| 172 | #define  PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK_NP | 
|---|
| 173 | #define pthread_mutex_setname_np(a,b,c) | 
|---|
| 174 | #define pthread_cond_setname_np(a,b,c) | 
|---|
| 175 | #endif | 
|---|
| 176 |  | 
|---|
| 177 | void TOIProcessor::start() { | 
|---|
| 178 | pthread_cond_init(&dataReady, NULL); | 
|---|
| 179 | pthread_mutexattr_init(&mutattr); | 
|---|
| 180 | //  pthread_mutexattr_settype(&mutattr, PTHREAD_MUTEX_ERRORCHECK); | 
|---|
| 181 | pthread_mutex_init(&mutex, &mutattr); | 
|---|
| 182 | //pthread_mutex_setname_np(&mutex, (name + "_proc_mutex").c_str(), 0); | 
|---|
| 183 | //pthread_cond_setname_np(&dataReady, (name + "_proc_cond").c_str(), 0); | 
|---|
| 184 | //cout << name << " starting thread " << &thread << endl; | 
|---|
| 185 | pthread_create(&thread, NULL, ThreadStart, this); | 
|---|
| 186 | TOIManager::getManager()->addThread(&thread); | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | #ifndef NO_SOPHYA | 
|---|
| 190 | Array TOIProcessor::getData(int toiIndex, int iStart, int iEnd) { | 
|---|
| 191 | TOI* toi = inTOIs[toiIndex]; | 
|---|
| 192 | toi->waitForData(iStart, iEnd); | 
|---|
| 193 | return toi->getData(iStart, iEnd); | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | Array TOIProcessor::getError(int toiIndex, int iStart, int iEnd) { | 
|---|
| 197 | TOI* toi = inTOIs[toiIndex]; | 
|---|
| 198 | toi->waitForData(iStart, iEnd); | 
|---|
| 199 | return toi->getError(iStart, iEnd); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | TArray<int_4> TOIProcessor::getFlag(int toiIndex, int iStart, int iEnd) { | 
|---|
| 203 | TOI* toi = inTOIs[toiIndex]; | 
|---|
| 204 | toi->waitForData(iStart, iEnd); | 
|---|
| 205 | return toi->getFlag(iStart, iEnd); | 
|---|
| 206 | } | 
|---|
| 207 | #endif | 
|---|
| 208 |  | 
|---|
| 209 | double TOIProcessor::getData(int toiIndex, int i) { | 
|---|
| 210 | TOI* toi = inTOIs[toiIndex]; | 
|---|
| 211 | toi->waitForData(i); | 
|---|
| 212 | return toi->getData(i); | 
|---|
| 213 | } | 
|---|
| 214 |  | 
|---|
| 215 | double TOIProcessor::getError(int toiIndex, int i) { | 
|---|
| 216 | TOI* toi = inTOIs[toiIndex]; | 
|---|
| 217 | toi->waitForData(i); | 
|---|
| 218 | return toi->getError(i); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | int_4 TOIProcessor::getFlag(int toiIndex, int i) { | 
|---|
| 222 | TOI* toi = inTOIs[toiIndex]; | 
|---|
| 223 | toi->waitForData(i); | 
|---|
| 224 | return toi->getFlag(i); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | void TOIProcessor::setNeededHistory(int nsamples) { | 
|---|
| 228 | neededHistory =  nsamples; | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | void TOIProcessor::wontNeedBefore(int i) { | 
|---|
| 232 | if (i<wontNeedValue) return; | 
|---|
| 233 | wontNeedValue = i; | 
|---|
| 234 | for (int j=0; j< (int) inIx.size();  j++) { | 
|---|
| 235 | inTOIs[j]->wontNeedBefore(i); | 
|---|
| 236 | } | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | void TOIProcessor::autoWontNeed(int iCur) { | 
|---|
| 240 | if (neededHistory <=0) return; | 
|---|
| 241 | if (iCur < lastAWN + neededHistory) return; | 
|---|
| 242 | lastAWN = iCur; | 
|---|
| 243 | //cout << name << " wontNeedBefore "  << iCur-neededHistory << endl; | 
|---|
| 244 | wontNeedBefore(iCur-neededHistory); | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | void TOIProcessor::notify() { | 
|---|
| 248 | lock(); | 
|---|
| 249 | /*  if (mutex.__m_lock.__status == 0) { | 
|---|
| 250 | cout << "wait without lock" << endl; abort(); | 
|---|
| 251 | }*/ | 
|---|
| 252 |  | 
|---|
| 253 | pthread_cond_broadcast(&dataReady); | 
|---|
| 254 | unlock(); | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 |  | 
|---|
| 258 | void TOIProcessor::putData(int toiIndex, int i, double value, int_4 flg) { | 
|---|
| 259 | TOI* toi = outTOIs[toiIndex]; | 
|---|
| 260 | toi->putData(i, value, flg); | 
|---|
| 261 | autoWontNeed(i); | 
|---|
| 262 | notify(); | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 |  | 
|---|
| 266 | void TOIProcessor::putDataError(int toiIndex, int i, double value, | 
|---|
| 267 | double error, int_4 flg) { | 
|---|
| 268 | TOI* toi = outTOIs[toiIndex]; | 
|---|
| 269 | toi->putDataError(i, value, error, flg); | 
|---|
| 270 | autoWontNeed(i); | 
|---|
| 271 | notify(); | 
|---|
| 272 | } | 
|---|
| 273 |  | 
|---|
| 274 |  | 
|---|