source: Sophya/trunk/ArchTOIPipe/Kernel/toiprocessor.cc@ 1687

Last change on this file since 1687 was 1663, checked in by aubourg, 24 years ago

darwin

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