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

Last change on this file since 1439 was 1439, checked in by ansari, 25 years ago

Correction bugs, protections, ameliorations - Reza 13/3/2001

File size: 9.0 KB
Line 
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
14TOIProcessor::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
30TOIProcessor::~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
41void TOIProcessor::init() {
42 //cout << "TOIProcessor::init" << endl;
43}
44
45void 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
55int TOIProcessor::getMinOut() {
56 //cout << name << "minout" << endl;
57 if (minOut < 0) minOut = calcMinOut();
58 //cout << name << "minout=" << minOut << endl;
59 return minOut;
60}
61
62int TOIProcessor::getMaxOut() {
63 //cout << name << "maxout" << endl;
64 if (maxOut < 0) maxOut = calcMaxOut();
65 //cout << name << "maxout=" << maxOut << endl;
66 return maxOut;
67}
68
69int TOIProcessor::calcMinOut() {
70 return getMinIn() + lowExtra;
71}
72
73int TOIProcessor::calcMaxOut() {
74 return getMaxIn() - upExtra;
75}
76
77int 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
89int 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
102int 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
110int 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
118int 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
125int 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
133TOI* 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
143TOI* 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
153bool TOIProcessor::checkInputTOIIndex(int toiIndex) {
154 if (toiIndex >= inIx.size()) return false;
155 if (inTOIs[toiIndex] == NULL) return false;
156 return true;
157}
158
159bool TOIProcessor::checkOutputTOIIndex(int toiIndex) {
160 if (toiIndex >= outIx.size()) return false;
161 if (outTOIs[toiIndex] == NULL) return false;
162 return true;
163}
164
165void 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
191void 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
200void 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
209string 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
219string 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
229void TOIProcessor::run() {
230
231}
232
233void* 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
248void 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
261Array TOIProcessor::getData(int toiIndex, int iStart, int iEnd) {
262 TOI* toi = getInputTOI(toiIndex);
263 toi->waitForData(iStart, iEnd);
264 return toi->getData(iStart, iEnd);
265}
266
267Array TOIProcessor::getError(int toiIndex, int iStart, int iEnd) {
268 TOI* toi = getInputTOI(toiIndex);
269 toi->waitForData(iStart, iEnd);
270 return toi->getError(iStart, iEnd);
271}
272
273TArray<int_4> TOIProcessor::getFlag(int toiIndex, int iStart, int iEnd) {
274 TOI* toi = getInputTOI(toiIndex);
275 toi->waitForData(iStart, iEnd);
276 return toi->getFlag(iStart, iEnd);
277}
278#endif
279
280double TOIProcessor::getData(int toiIndex, int i) {
281 TOI* toi = getInputTOI(toiIndex);
282 toi->waitForData(i);
283 return toi->getData(i);
284}
285
286double TOIProcessor::getError(int toiIndex, int i) {
287 TOI* toi = getInputTOI(toiIndex);
288 toi->waitForData(i);
289 return toi->getError(i);
290}
291
292int_4 TOIProcessor::getFlag(int toiIndex, int i) {
293 TOI* toi = getInputTOI(toiIndex);
294 toi->waitForData(i);
295 return toi->getFlag(i);
296}
297
298void TOIProcessor::setNeededHistory(int nsamples) {
299 neededHistory = nsamples;
300}
301
302void TOIProcessor::wontNeedBefore(int i) {
303 if (i<wontNeedValue) return;
304 wontNeedValue = i;
305 for (int j=0; j< (int) inIx.size(); j++) {
306 inTOIs[j]->wontNeedBefore(i);
307 }
308}
309
310void TOIProcessor::autoWontNeed(int iCur) {
311 if (neededHistory <=0) return;
312 if (iCur < lastAWN + neededHistory) return;
313 lastAWN = iCur;
314 //cout << name << " wontNeedBefore " << iCur-neededHistory << endl;
315 wontNeedBefore(iCur-neededHistory);
316}
317
318void TOIProcessor::notify() {
319 lock();
320/* if (mutex.__m_lock.__status == 0) {
321 cout << "wait without lock" << endl; abort();
322 }*/
323
324 pthread_cond_broadcast(&dataReady);
325 unlock();
326}
327
328
329void TOIProcessor::putData(int toiIndex, int i, double value, int_4 flg) {
330 TOI* toi = getOutputTOI(toiIndex);
331 toi->putData(i, value, flg);
332 autoWontNeed(i);
333 notify();
334}
335
336
337void TOIProcessor::putDataError(int toiIndex, int i, double value,
338 double error, int_4 flg) {
339 TOI* toi = getOutputTOI(toiIndex);
340 if (toi == NULL)
341 throw NullPtrError("TOIProcessor::putDataError() - Not assigned TOI !");
342 toi->putDataError(i, value, error, flg);
343 autoWontNeed(i);
344 notify();
345}
346
347
Note: See TracBrowser for help on using the repository browser.