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 | int i;
|
---|
47 | inTOIs = new (TOI*[inIx.size()]);
|
---|
48 | for(i=0; i<inIx.size(); i++)
|
---|
49 | inTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
50 | outTOIs = new (TOI*[outIx.size()]);
|
---|
51 | for(i=0; i<outIx.size(); i++)
|
---|
52 | outTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
|
---|
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];
|
---|
82 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
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];
|
---|
94 | if (toi == NULL) continue; // Protection - Reza 13/3/2001
|
---|
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) {
|
---|
119 | chkinit();
|
---|
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) {
|
---|
126 | chkinit();
|
---|
127 | map<string, int>::iterator i = outIx.find(toi);
|
---|
128 | if (i == outIx.end()) return -1;
|
---|
129 | return (*i).second;
|
---|
130 | }
|
---|
131 |
|
---|
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 | {
|
---|
167 | chkinit();
|
---|
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 |
|
---|
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");
|
---|
196 | inTOIs[(*i).second] = toi;
|
---|
197 | toi->addConsumer(this); // $CHECK$ Reza 13/3/2001
|
---|
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 |
|
---|
209 | string TOIProcessor::getOutName(int i) {
|
---|
210 | if (i > outIx.size()) throw RangeCheckError("TOIProcessor::getOutName "
|
---|
211 | " out of bound");
|
---|
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 !");
|
---|
217 | }
|
---|
218 |
|
---|
219 | string TOIProcessor::getInName(int i) {
|
---|
220 | if (i > inIx.size()) throw RangeCheckError("TOIProcessor::getInName "
|
---|
221 | " out of bound");
|
---|
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 !");
|
---|
227 | }
|
---|
228 |
|
---|
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 | /* ---- l'interface va etre modifiee, NE PAS UTILISER
|
---|
262 | Array TOIProcessor::getData(int toiIndex, int iStart, int iEnd) {
|
---|
263 | TOI* toi = getInputTOI(toiIndex);
|
---|
264 | toi->waitForData(iStart, iEnd);
|
---|
265 | return toi->getData(iStart, iEnd);
|
---|
266 | }
|
---|
267 |
|
---|
268 | Array TOIProcessor::getError(int toiIndex, int iStart, int iEnd) {
|
---|
269 | TOI* toi = getInputTOI(toiIndex);
|
---|
270 | toi->waitForData(iStart, iEnd);
|
---|
271 | return toi->getError(iStart, iEnd);
|
---|
272 | }
|
---|
273 |
|
---|
274 | TArray<int_4> TOIProcessor::getFlag(int toiIndex, int iStart, int iEnd) {
|
---|
275 | TOI* toi = getInputTOI(toiIndex);
|
---|
276 | toi->waitForData(iStart, iEnd);
|
---|
277 | return toi->getFlag(iStart, iEnd);
|
---|
278 | }
|
---|
279 | l'interface va etre modifiee, NE PAS UTILISER ---- */
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | double TOIProcessor::getData(int toiIndex, int i) {
|
---|
283 | TOI* toi = getInputTOI(toiIndex);
|
---|
284 | toi->waitForData(i);
|
---|
285 | return toi->getData(i);
|
---|
286 | }
|
---|
287 |
|
---|
288 | void TOIProcessor::getData(int toiIndex, int i, double &data,int_8 &flag)
|
---|
289 | {
|
---|
290 | TOI* toi = getInputTOI(toiIndex);
|
---|
291 | toi->waitForData(i);
|
---|
292 | toi->getData(i, data, flag);
|
---|
293 | return;
|
---|
294 | }
|
---|
295 |
|
---|
296 | /*RZCMV
|
---|
297 | double TOIProcessor::getError(int toiIndex, int i) {
|
---|
298 | TOI* toi = getInputTOI(toiIndex);
|
---|
299 | toi->waitForData(i);
|
---|
300 | return toi->getError(i);
|
---|
301 | }
|
---|
302 |
|
---|
303 | int_4 TOIProcessor::getFlag(int toiIndex, int i) {
|
---|
304 | TOI* toi = getInputTOI(toiIndex);
|
---|
305 | toi->waitForData(i);
|
---|
306 | return toi->getFlag(i);
|
---|
307 | }
|
---|
308 | */
|
---|
309 |
|
---|
310 | void TOIProcessor::setNeededHistory(int nsamples) {
|
---|
311 | neededHistory = nsamples;
|
---|
312 | }
|
---|
313 |
|
---|
314 | void TOIProcessor::wontNeedBefore(int i) {
|
---|
315 | if (i<wontNeedValue) return;
|
---|
316 | wontNeedValue = i;
|
---|
317 | for (int j=0; j< (int) inIx.size(); j++) {
|
---|
318 | inTOIs[j]->wontNeedBefore(i);
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | void TOIProcessor::autoWontNeed(int iCur) {
|
---|
323 | if (neededHistory <=0) return;
|
---|
324 | if (iCur < lastAWN + neededHistory) return;
|
---|
325 | lastAWN = iCur;
|
---|
326 | //cout << name << " wontNeedBefore " << iCur-neededHistory << endl;
|
---|
327 | wontNeedBefore(iCur-neededHistory);
|
---|
328 | }
|
---|
329 |
|
---|
330 | void TOIProcessor::notify() {
|
---|
331 | lock();
|
---|
332 | /* if (mutex.__m_lock.__status == 0) {
|
---|
333 | cout << "wait without lock" << endl; abort();
|
---|
334 | }*/
|
---|
335 |
|
---|
336 | pthread_cond_broadcast(&dataReady);
|
---|
337 | unlock();
|
---|
338 | }
|
---|
339 |
|
---|
340 |
|
---|
341 | void TOIProcessor::putData(int toiIndex, int i, double value, int_8 flg) {
|
---|
342 | TOI* toi = getOutputTOI(toiIndex);
|
---|
343 | toi->putData(i, value, flg);
|
---|
344 | autoWontNeed(i);
|
---|
345 | notify();
|
---|
346 | }
|
---|
347 |
|
---|
348 | /*RZCMV
|
---|
349 | void TOIProcessor::putDataError(int toiIndex, int i, double value,
|
---|
350 | double error, int_4 flg) {
|
---|
351 | TOI* toi = getOutputTOI(toiIndex);
|
---|
352 | if (toi == NULL)
|
---|
353 | throw NullPtrError("TOIProcessor::putDataError() - Not assigned TOI !");
|
---|
354 | toi->putDataError(i, value, error, flg);
|
---|
355 | autoWontNeed(i);
|
---|
356 | notify();
|
---|
357 | }
|
---|
358 | */
|
---|
359 |
|
---|