source: Sophya/trunk/ArchTOIPipe/Kernel/toi.cc@ 1442

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

Protections diverses dans TOIProcessor et FITSTOIReader/Writer
Ajout d'un TOI (TOISeqBuffered) avec gestion d'un buffer entre put/get
Ajout de processeurs de test (RZTOIProc...) , programme test associe
et SMakefile (pour compil avec SOPHYA)

Reza 12/3/2001

File size: 7.0 KB
Line 
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
16TOI::TOI() {
17 TOIInit();
18}
19
20TOI::TOI(string n) {
21 name = n;
22 TOIInit();
23}
24
25void 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
39TOI::~TOI() {
40 pthread_mutex_destroy(&mutex);
41}
42
43void 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
57void TOI::setProducer(TOIProcessor* p) {
58 if (producer)
59 throw DuplicateIdExc("TOI::setProducer : producer already defined");
60 producer = p;
61}
62
63void TOI::addConsumer(TOIProcessor* p) {
64 consumers.push_back(p);
65}
66
67int TOI::getMinSn(){
68 return producer->getMinOut();
69}
70
71int TOI::getMaxSn(){
72 return producer->getMaxOut();
73}
74
75#ifndef NO_SOPHYA
76Array 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
82double TOI::getError(int i) {
83 if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
84 return errorTOI->getData(i);
85}
86
87void TOI::putDataError(int i, double value, double error, int_4 flag) {
88 if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
89 putData(i, value, flag);
90 errorTOI->putData(i, value, flag);
91}
92
93#ifndef NO_SOPHYA
94Array TOI::getData(int iStart, int iEnd) {
95 lock();
96 Array a = doGetData(iStart, iEnd);
97 unlock();
98 if (fgsigput) { fgsigput = false; broadcast(); }
99 return a;
100}
101#endif
102
103double TOI::getData(int i) {
104 lock();
105 double dat = doGetData(i);
106 unlock();
107 if (fgsigput) { fgsigput = false; broadcast(); }
108 return dat;
109}
110
111
112#ifndef NO_SOPHYA
113TArray<int_4> TOI::getFlag(int iStart, int iEnd) {
114 lock();
115 TArray<int_4> a = doGetFlag(iStart, iEnd);
116 unlock();
117 if (fgsigput) { fgsigput = false; broadcast(); }
118 return a;
119}
120#endif
121
122int_4 TOI::getFlag(int i) {
123 lock();
124 int_4 f = doGetFlag(i);
125 unlock();
126 if (fgsigput) { fgsigput = false; broadcast(); }
127 return f;
128}
129
130
131
132void TOI::putData(int i, double value, int_4 flag) {
133 lock();
134 doPutData(i, value, flag);
135 unlock();
136 if (fgsigget) { fgsigget = false; broadcast(); }
137}
138
139void TOI::waitForData(int iStart, int iEnd) {
140 if (producer == NULL) throw NotFoundExc("TOI has no producer !");
141
142 DataStatus s = isDataAvail(iStart, iEnd);
143 if (s == DATA_OK) {
144 return;
145 }
146 if (s == DATA_DELETED) {
147 throw NotFoundExc("Data has been purged !");
148 }
149
150 producer->lock();
151 while (isDataAvailNL(iStart, iEnd) == DATA_NOT_YET) {
152 producer->wait();
153 }
154 producer->unlock();
155 return;
156}
157
158void TOI::waitForData(int i) {
159 waitForData(i,i);
160}
161
162void TOI::waitForAnyData() {
163 if (! hasSomeData()) {
164 producer->lock();
165 producer->wait();
166 producer->unlock();
167 }
168}
169
170TOI::DataStatus TOI::isDataAvail(int i) {
171 lock();
172 DataStatus stat = isDataAvailNL(i);
173 unlock();
174 return stat;
175}
176
177TOI::DataStatus TOI::isDataAvail(int i, int j) {
178 lock();
179 DataStatus stat = isDataAvailNL(i,j);
180 unlock();
181 return stat;
182}
183
184TOI::DataStatus TOI::isDataAvailNL(int i) {
185 return isDataAvailNL(i,i);
186}
187
188void TOI::wontNeedBefore(int i) {
189 int j=i;
190 for (vector<TOIProcessor*>::iterator k = consumers.begin();
191 k != consumers.end(); k++) {
192 if ((*k)->wontNeedValue < j) j = (*k)->wontNeedValue;
193 }
194 lock();
195 doWontNeedBefore(j);
196 unlock();
197}
198
199void TOI::doWontNeedBefore(int i) {
200}
201
202
203TOIRegularWindow::TOIRegularWindow() {
204 i0 = -1;
205}
206
207TOIRegularWindow::TOIRegularWindow(string nm) {
208 i0 = -1;
209 setName(nm);
210}
211
212TOIRegularWindow::~TOIRegularWindow() {
213}
214
215TOI::DataStatus TOIRegularWindow::isDataAvailNL(int iStart, int iEnd) {
216 if (iEnd >= i0 + (long)data.size()) return DATA_NOT_YET;
217 if (iStart < i0) return DATA_DELETED;
218 return DATA_OK;
219}
220
221TOI::DataStatus TOIRegularWindow::isDataAvailNL(int i) {
222 return TOI::isDataAvailNL(i);
223}
224
225void TOIRegularWindow::doWontNeedBefore(int i) {
226 if (i>= i0 + (long)data.size())
227 i = i0 + (long)data.size() - 1;
228 if (i>i0) { // don't empty list
229 int osz = data.size();
230 data.erase(data.begin(), data.begin()+(i-i0));
231 flags.erase(flags.begin(), flags.begin()+(i-i0));
232 i0 = i;
233 }
234}
235
236
237#ifndef NO_SOPHYA
238Array TOIRegularWindow::doGetData(int iStart, int iEnd) {
239 if (!isDataAvailNL(iStart, iEnd)) {
240 throw RangeCheckError("TOI::getData : data not available");
241 }
242 Array dat(iEnd - iStart + 1);
243 long j0 = iStart - i0;
244 for (int i=0; i<iEnd-iStart+1; i++) {
245 dat[i] = data[i+j0];
246 }
247 return dat;
248}
249#endif
250
251double TOIRegularWindow::doGetData(int i) {
252 if (isDataAvailNL(i) != DATA_OK) {
253 cerr << "TOI::getData : data not available " << i << endl;
254 throw RangeCheckError("TOI::getData : data not available");
255 }
256 double dat = data[i - i0];
257 return dat;
258}
259
260
261#ifndef NO_SOPHYA
262TArray<int_4> TOIRegularWindow::doGetFlag(int iStart, int iEnd) {
263 if (isDataAvailNL(iStart, iEnd) != DATA_OK) throw RangeCheckError("TOI::getData : data not available");
264 TArray<int_4> dat(iEnd - iStart + 1);
265 long j0 = iStart - i0;
266 for (int i=0; i<iEnd-iStart+1; i++) {
267 dat[i] = flags[i+j0];
268 }
269 return dat;
270}
271#endif
272
273int_4 TOIRegularWindow::doGetFlag(int i) {
274 if (isDataAvailNL(i) != DATA_OK) {
275 cerr << "TOI::getFlag : data not available " << i << endl;
276 throw RangeCheckError("TOI::getFlag : data not available");
277 }
278 return flags[i - i0];
279}
280
281
282void TOIRegularWindow::doPutData(int i, double value, int_4 flag) {
283 if (i0 == -1) {
284 data.insert(data.begin(), 1, defaultValue);
285 flags.insert(flags.begin(), 1, 0);
286 i0 = i;
287 } else if (i<i0) {
288 data.insert(data.begin(), i0-i, defaultValue);
289 flags.insert(flags.begin(), i0-i, 0);
290 i0 = i;
291 } else if (i>=i0+(int)data.size()) {
292 data.insert(data.end(), (long) (i-(i0+data.size())+1), defaultValue);
293 flags.insert(flags.end(), (long) (i-(i0+flags.size())+1), 0);
294 }
295 data[i-i0] = value;
296 flags[i-i0] = flag;
297}
298
299bool TOIRegularWindow::hasSomeData() {
300 lock();
301 bool x = !data.empty();
302 unlock();
303 return x;
304}
305
306int TOIRegularWindow::nextDataAvail(int iAfter) {
307 lock();
308 if (iAfter >= i0 + (long)data.size()) {unlock(); return -1;}
309 if (iAfter < i0) {unlock(); return i0;}
310 unlock();
311 return iAfter+1;
312}
313
314/* A faire, le nettoyage (heuristique selon demandes ?, guide ? ) */
315
316
Note: See TracBrowser for help on using the repository browser.