1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
2 | // Eric Aubourg
|
---|
3 | // Christophe Magneville
|
---|
4 | // Reza Ansari
|
---|
5 | // $Id: toiprocessor.cc,v 1.23 2002-05-30 07:57:04 ansari Exp $
|
---|
6 |
|
---|
7 | #include "toiprocessor.h"
|
---|
8 | #include "toimanager.h"
|
---|
9 | #include <pthread.h>
|
---|
10 |
|
---|
11 | #ifdef HAVE_VALUES_H
|
---|
12 | #include <values.h>
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #ifndef MAXINT
|
---|
16 | #define MAXINT 2147483647
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #ifdef HAVE_STDINT_H
|
---|
20 | #include <stdint.h>
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #ifdef WITH_SOPHYA
|
---|
24 | #include "pexceptions.h"
|
---|
25 | #else
|
---|
26 | #include "apexceptions.h"
|
---|
27 | #endif
|
---|
28 |
|
---|
29 | #define pthread_mutexattr_setkind_np pthread_mutexattr_settype
|
---|
30 |
|
---|
31 | TOIProcessor::TOIProcessor() {
|
---|
32 | //cout << "TOIProcessor::TOIProcessor" << endl;
|
---|
33 | outTOIs = NULL;
|
---|
34 | inTOIs = NULL;
|
---|
35 | inited=false;
|
---|
36 |
|
---|
37 | upExtra = 0;
|
---|
38 | lowExtra = 0;
|
---|
39 | minOut = -1;
|
---|
40 | maxOut = -1;
|
---|
41 | forcedMinIn = -1;
|
---|
42 | forcedMaxIn = -1;
|
---|
43 | neededHistory = 1000;
|
---|
44 | lastAWN = 0;
|
---|
45 | wontNeedValue = -1;
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | TOIProcessor::~TOIProcessor() {
|
---|
50 | delete[] outTOIs;
|
---|
51 | delete[] inTOIs;
|
---|
52 | //if (mutex)
|
---|
53 | pthread_mutex_destroy(&mutex);
|
---|
54 | //if (dataReady)
|
---|
55 | pthread_cond_destroy(&dataReady);
|
---|
56 | // Il ne faut apparemment pas appeler pthread_detach si on a fait join avant
|
---|
57 | // Extrait du man (Reza - Mai 2002)
|
---|
58 | //man: The pthread_join(3) routine also detaches the target thread after
|
---|
59 | //man: pthread_join(3) returns successfully.
|
---|
60 | // pthread_detach(thread); $CHECK$ - Reza Mai 2002
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | void TOIProcessor::init() {
|
---|
65 | //cout << "TOIProcessor::init" << endl;
|
---|
66 | }
|
---|
67 |
|
---|
68 | void TOIProcessor::afterinit() {
|
---|
69 | int i;
|
---|
70 | inTOIs = new (TOI*[inIx.size()]);
|
---|
71 | for(i=0; i<inIx.size(); i++)
|
---|
72 | inTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
73 | outTOIs = new (TOI*[outIx.size()]);
|
---|
74 | for(i=0; i<outIx.size(); i++)
|
---|
75 | outTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
76 | }
|
---|
77 |
|
---|
78 | int TOIProcessor::getMinOut() {
|
---|
79 | //cout << name << "minout" << endl;
|
---|
80 | if (minOut < 0) minOut = calcMinOut();
|
---|
81 | //cout << name << "minout=" << minOut << endl;
|
---|
82 | return minOut;
|
---|
83 | }
|
---|
84 |
|
---|
85 | int TOIProcessor::getMaxOut() {
|
---|
86 | //cout << name << "maxout" << endl;
|
---|
87 | if (maxOut < 0) maxOut = calcMaxOut();
|
---|
88 | //cout << name << "maxout=" << maxOut << endl;
|
---|
89 | return maxOut;
|
---|
90 | }
|
---|
91 |
|
---|
92 | int TOIProcessor::calcMinOut() {
|
---|
93 | return getMinIn() + lowExtra;
|
---|
94 | }
|
---|
95 |
|
---|
96 | int TOIProcessor::calcMaxOut() {
|
---|
97 | return getMaxIn() - upExtra;
|
---|
98 | }
|
---|
99 |
|
---|
100 | int TOIProcessor::getMinIn() {
|
---|
101 | int nIn = inIx.size();
|
---|
102 | int minIn = 0;
|
---|
103 | for (int i=0; i<nIn; i++) {
|
---|
104 | TOI* toi = inTOIs[i];
|
---|
105 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
106 | int x = toi->getMinSn();
|
---|
107 | if (x > minIn) minIn = x;
|
---|
108 | }
|
---|
109 | if (forcedMinIn > 0 && forcedMinIn > minIn) minIn = forcedMinIn;
|
---|
110 | return minIn;
|
---|
111 | }
|
---|
112 |
|
---|
113 | int TOIProcessor::getMaxIn() {
|
---|
114 | int_4 nIn = inIx.size();
|
---|
115 | int_4 maxIn = MAXINT;
|
---|
116 | for (int i=0; i<nIn; i++) {
|
---|
117 | TOI* toi = inTOIs[i];
|
---|
118 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
119 | int_4 x = toi->getMaxSn();
|
---|
120 | if (x < maxIn) maxIn = x;
|
---|
121 | }
|
---|
122 | if (forcedMaxIn > 0 && forcedMaxIn < maxIn) maxIn = forcedMaxIn;
|
---|
123 | return maxIn;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | int TOIProcessor::declareInput(string toi) {
|
---|
128 | if (inIx.find(toi) != inIx.end())
|
---|
129 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared");
|
---|
130 | int i = inIx.size();
|
---|
131 | inIx[toi] = i;
|
---|
132 | return i;
|
---|
133 | }
|
---|
134 |
|
---|
135 | int TOIProcessor::declareOutput(string toi) {
|
---|
136 | if (outIx.find(toi) != outIx.end())
|
---|
137 | throw DuplicateIdExc("TOIProcessor::declareInput : "+toi+" already declared");
|
---|
138 | int i = outIx.size();
|
---|
139 | outIx[toi] = i;
|
---|
140 | return i;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int TOIProcessor::getInputTOIIndex(string toi) {
|
---|
144 | chkinit();
|
---|
145 | map<string, int>::iterator i = inIx.find(toi);
|
---|
146 | if (i == inIx.end()) return -1;
|
---|
147 | return (*i).second;
|
---|
148 | }
|
---|
149 |
|
---|
150 | int TOIProcessor::getOutputTOIIndex(string toi) {
|
---|
151 | chkinit();
|
---|
152 | map<string, int>::iterator i = outIx.find(toi);
|
---|
153 | if (i == outIx.end()) return -1;
|
---|
154 | return (*i).second;
|
---|
155 | }
|
---|
156 |
|
---|
157 | // Methodes rajoutees par Reza 11/3/2001
|
---|
158 | TOI* TOIProcessor::getInputTOI(int toiIndex) {
|
---|
159 | // chkinit();
|
---|
160 | if (toiIndex >= inIx.size())
|
---|
161 | throw RangeCheckError("TOIProcessor::getInputTOI() out of bound toiIndex");
|
---|
162 | TOI* toi = inTOIs[toiIndex];
|
---|
163 | if (toi == NULL)
|
---|
164 | throw NullPtrError("TOIProcessor::getInputTOI() - Not assigned TOI !");
|
---|
165 | return(toi);
|
---|
166 | }
|
---|
167 |
|
---|
168 | TOI* TOIProcessor::getOutputTOI(int toiIndex) {
|
---|
169 | // chkinit();
|
---|
170 | if (toiIndex >= outIx.size())
|
---|
171 | throw RangeCheckError("TOIProcessor::getOutputTOI() out of bound toiIndex");
|
---|
172 | TOI* toi = outTOIs[toiIndex];
|
---|
173 | // if (toi == NULL)
|
---|
174 | // throw NullPtrError("TOIProcessor::getOutputTOI() - Not assigned TOI !");
|
---|
175 | return(toi);
|
---|
176 | }
|
---|
177 |
|
---|
178 | bool TOIProcessor::checkInputTOIIndex(int toiIndex) {
|
---|
179 | if (toiIndex >= inIx.size()) return false;
|
---|
180 | if (inTOIs[toiIndex] == NULL) return false;
|
---|
181 | return true;
|
---|
182 | }
|
---|
183 |
|
---|
184 | bool TOIProcessor::checkOutputTOIIndex(int toiIndex) {
|
---|
185 | if (toiIndex >= outIx.size()) return false;
|
---|
186 | if (outTOIs[toiIndex] == NULL) return false;
|
---|
187 | return true;
|
---|
188 | }
|
---|
189 |
|
---|
190 | void TOIProcessor::PrintStatus(::ostream & os)
|
---|
191 | {
|
---|
192 | chkinit();
|
---|
193 | os << " TOIProcessor::PrintStatus() - Name= " << name
|
---|
194 | << " MinIn=" << getMinIn() << " MaxIn=" << getMaxIn() << endl;
|
---|
195 | os << " --- Inputs N= " << inIx.size() << endl;
|
---|
196 | int k;
|
---|
197 | for(k=0; k<inIx.size(); k++) {
|
---|
198 | os << "Input[" << k << "] : " << getInName(k) ;
|
---|
199 | if (inTOIs[k] != NULL)
|
---|
200 | os << " Connected TOI " << inTOIs[k]->getName() << endl;
|
---|
201 | else os << " NO TOI " << endl;
|
---|
202 | }
|
---|
203 | os << " --- Outputs N= " << outIx.size() << endl;
|
---|
204 | for(k=0; k<outIx.size(); k++) {
|
---|
205 | os << "Output[" << k << "] : " << getOutName(k) ;
|
---|
206 | if (outTOIs[k] != NULL)
|
---|
207 | os << " Connected TOI " << outTOIs[k]->getName() << endl;
|
---|
208 | else os << " NO TOI " << endl;
|
---|
209 | }
|
---|
210 | os << endl;
|
---|
211 | return;
|
---|
212 | }
|
---|
213 |
|
---|
214 | // Fin rajout Reza 11/3/2001
|
---|
215 |
|
---|
216 | void TOIProcessor::addInput(string name, TOI* toi) {
|
---|
217 | chkinit();
|
---|
218 | map<string, int>::iterator i = inIx.find(name);
|
---|
219 | if (i == inIx.end()) throw NotFoundExc("TOIProcessor::addInput "+
|
---|
220 | name+" not declared");
|
---|
221 | inTOIs[(*i).second] = toi;
|
---|
222 | toi->addConsumer(this); // $CHECK$ Reza 13/3/2001
|
---|
223 | }
|
---|
224 |
|
---|
225 | void TOIProcessor::addOutput(string name, TOI* toi) {
|
---|
226 | chkinit();
|
---|
227 | map<string, int>::iterator i = outIx.find(name);
|
---|
228 | if (i == outIx.end()) throw NotFoundExc("TOIProcessor::addOutput "+
|
---|
229 | name+" not declared");
|
---|
230 | toi->setProducer(this);
|
---|
231 | outTOIs[(*i).second] = toi;
|
---|
232 | }
|
---|
233 |
|
---|
234 | string TOIProcessor::getOutName(int i) {
|
---|
235 | if (i > outIx.size()) throw RangeCheckError("TOIProcessor::getOutName "
|
---|
236 | " out of bound");
|
---|
237 | map<string, int>::iterator j;
|
---|
238 | for(j=outIx.begin(); j!= outIx.end(); j++)
|
---|
239 | if ((*j).second == i) return (*j).first;
|
---|
240 |
|
---|
241 | throw RangeCheckError("TOIProcessor::getOutName Not found index !");
|
---|
242 | }
|
---|
243 |
|
---|
244 | string TOIProcessor::getInName(int i) {
|
---|
245 | if (i > inIx.size()) throw RangeCheckError("TOIProcessor::getInName "
|
---|
246 | " out of bound");
|
---|
247 | map<string, int>::iterator j;
|
---|
248 | for(j=inIx.begin(); j!= inIx.end(); j++)
|
---|
249 | if ((*j).second == i) return (*j).first;
|
---|
250 |
|
---|
251 | throw RangeCheckError("TOIProcessor::getOutName Not found index !");
|
---|
252 | }
|
---|
253 |
|
---|
254 | void TOIProcessor::run() {
|
---|
255 |
|
---|
256 | }
|
---|
257 |
|
---|
258 | void TOIProcessor::warnPutDone() {
|
---|
259 | int n = outIx.size();
|
---|
260 | for (int i=0; i<n; i++) {
|
---|
261 | TOI* toi = outTOIs[i];
|
---|
262 | if (toi) {
|
---|
263 | toi->putDone();
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | void* TOIProcessor::ThreadStart(void* arg) {
|
---|
269 | TOIProcessor* p = (TOIProcessor*) arg;
|
---|
270 | // cout << p->name << " new thread running " << pthread_self() << endl;
|
---|
271 | p->run();
|
---|
272 | p->warnPutDone();
|
---|
273 | pthread_exit(NULL);
|
---|
274 | // cout << p->name << " thread done " << pthread_self() << endl;
|
---|
275 | return NULL;
|
---|
276 | }
|
---|
277 |
|
---|
278 | #ifdef Linux
|
---|
279 | #define pthread_mutexattr_settype pthread_mutexattr_setkind_np
|
---|
280 | #define PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK_NP
|
---|
281 | #define pthread_mutex_setname_np(a,b,c)
|
---|
282 | #define pthread_cond_setname_np(a,b,c)
|
---|
283 | #endif
|
---|
284 |
|
---|
285 | void TOIProcessor::start() {
|
---|
286 | pthread_cond_init(&dataReady, NULL);
|
---|
287 | pthread_mutexattr_init(&mutattr);
|
---|
288 | // pthread_mutexattr_settype(&mutattr, PTHREAD_MUTEX_ERRORCHECK);
|
---|
289 | pthread_mutex_init(&mutex, &mutattr);
|
---|
290 | //pthread_mutex_setname_np(&mutex, (name + "_proc_mutex").c_str(), 0);
|
---|
291 | //pthread_cond_setname_np(&dataReady, (name + "_proc_cond").c_str(), 0);
|
---|
292 | //cout << name << " starting thread " << &thread << endl;
|
---|
293 | pthread_create(&thread, NULL, ThreadStart, this);
|
---|
294 | TOIManager::getManager()->addThread(&thread);
|
---|
295 | }
|
---|
296 |
|
---|
297 | #ifndef NO_SOPHYA
|
---|
298 | /* ---- l'interface va etre modifiee, NE PAS UTILISER
|
---|
299 | Array TOIProcessor::getData(int toiIndex, int iStart, int iEnd) {
|
---|
300 | TOI* toi = getInputTOI(toiIndex);
|
---|
301 | toi->waitForData(iStart, iEnd);
|
---|
302 | return toi->getData(iStart, iEnd);
|
---|
303 | }
|
---|
304 |
|
---|
305 | Array TOIProcessor::getError(int toiIndex, int iStart, int iEnd) {
|
---|
306 | TOI* toi = getInputTOI(toiIndex);
|
---|
307 | toi->waitForData(iStart, iEnd);
|
---|
308 | return toi->getError(iStart, iEnd);
|
---|
309 | }
|
---|
310 |
|
---|
311 | TArray<int_4> TOIProcessor::getFlag(int toiIndex, int iStart, int iEnd) {
|
---|
312 | TOI* toi = getInputTOI(toiIndex);
|
---|
313 | toi->waitForData(iStart, iEnd);
|
---|
314 | return toi->getFlag(iStart, iEnd);
|
---|
315 | }
|
---|
316 | l'interface va etre modifiee, NE PAS UTILISER ---- */
|
---|
317 | #endif
|
---|
318 |
|
---|
319 | double TOIProcessor::getData(int toiIndex, int i) {
|
---|
320 | TOI* toi = getInputTOI(toiIndex);
|
---|
321 | if (toi->needSyncOldWay()) toi->waitForData(i); // seulement pour autre que segmented
|
---|
322 | autoWontNeed(i);
|
---|
323 | return toi->getData(i);
|
---|
324 | }
|
---|
325 |
|
---|
326 | void TOIProcessor::getData(int toiIndex, int i, double &data, uint_8 &flag)
|
---|
327 | {
|
---|
328 | TOI* toi = getInputTOI(toiIndex);
|
---|
329 | if (toi->needSyncOldWay()) toi->waitForData(i); // seulement pour autre que segmented
|
---|
330 | toi->getData(i, data, flag);
|
---|
331 | autoWontNeed(i);
|
---|
332 | return;
|
---|
333 | }
|
---|
334 |
|
---|
335 | void TOIProcessor::getData(int toiIndex, int i, int n, double* d)
|
---|
336 | {
|
---|
337 | TOI* toi = getInputTOI(toiIndex);
|
---|
338 | if (toi->needSyncOldWay()) toi->waitForData(i+n); // seulement pour autre que segmented
|
---|
339 | toi->getData(i, n, d);
|
---|
340 | autoWontNeed(i);
|
---|
341 | return;
|
---|
342 | }
|
---|
343 |
|
---|
344 | void TOIProcessor::getData(int toiIndex, int i, int n, double* d, uint_8* f)
|
---|
345 | {
|
---|
346 | TOI* toi = getInputTOI(toiIndex);
|
---|
347 | if (toi->needSyncOldWay()) toi->waitForData(i+n); // seulement pour autre que segmented
|
---|
348 | toi->getData(i, n, d, f);
|
---|
349 | autoWontNeed(i);
|
---|
350 | return;
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 | /*RZCMV
|
---|
355 | double TOIProcessor::getError(int toiIndex, int i) {
|
---|
356 | TOI* toi = getInputTOI(toiIndex);
|
---|
357 | toi->waitForData(i);
|
---|
358 | return toi->getError(i);
|
---|
359 | }
|
---|
360 |
|
---|
361 | int_4 TOIProcessor::getFlag(int toiIndex, int i) {
|
---|
362 | TOI* toi = getInputTOI(toiIndex);
|
---|
363 | toi->waitForData(i);
|
---|
364 | return toi->getFlag(i);
|
---|
365 | }
|
---|
366 | */
|
---|
367 |
|
---|
368 | void TOIProcessor::setNeededHistory(int nsamples) {
|
---|
369 | neededHistory = nsamples;
|
---|
370 | }
|
---|
371 |
|
---|
372 | void TOIProcessor::wontNeedBefore(int i) {
|
---|
373 | if (i<wontNeedValue) return;
|
---|
374 | wontNeedValue = i;
|
---|
375 | for (int j=0; j< (int) inIx.size(); j++) {
|
---|
376 | // $CHECK$ Reza 6/5/2001 Protection sur non connected TOI
|
---|
377 | if (inTOIs[j]) inTOIs[j]->wontNeedBefore(i);
|
---|
378 | }
|
---|
379 | }
|
---|
380 |
|
---|
381 | void TOIProcessor::autoWontNeed(int iCur) {
|
---|
382 | if (neededHistory <=0) return;
|
---|
383 | if (iCur < lastAWN + neededHistory/10) return;
|
---|
384 | lastAWN = iCur;
|
---|
385 | // cout << name << " wontNeedBefore " << iCur-neededHistory << endl;
|
---|
386 | wontNeedBefore(iCur-neededHistory);
|
---|
387 | }
|
---|
388 |
|
---|
389 | void TOIProcessor::notify() {
|
---|
390 | lock();
|
---|
391 | pthread_cond_broadcast(&dataReady);
|
---|
392 | unlock();
|
---|
393 | }
|
---|
394 |
|
---|
395 |
|
---|
396 | void TOIProcessor::putData(int toiIndex, int i, double value, uint_8 flg) {
|
---|
397 | TOI* toi = getOutputTOI(toiIndex);
|
---|
398 | if (toi == NULL) return;
|
---|
399 | toi->putData(i, value, flg);
|
---|
400 | // autoWontNeed(i); // now done on getData
|
---|
401 | if (toi->needSyncOldWay()) notify(); // seulement pour non segmented
|
---|
402 | }
|
---|
403 |
|
---|
404 | void TOIProcessor::putData(int toiIndex, int i, int n, double const* val,
|
---|
405 | uint_8 const* flg) {
|
---|
406 | TOI* toi = getOutputTOI(toiIndex);
|
---|
407 | if (toi == NULL) return;
|
---|
408 | toi->putData(i, n, val, flg);
|
---|
409 | if (toi->needSyncOldWay()) notify(); // seulement pour non segmented
|
---|
410 | }
|
---|
411 |
|
---|
412 | /*RZCMV
|
---|
413 | void TOIProcessor::putDataError(int toiIndex, int i, double value,
|
---|
414 | double error, int_4 flg) {
|
---|
415 | TOI* toi = getOutputTOI(toiIndex);
|
---|
416 | if (toi == NULL)
|
---|
417 | throw NullPtrError("TOIProcessor::putDataError() - Not assigned TOI !");
|
---|
418 | toi->putDataError(i, value, error, flg);
|
---|
419 | autoWontNeed(i);
|
---|
420 | notify();
|
---|
421 | }
|
---|
422 | */
|
---|
423 |
|
---|