1 | #include "toiprocessor.h"
|
---|
2 | #include "toi.h"
|
---|
3 | #include <pthread.h>
|
---|
4 |
|
---|
5 | #ifdef WITH_SOPHYA
|
---|
6 | #include "pexceptions.h"
|
---|
7 | #else
|
---|
8 | #include "apexceptions.h"
|
---|
9 | #endif
|
---|
10 |
|
---|
11 | #define CHKSYNC(ctx)
|
---|
12 | // if (((TOIRegularWindow*)this)->data.size() != ((TOIRegularWindow*)this)->flags.size()) \
|
---|
13 | // cout << ctx << ((TOIRegularWindow*)this)->data.size() << " " << \
|
---|
14 | // ((TOIRegularWindow*)this)->flags.size() << endl; \
|
---|
15 |
|
---|
16 | TOI::TOI() {
|
---|
17 | TOIInit();
|
---|
18 | }
|
---|
19 |
|
---|
20 | TOI::TOI(string n) {
|
---|
21 | name = n;
|
---|
22 | TOIInit();
|
---|
23 | }
|
---|
24 |
|
---|
25 | void TOI::TOIInit() {
|
---|
26 | pthread_mutex_init(&mutex, NULL);
|
---|
27 | // ----- Rajouts Reza 12/3/2001
|
---|
28 | pthread_cond_init(&condv, NULL);
|
---|
29 | fgwaitput = fgwaitget = false;
|
---|
30 | fgsigput = fgsigget = false;
|
---|
31 | countwaitput = countwaitget = 0;
|
---|
32 | // Fin rajouts Reza 12/3/2001 ------
|
---|
33 | // pthread_mutex_setname_np(&mutex, (name + "_toi_mutex").c_str(), 0);
|
---|
34 | defaultValue = 0;
|
---|
35 | producer = NULL;
|
---|
36 | dbg = false;
|
---|
37 | }
|
---|
38 |
|
---|
39 | TOI::~TOI() {
|
---|
40 | pthread_mutex_destroy(&mutex);
|
---|
41 | }
|
---|
42 |
|
---|
43 | void TOI::PrintStatus(ostream & os) const
|
---|
44 | {
|
---|
45 | os << "TOI::PrintStatus() - Name=" << getName() << endl;
|
---|
46 | os << " WaitStatus: Put/" ;
|
---|
47 | if (isPutWaiting()) os << "Waiting " ;
|
---|
48 | else os << "Running ";
|
---|
49 | os << " PutCountWait= " << getCountWaitPut() << endl;
|
---|
50 | os << " WaitStatus: Get/" ;
|
---|
51 | if (isGetWaiting()) os << "Waiting " ;
|
---|
52 | else os << "Running ";
|
---|
53 | os << " GetCountWait= " << getCountWaitGet() << endl;
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | void TOI::setProducer(TOIProcessor* p) {
|
---|
58 | if (producer)
|
---|
59 | throw DuplicateIdExc("TOI::setProducer : producer already defined");
|
---|
60 | producer = p;
|
---|
61 | }
|
---|
62 |
|
---|
63 | void TOI::addConsumer(TOIProcessor* p) {
|
---|
64 | consumers.push_back(p);
|
---|
65 | }
|
---|
66 |
|
---|
67 | int TOI::getMinSn(){
|
---|
68 | return producer->getMinOut();
|
---|
69 | }
|
---|
70 |
|
---|
71 | int TOI::getMaxSn(){
|
---|
72 | return producer->getMaxOut();
|
---|
73 | }
|
---|
74 |
|
---|
75 | #ifndef NO_SOPHYA
|
---|
76 | Array TOI::getError(int iStart, int iEnd) {
|
---|
77 | if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
|
---|
78 | return errorTOI->getData(iStart, iEnd);
|
---|
79 | }
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | /*RZCMV
|
---|
83 | double TOI::getError(int i) {
|
---|
84 | if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
|
---|
85 | return errorTOI->getData(i);
|
---|
86 | }
|
---|
87 |
|
---|
88 | void TOI::putDataError(int i, double value, double error, int_4 flag) {
|
---|
89 | if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
|
---|
90 | putData(i, value, flag);
|
---|
91 | errorTOI->putData(i, value, flag);
|
---|
92 | }
|
---|
93 |
|
---|
94 | */
|
---|
95 |
|
---|
96 | #ifndef NO_SOPHYA
|
---|
97 | Array TOI::getData(int iStart, int iEnd) {
|
---|
98 | lock();
|
---|
99 | Array a = doGetData(iStart, iEnd);
|
---|
100 | unlock();
|
---|
101 | if (fgsigput) { fgsigput = false; broadcast(); }
|
---|
102 | return a;
|
---|
103 | }
|
---|
104 | #endif
|
---|
105 |
|
---|
106 | double TOI::getData(int i) {
|
---|
107 | lock();
|
---|
108 | int_4 flg;
|
---|
109 | double dat;
|
---|
110 | doGetData(i, dat, flg);
|
---|
111 | unlock();
|
---|
112 | if (fgsigput) { fgsigput = false; broadcast(); }
|
---|
113 | return dat;
|
---|
114 | }
|
---|
115 |
|
---|
116 | void TOI::getData(int i, double &data,int_4 &flag) {
|
---|
117 | lock();
|
---|
118 | doGetData(i, data, flag);
|
---|
119 | unlock();
|
---|
120 | if (fgsigput) { fgsigput = false; broadcast(); }
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 | #ifndef NO_SOPHYA
|
---|
127 | TArray<int_4> TOI::getFlag(int iStart, int iEnd) {
|
---|
128 | lock();
|
---|
129 | TArray<int_4> a = doGetFlag(iStart, iEnd);
|
---|
130 | unlock();
|
---|
131 | if (fgsigput) { fgsigput = false; broadcast(); }
|
---|
132 | return a;
|
---|
133 | }
|
---|
134 | #endif
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 |
|
---|
139 | void TOI::putData(int i, double value, int_4 flag) {
|
---|
140 | lock();
|
---|
141 | doPutData(i, value, flag);
|
---|
142 | unlock();
|
---|
143 | if (fgsigget) { fgsigget = false; broadcast(); }
|
---|
144 | }
|
---|
145 |
|
---|
146 | void TOI::waitForData(int iStart, int iEnd) {
|
---|
147 | if (producer == NULL) throw NotFoundExc("TOI has no producer !");
|
---|
148 |
|
---|
149 | DataStatus s = isDataAvail(iStart, iEnd);
|
---|
150 | if (s == DATA_OK) {
|
---|
151 | return;
|
---|
152 | }
|
---|
153 | if (s == DATA_DELETED) {
|
---|
154 | throw NotFoundExc("Data has been purged !");
|
---|
155 | }
|
---|
156 |
|
---|
157 | producer->lock();
|
---|
158 | while (isDataAvailNL(iStart, iEnd) == DATA_NOT_YET) {
|
---|
159 | producer->wait();
|
---|
160 | }
|
---|
161 | producer->unlock();
|
---|
162 | return;
|
---|
163 | }
|
---|
164 |
|
---|
165 | void TOI::waitForData(int i) {
|
---|
166 | waitForData(i,i);
|
---|
167 | }
|
---|
168 |
|
---|
169 | void TOI::waitForAnyData() {
|
---|
170 | if (! hasSomeData()) {
|
---|
171 | producer->lock();
|
---|
172 | producer->wait();
|
---|
173 | producer->unlock();
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | TOI::DataStatus TOI::isDataAvail(int i) {
|
---|
178 | lock();
|
---|
179 | DataStatus stat = isDataAvailNL(i);
|
---|
180 | unlock();
|
---|
181 | return stat;
|
---|
182 | }
|
---|
183 |
|
---|
184 | TOI::DataStatus TOI::isDataAvail(int i, int j) {
|
---|
185 | lock();
|
---|
186 | DataStatus stat = isDataAvailNL(i,j);
|
---|
187 | unlock();
|
---|
188 | return stat;
|
---|
189 | }
|
---|
190 |
|
---|
191 | TOI::DataStatus TOI::isDataAvailNL(int i) {
|
---|
192 | return isDataAvailNL(i,i);
|
---|
193 | }
|
---|
194 |
|
---|
195 | void TOI::wontNeedBefore(int i) {
|
---|
196 | int j=i;
|
---|
197 | for (vector<TOIProcessor*>::iterator k = consumers.begin();
|
---|
198 | k != consumers.end(); k++) {
|
---|
199 | if ((*k)->wontNeedValue < j) j = (*k)->wontNeedValue;
|
---|
200 | }
|
---|
201 | lock();
|
---|
202 | doWontNeedBefore(j);
|
---|
203 | unlock();
|
---|
204 | }
|
---|
205 |
|
---|
206 | void TOI::doWontNeedBefore(int i) {
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | TOIRegularWindow::TOIRegularWindow() {
|
---|
211 | i0 = -1;
|
---|
212 | }
|
---|
213 |
|
---|
214 | TOIRegularWindow::TOIRegularWindow(string nm) {
|
---|
215 | i0 = -1;
|
---|
216 | setName(nm);
|
---|
217 | }
|
---|
218 |
|
---|
219 | TOIRegularWindow::~TOIRegularWindow() {
|
---|
220 | }
|
---|
221 |
|
---|
222 | TOI::DataStatus TOIRegularWindow::isDataAvailNL(int iStart, int iEnd) {
|
---|
223 | if (iEnd >= i0 + (long)data.size()) return DATA_NOT_YET;
|
---|
224 | if (iStart < i0) return DATA_DELETED;
|
---|
225 | return DATA_OK;
|
---|
226 | }
|
---|
227 |
|
---|
228 | TOI::DataStatus TOIRegularWindow::isDataAvailNL(int i) {
|
---|
229 | return TOI::isDataAvailNL(i);
|
---|
230 | }
|
---|
231 |
|
---|
232 | void TOIRegularWindow::doWontNeedBefore(int i) {
|
---|
233 | if (i>= i0 + (long)data.size())
|
---|
234 | i = i0 + (long)data.size() - 1;
|
---|
235 | if (i>i0) { // don't empty list
|
---|
236 | int osz = data.size();
|
---|
237 | data.erase(data.begin(), data.begin()+(i-i0));
|
---|
238 | flags.erase(flags.begin(), flags.begin()+(i-i0));
|
---|
239 | i0 = i;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 |
|
---|
244 | #ifndef NO_SOPHYA
|
---|
245 | Array TOIRegularWindow::doGetData(int iStart, int iEnd) {
|
---|
246 | if (!isDataAvailNL(iStart, iEnd)) {
|
---|
247 | throw RangeCheckError("TOI::getData : data not available");
|
---|
248 | }
|
---|
249 | Array dat(iEnd - iStart + 1);
|
---|
250 | long j0 = iStart - i0;
|
---|
251 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
252 | dat[i] = data[i+j0];
|
---|
253 | }
|
---|
254 | return dat;
|
---|
255 | }
|
---|
256 | #endif
|
---|
257 |
|
---|
258 | void TOIRegularWindow::doGetData(int i, double & val, int_4 & flg) {
|
---|
259 | if (isDataAvailNL(i) != DATA_OK) {
|
---|
260 | cerr << "TOI::getData : data not available " << i << endl;
|
---|
261 | throw RangeCheckError("TOI::getData : data not available");
|
---|
262 | }
|
---|
263 |
|
---|
264 | val = data[i - i0];
|
---|
265 | flg = flags[i - i0];
|
---|
266 |
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | #ifndef NO_SOPHYA
|
---|
271 | TArray<int_4> TOIRegularWindow::doGetFlag(int iStart, int iEnd) {
|
---|
272 | if (isDataAvailNL(iStart, iEnd) != DATA_OK) throw RangeCheckError("TOI::getData : data not available");
|
---|
273 | TArray<int_4> dat(iEnd - iStart + 1);
|
---|
274 | long j0 = iStart - i0;
|
---|
275 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
276 | dat[i] = flags[i+j0];
|
---|
277 | }
|
---|
278 | return dat;
|
---|
279 | }
|
---|
280 | #endif
|
---|
281 |
|
---|
282 | /*RZCMV
|
---|
283 | int_4 TOIRegularWindow::doGetFlag(int i) {
|
---|
284 | if (isDataAvailNL(i) != DATA_OK) {
|
---|
285 | cerr << "TOI::getFlag : data not available " << i << endl;
|
---|
286 | throw RangeCheckError("TOI::getFlag : data not available");
|
---|
287 | }
|
---|
288 | return flags[i - i0];
|
---|
289 | }
|
---|
290 | */
|
---|
291 |
|
---|
292 |
|
---|
293 | void TOIRegularWindow::doPutData(int i, double value, int_4 flag) {
|
---|
294 | if (i0 == -1) {
|
---|
295 | data.insert(data.begin(), 1, defaultValue);
|
---|
296 | flags.insert(flags.begin(), 1, 0);
|
---|
297 | i0 = i;
|
---|
298 | } else if (i<i0) {
|
---|
299 | data.insert(data.begin(), i0-i, defaultValue);
|
---|
300 | flags.insert(flags.begin(), i0-i, 0);
|
---|
301 | i0 = i;
|
---|
302 | } else if (i>=i0+(int)data.size()) {
|
---|
303 | data.insert(data.end(), (long) (i-(i0+data.size())+1), defaultValue);
|
---|
304 | flags.insert(flags.end(), (long) (i-(i0+flags.size())+1), 0);
|
---|
305 | }
|
---|
306 | data[i-i0] = value;
|
---|
307 | flags[i-i0] = flag;
|
---|
308 | }
|
---|
309 |
|
---|
310 | bool TOIRegularWindow::hasSomeData() {
|
---|
311 | lock();
|
---|
312 | bool x = !data.empty();
|
---|
313 | unlock();
|
---|
314 | return x;
|
---|
315 | }
|
---|
316 |
|
---|
317 | int TOIRegularWindow::nextDataAvail(int iAfter) {
|
---|
318 | lock();
|
---|
319 | if (iAfter >= i0 + (long)data.size()) {unlock(); return -1;}
|
---|
320 | if (iAfter < i0) {unlock(); return i0;}
|
---|
321 | unlock();
|
---|
322 | return iAfter+1;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /* A faire, le nettoyage (heuristique selon demandes ?, guide ? ) */
|
---|
326 |
|
---|
327 |
|
---|