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

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

segmented buffers...

File size: 9.7 KB
Line 
1#include "toiprocessor.h"
2#include "toimanager.h"
3#include <pthread.h>
4
5#ifdef HAVE_VALUES_H
6#include <values.h>
7#endif
8
9#ifndef MAXINT
10#define MAXINT 2147483647
11#endif
12
13#ifdef HAVE_STDINT_H
14#include <stdint.h>
15#endif
16
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() {
57 int i;
58 inTOIs = new (TOI*[inIx.size()]);
59 for(i=0; i<inIx.size(); i++)
60 inTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
61 outTOIs = new (TOI*[outIx.size()]);
62 for(i=0; i<outIx.size(); i++)
63 outTOIs[i] = NULL; // Protection-Initialisation - Reza 11/3/2001
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];
93 if (toi == NULL) continue; // Protection - Reza 13/3/2001
94 int x = toi->getMinSn();
95 if (x > minIn) minIn = x;
96 }
97 return minIn;
98}
99
100int TOIProcessor::getMaxIn() {
101 int_4 nIn = inIx.size();
102 int_4 maxIn = MAXINT;
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_4 x = toi->getMaxSn();
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) {
130 chkinit();
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) {
137 chkinit();
138 map<string, int>::iterator i = outIx.find(toi);
139 if (i == outIx.end()) return -1;
140 return (*i).second;
141}
142
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{
178 chkinit();
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
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");
207 inTOIs[(*i).second] = toi;
208 toi->addConsumer(this); // $CHECK$ Reza 13/3/2001
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
220string TOIProcessor::getOutName(int i) {
221 if (i > outIx.size()) throw RangeCheckError("TOIProcessor::getOutName "
222 " out of bound");
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 !");
228}
229
230string TOIProcessor::getInName(int i) {
231 if (i > inIx.size()) throw RangeCheckError("TOIProcessor::getInName "
232 " out of bound");
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 !");
238}
239
240void TOIProcessor::run() {
241
242}
243
244void TOIProcessor::warnPutDone() {
245 int n = outIx.size();
246 for (int i=0; i<n; i++) {
247 TOI* toi = outTOIs[i];
248 toi->putDone();
249 }
250}
251
252void* TOIProcessor::ThreadStart(void* arg) {
253 TOIProcessor* p = (TOIProcessor*) arg;
254 // cout << p->name << " new thread running " << pthread_self() << endl;
255 p->run();
256 p->warnPutDone();
257 pthread_exit(NULL);
258 // cout << p->name << " thread done " << pthread_self() << endl;
259 return NULL;
260}
261
262#ifdef Linux
263#define pthread_mutexattr_settype pthread_mutexattr_setkind_np
264#define PTHREAD_MUTEX_ERRORCHECK PTHREAD_MUTEX_ERRORCHECK_NP
265#define pthread_mutex_setname_np(a,b,c)
266#define pthread_cond_setname_np(a,b,c)
267#endif
268
269void TOIProcessor::start() {
270 pthread_cond_init(&dataReady, NULL);
271 pthread_mutexattr_init(&mutattr);
272 // pthread_mutexattr_settype(&mutattr, PTHREAD_MUTEX_ERRORCHECK);
273 pthread_mutex_init(&mutex, &mutattr);
274 //pthread_mutex_setname_np(&mutex, (name + "_proc_mutex").c_str(), 0);
275 //pthread_cond_setname_np(&dataReady, (name + "_proc_cond").c_str(), 0);
276 //cout << name << " starting thread " << &thread << endl;
277 pthread_create(&thread, NULL, ThreadStart, this);
278 TOIManager::getManager()->addThread(&thread);
279}
280
281#ifndef NO_SOPHYA
282/* ---- l'interface va etre modifiee, NE PAS UTILISER
283Array TOIProcessor::getData(int toiIndex, int iStart, int iEnd) {
284 TOI* toi = getInputTOI(toiIndex);
285 toi->waitForData(iStart, iEnd);
286 return toi->getData(iStart, iEnd);
287}
288
289Array TOIProcessor::getError(int toiIndex, int iStart, int iEnd) {
290 TOI* toi = getInputTOI(toiIndex);
291 toi->waitForData(iStart, iEnd);
292 return toi->getError(iStart, iEnd);
293}
294
295TArray<int_4> TOIProcessor::getFlag(int toiIndex, int iStart, int iEnd) {
296 TOI* toi = getInputTOI(toiIndex);
297 toi->waitForData(iStart, iEnd);
298 return toi->getFlag(iStart, iEnd);
299}
300 l'interface va etre modifiee, NE PAS UTILISER ---- */
301#endif
302
303double TOIProcessor::getData(int toiIndex, int i) {
304 TOI* toi = getInputTOI(toiIndex);
305 toi->waitForData(i);
306 return toi->getData(i);
307}
308
309void TOIProcessor::getData(int toiIndex, int i, double &data, uint_8 &flag)
310{
311 TOI* toi = getInputTOI(toiIndex);
312 toi->waitForData(i);
313 toi->getData(i, data, flag);
314 return;
315}
316
317/*RZCMV
318double TOIProcessor::getError(int toiIndex, int i) {
319 TOI* toi = getInputTOI(toiIndex);
320 toi->waitForData(i);
321 return toi->getError(i);
322}
323
324int_4 TOIProcessor::getFlag(int toiIndex, int i) {
325 TOI* toi = getInputTOI(toiIndex);
326 toi->waitForData(i);
327 return toi->getFlag(i);
328}
329*/
330
331void TOIProcessor::setNeededHistory(int nsamples) {
332 neededHistory = nsamples;
333}
334
335void TOIProcessor::wontNeedBefore(int i) {
336 if (i<wontNeedValue) return;
337 wontNeedValue = i;
338 for (int j=0; j< (int) inIx.size(); j++) {
339 // $CHECK$ Reza 6/5/2001 Protection sur non connected TOI
340 if (inTOIs[j]) inTOIs[j]->wontNeedBefore(i);
341 }
342}
343
344void TOIProcessor::autoWontNeed(int iCur) {
345 if (neededHistory <=0) return;
346 if (iCur < lastAWN + neededHistory) return;
347 lastAWN = iCur;
348 //cout << name << " wontNeedBefore " << iCur-neededHistory << endl;
349 wontNeedBefore(iCur-neededHistory);
350}
351
352void TOIProcessor::notify() {
353 lock();
354/* if (mutex.__m_lock.__status == 0) {
355 cout << "wait without lock" << endl; abort();
356 }*/
357
358 pthread_cond_broadcast(&dataReady);
359 unlock();
360}
361
362
363void TOIProcessor::putData(int toiIndex, int i, double value, uint_8 flg) {
364 TOI* toi = getOutputTOI(toiIndex);
365 toi->putData(i, value, flg);
366 autoWontNeed(i);
367 notify();
368}
369
370/*RZCMV
371void TOIProcessor::putDataError(int toiIndex, int i, double value,
372 double error, int_4 flg) {
373 TOI* toi = getOutputTOI(toiIndex);
374 if (toi == NULL)
375 throw NullPtrError("TOIProcessor::putDataError() - Not assigned TOI !");
376 toi->putDataError(i, value, error, flg);
377 autoWontNeed(i);
378 notify();
379}
380*/
381
Note: See TracBrowser for help on using the repository browser.