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 | // pthread_mutex_setname_np(&mutex, (name + "_toi_mutex").c_str(), 0);
|
---|
28 | defaultValue = 0;
|
---|
29 | producer = NULL;
|
---|
30 | dbg = false;
|
---|
31 | }
|
---|
32 |
|
---|
33 | TOI::~TOI() {
|
---|
34 | pthread_mutex_destroy(&mutex);
|
---|
35 | }
|
---|
36 |
|
---|
37 | void TOI::setProducer(TOIProcessor* p) {
|
---|
38 | if (producer)
|
---|
39 | throw DuplicateIdExc("TOI::setProducer : producer already defined");
|
---|
40 | producer = p;
|
---|
41 | }
|
---|
42 |
|
---|
43 | void TOI::addConsumer(TOIProcessor* p) {
|
---|
44 | consumers.push_back(p);
|
---|
45 | }
|
---|
46 |
|
---|
47 | int TOI::getMinSn(){
|
---|
48 | return producer->getMinOut();
|
---|
49 | }
|
---|
50 |
|
---|
51 | int TOI::getMaxSn(){
|
---|
52 | return producer->getMaxOut();
|
---|
53 | }
|
---|
54 |
|
---|
55 | #ifndef NO_SOPHYA
|
---|
56 | Array TOI::getError(int iStart, int iEnd) {
|
---|
57 | if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
|
---|
58 | return errorTOI->getData(iStart, iEnd);
|
---|
59 | }
|
---|
60 | #endif
|
---|
61 |
|
---|
62 | double TOI::getError(int i) {
|
---|
63 | if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
|
---|
64 | return errorTOI->getData(i);
|
---|
65 | }
|
---|
66 |
|
---|
67 | void TOI::putDataError(int i, double value, double error, int_4 flag) {
|
---|
68 | if (errorTOI == NULL) throw NotFoundExc("TOI::getDataError : no Error TOI");
|
---|
69 | putData(i, value, flag);
|
---|
70 | errorTOI->putData(i, value, flag);
|
---|
71 | }
|
---|
72 |
|
---|
73 | #ifndef NO_SOPHYA
|
---|
74 | Array TOI::getData(int iStart, int iEnd) {
|
---|
75 | lock();
|
---|
76 | Array a = doGetData(iStart, iEnd);
|
---|
77 | unlock();
|
---|
78 | return a;
|
---|
79 | }
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | double TOI::getData(int i) {
|
---|
83 | lock();
|
---|
84 | double dat = doGetData(i);
|
---|
85 | unlock();
|
---|
86 | return dat;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | #ifndef NO_SOPHYA
|
---|
91 | TArray<int_4> TOI::getFlag(int iStart, int iEnd) {
|
---|
92 | lock();
|
---|
93 | TArray<int_4> a = doGetFlag(iStart, iEnd);
|
---|
94 | unlock();
|
---|
95 | return a;
|
---|
96 | }
|
---|
97 | #endif
|
---|
98 |
|
---|
99 | int_4 TOI::getFlag(int i) {
|
---|
100 | lock();
|
---|
101 | int_4 f = doGetFlag(i);
|
---|
102 | unlock();
|
---|
103 | return f;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 | void TOI::putData(int i, double value, int_4 flag) {
|
---|
109 | lock();
|
---|
110 | doPutData(i, value, flag);
|
---|
111 | unlock();
|
---|
112 | }
|
---|
113 |
|
---|
114 | void TOI::waitForData(int iStart, int iEnd) {
|
---|
115 | if (producer == NULL) throw NotFoundExc("TOI has no producer !");
|
---|
116 |
|
---|
117 | DataStatus s = isDataAvail(iStart, iEnd);
|
---|
118 | if (s == DATA_OK) {
|
---|
119 | return;
|
---|
120 | }
|
---|
121 | if (s == DATA_DELETED) {
|
---|
122 | throw NotFoundExc("Data has been purged !");
|
---|
123 | }
|
---|
124 |
|
---|
125 | producer->lock();
|
---|
126 | while (isDataAvailNL(iStart, iEnd) == DATA_NOT_YET) {
|
---|
127 | producer->wait();
|
---|
128 | }
|
---|
129 | producer->unlock();
|
---|
130 | return;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void TOI::waitForData(int i) {
|
---|
134 | waitForData(i,i);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void TOI::waitForAnyData() {
|
---|
138 | if (! hasSomeData()) {
|
---|
139 | producer->lock();
|
---|
140 | producer->wait();
|
---|
141 | producer->unlock();
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | TOI::DataStatus TOI::isDataAvail(int i) {
|
---|
146 | lock();
|
---|
147 | DataStatus stat = isDataAvailNL(i);
|
---|
148 | unlock();
|
---|
149 | return stat;
|
---|
150 | }
|
---|
151 |
|
---|
152 | TOI::DataStatus TOI::isDataAvail(int i, int j) {
|
---|
153 | lock();
|
---|
154 | DataStatus stat = isDataAvailNL(i,j);
|
---|
155 | unlock();
|
---|
156 | return stat;
|
---|
157 | }
|
---|
158 |
|
---|
159 | TOI::DataStatus TOI::isDataAvailNL(int i) {
|
---|
160 | return isDataAvailNL(i,i);
|
---|
161 | }
|
---|
162 |
|
---|
163 | void TOI::wontNeedBefore(int i) {
|
---|
164 | int j=i;
|
---|
165 | for (vector<TOIProcessor*>::iterator k = consumers.begin();
|
---|
166 | k != consumers.end(); k++) {
|
---|
167 | if ((*k)->wontNeedValue < j) j = (*k)->wontNeedValue;
|
---|
168 | }
|
---|
169 | lock();
|
---|
170 | doWontNeedBefore(j);
|
---|
171 | unlock();
|
---|
172 | }
|
---|
173 |
|
---|
174 | void TOI::doWontNeedBefore(int i) {
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | TOIRegularWindow::TOIRegularWindow() {
|
---|
179 | i0 = -1;
|
---|
180 | }
|
---|
181 |
|
---|
182 | TOIRegularWindow::TOIRegularWindow(string nm) {
|
---|
183 | i0 = -1;
|
---|
184 | setName(nm);
|
---|
185 | }
|
---|
186 |
|
---|
187 | TOIRegularWindow::~TOIRegularWindow() {
|
---|
188 | }
|
---|
189 |
|
---|
190 | TOI::DataStatus TOIRegularWindow::isDataAvailNL(int iStart, int iEnd) {
|
---|
191 | if (iEnd >= i0 + (long)data.size()) return DATA_NOT_YET;
|
---|
192 | if (iStart < i0) return DATA_DELETED;
|
---|
193 | return DATA_OK;
|
---|
194 | }
|
---|
195 |
|
---|
196 | TOI::DataStatus TOIRegularWindow::isDataAvailNL(int i) {
|
---|
197 | return TOI::isDataAvailNL(i);
|
---|
198 | }
|
---|
199 |
|
---|
200 | void TOIRegularWindow::doWontNeedBefore(int i) {
|
---|
201 | if (i>= i0 + (long)data.size())
|
---|
202 | i = i0 + (long)data.size() - 1;
|
---|
203 | if (i>i0) { // don't empty list
|
---|
204 | int osz = data.size();
|
---|
205 | data.erase(data.begin(), data.begin()+(i-i0));
|
---|
206 | flags.erase(flags.begin(), flags.begin()+(i-i0));
|
---|
207 | i0 = i;
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | #ifndef NO_SOPHYA
|
---|
213 | Array TOIRegularWindow::doGetData(int iStart, int iEnd) {
|
---|
214 | if (!isDataAvailNL(iStart, iEnd)) throw RangeCheckError("TOI::getData : data not available");
|
---|
215 | Array dat(iEnd - iStart + 1);
|
---|
216 | long j0 = iStart - i0;
|
---|
217 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
218 | dat[i] = data[i+j0];
|
---|
219 | }
|
---|
220 | return dat;
|
---|
221 | }
|
---|
222 | #endif
|
---|
223 |
|
---|
224 | double TOIRegularWindow::doGetData(int i) {
|
---|
225 | if (isDataAvailNL(i) != DATA_OK) throw RangeCheckError("TOI::getData : data not available");
|
---|
226 | double dat = data[i - i0];
|
---|
227 | return dat;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | #ifndef NO_SOPHYA
|
---|
232 | TArray<int_4> TOIRegularWindow::doGetFlag(int iStart, int iEnd) {
|
---|
233 | if (isDataAvailNL(iStart, iEnd) != DATA_OK) throw RangeCheckError("TOI::getData : data not available");
|
---|
234 | TArray<int_4> dat(iEnd - iStart + 1);
|
---|
235 | long j0 = iStart - i0;
|
---|
236 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
237 | dat[i] = flags[i+j0];
|
---|
238 | }
|
---|
239 | return dat;
|
---|
240 | }
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | int_4 TOIRegularWindow::doGetFlag(int i) {
|
---|
244 | if (!isDataAvailNL(i)) throw RangeCheckError("TOI::getData : data not available");
|
---|
245 | return flags[i - i0];
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | void TOIRegularWindow::doPutData(int i, double value, int_4 flag) {
|
---|
250 | if (i0 == -1) {
|
---|
251 | data.insert(data.begin(), 1, defaultValue);
|
---|
252 | flags.insert(flags.begin(), 1, 0);
|
---|
253 | i0 = i;
|
---|
254 | } else if (i<i0) {
|
---|
255 | data.insert(data.begin(), i0-i, defaultValue);
|
---|
256 | flags.insert(flags.begin(), i0-i, 0);
|
---|
257 | i0 = i;
|
---|
258 | } else if (i>=i0+(int)data.size()) {
|
---|
259 | data.insert(data.end(), (long) (i-(i0+data.size())+1), defaultValue);
|
---|
260 | flags.insert(flags.end(), (long) (i-(i0+flags.size())+1), 0);
|
---|
261 | }
|
---|
262 | data[i-i0] = value;
|
---|
263 | flags[i-i0] = flag;
|
---|
264 | }
|
---|
265 |
|
---|
266 | bool TOIRegularWindow::hasSomeData() {
|
---|
267 | lock();
|
---|
268 | bool x = !data.empty();
|
---|
269 | unlock();
|
---|
270 | return x;
|
---|
271 | }
|
---|
272 |
|
---|
273 | int TOIRegularWindow::nextDataAvail(int iAfter) {
|
---|
274 | lock();
|
---|
275 | if (iAfter >= i0 + (long)data.size()) {unlock(); return -1;}
|
---|
276 | if (iAfter < i0) {unlock(); return i0;}
|
---|
277 | unlock();
|
---|
278 | return iAfter+1;
|
---|
279 | }
|
---|
280 |
|
---|
281 | /* A faire, le nettoyage (heuristique selon demandes ?, guide ? ) */
|
---|
282 |
|
---|
283 |
|
---|