source: Sophya/trunk/ArchTOIPipe/Kernel/toiseqbuff.cc@ 1992

Last change on this file since 1992 was 1992, checked in by ansari, 23 years ago

Ajout de syncOldWay=false pour TOISeqBuff - Reza 8/5/2002

File size: 10.1 KB
Line 
1// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
2// Eric Aubourg
3// Christophe Magneville
4// Reza Ansari
5// $Id: toiseqbuff.cc,v 1.12 2002-05-08 22:22:33 ansari Exp $
6
7#include "toiprocessor.h"
8#include "toiseqbuff.h"
9#include <pthread.h>
10
11#ifdef WITH_SOPHYA
12#include "pexceptions.h"
13#else
14#include "apexceptions.h"
15#endif
16
17
18TOISeqBuffered::TOISeqBuffered(int wsz) {
19 data = NULL;
20 flags = NULL;
21 AllocBuffer(wsz);
22 setName("toiseqbuff");
23 syncOldWay = false;
24}
25
26TOISeqBuffered::TOISeqBuffered(string nm, int wsz) {
27 data = NULL;
28 flags = NULL;
29 AllocBuffer(wsz);
30 setName(nm);
31 syncOldWay = false;
32}
33
34TOISeqBuffered::~TOISeqBuffered() {
35 delete[] data;
36 delete[] flags;
37}
38
39void TOISeqBuffered::AllocBuffer(int wsz)
40{
41 if (wsz < 128) wsz = 128;
42 wsize = wsz;
43 buffsize = 2*wsz;
44 if (data) delete[] data;
45 if (flags) delete[] flags;
46 data = new double[buffsize];
47 flags = new uint_8[buffsize];
48 for(int k=0; k<buffsize; k++) {
49 data[k] = defaultValue;
50 flags[k] = 0;
51 }
52 next_in = next_out = -1;
53 first_in = first_out = -1;
54 started = false;
55 dbglev = 0;
56}
57
58void TOISeqBuffered::PrintStatus(::ostream & os) const
59{
60 os << "---TOISeqBuffered::PrintStatus() - Name=" << getName()
61 << "\n WindowSize= " << wsize << " BufferSize= " << buffsize << endl;
62 os << "Index: FirstIn= " << getFirstIn() << " LastIn= " << getLastIn()
63 << " Total= " << getLastIn()-getFirstIn()+1 << endl;
64 os << "Index: FirstOut= " << getFirstOut() << " LastOut= " << getLastOut()
65 << " Total= " << getLastOut()-getFirstOut()+1 << endl;
66 os << " WaitStatus: Put/" ;
67 if (isPutWaiting()) os << "Waiting " ;
68 else os << "Running ";
69 os << " PutCountWait= " << getCountWaitPut() << endl;
70 os << " WaitStatus: Get/" ;
71 if (isGetWaiting()) os << "Waiting " ;
72 else os << "Running ";
73 os << " GetCountWait= " << getCountWaitGet() << endl;
74}
75
76TOI::DataStatus TOISeqBuffered::isDataAvailNL(int iStart, int iEnd) {
77 if (iEnd < iStart)
78 throw RangeCheckError("TOISeqBuffered::isDataAvailNL : iEnd<iStart !");
79 if (!started) return DATA_NOT_YET;
80 if (iEnd >= next_in) return DATA_NOT_YET;
81 if (isDataDeleted(iStart)) return DATA_DELETED;
82 return DATA_OK;
83}
84
85TOI::DataStatus TOISeqBuffered::isDataAvailNL(int i) {
86 return TOI::isDataAvailNL(i);
87}
88
89void TOISeqBuffered::wontNeedBefore(int i) {
90 // $CHECK$ Reza 30/4/2001 - Je ne sais pas a quoi ca sert !
91 // next_out = i; $CHECK$ Reza 30/4/2001
92}
93
94
95#ifndef NO_SOPHYA
96/* ---- l'interface va etre modifiee, NE PAS UTILISER
97Array TOISeqBuffered::doGetData(int iStart, int iEnd) {
98 // if (iEnd < iStart)
99 // throw RangeCheckError("TOI::getData : iEnd<iStart !");
100 // if (iStart <= out_last)
101 if (!started) waitGet();
102 if (!isDataAvailNL(iStart, iEnd))
103 throw RangeCheckError("TOISeqBuffered::getData(iS,iE) : data not available");
104 cleanWaitGet();
105 Vector dat(iEnd - iStart + 1);
106 for (int i=0; i<iEnd-iStart+1; i++) {
107 dat(i) = dataRef(i+iStart);
108 }
109 if (first_out < 0) first_out = iStart;
110 if ((iEnd+1) > next_out) next_out = iEnd+1;
111 if (isPutWaiting() && (next_in-next_out < wsize/2 )) signalPut();
112 return dat;
113}
114 l'interface va etre modifiee, NE PAS UTILISER ---- */
115#endif
116
117double TOISeqBuffered::getData(int i)
118{
119 double val;
120 uint_8 flg;
121 getData(i, val, flg);
122 return(val);
123}
124
125
126void TOISeqBuffered::getData(int i, double & val, uint_8 & flg) {
127 if (!started) {
128 cout << " TOISeqBuffered::getData() - waitGet() Waiting for start ... " << endl;
129 waitGet();
130 }
131 cleanWaitGet();
132 if (isDataDeleted(i)) {
133 if (dbglev > 0)
134 cout << " TOISeqBuffered::getData() - DataDeleted() name=" << getName()
135 << " i=" << i << " next_in= " << next_in
136 << " next_out=" << next_out << endl;
137 throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted");
138 }
139 while (i >= next_in) {
140 if (i>next_out) next_out = i;
141 if (dbglev > 0)
142 cout << " TOISeqBuffered::getData() - waitGet() name=" << getName()
143 << " i=" << i << " next_in= " << next_in
144 << " next_out=" << next_out << endl;
145 waitGet();
146 if (dbglev > 0)
147 cout << " ... Out of waitGet() i=" << i
148 << " next_in= " << next_in << " next_out=" << next_out << endl;
149 }
150 cleanWaitGet();
151 val = dataRef(i);
152 flg = flagRef(i);
153 if (first_out < 0) first_out = i;
154 if ((i+1) > next_out) next_out = i+1;
155 if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
156 if (dbglev > 0)
157 cout << " TOISeqBuffered::getData() - signalPut() name=" << getName()
158 << " i=" << i << " next_in= " << next_in
159 << " next_out=" << next_out << endl;
160 signalPut(); broadcast();
161 }
162 return;
163}
164
165
166void TOISeqBuffered::getData(int i, int n, double* data, uint_8* flg)
167{
168 if (!started) {
169 cout << " TOISeqBuffered::getData(i,n ...) - waitGet() Waiting for start ... " << endl;
170 waitGet();
171 }
172 cleanWaitGet();
173 if (isDataDeleted(i)) {
174 if (dbglev > 0)
175 cout << " TOISeqBuffered::getData(i,n ...) - DataDeleted() name=" << getName()
176 << " i=" << i << " next_in= " << next_in
177 << " next_out=" << next_out << endl;
178 throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted");
179 }
180 for(int j=i; i<i+n; j++) {
181 while (j >= next_in) {
182 if (j>next_out) next_out = j;
183 if (dbglev > 0)
184 cout << " TOISeqBuffered::getData(i,n ...) - waitGet() name=" << getName()
185 << " j=" << j << " next_in= " << next_in
186 << " next_out=" << next_out << endl;
187 waitGet();
188 if (dbglev > 0)
189 cout << " ... Out of waitGet() j=" << j
190 << " next_in= " << next_in << " next_out=" << next_out << endl;
191 }
192 cleanWaitGet();
193 data[j-i] = dataRef(j);
194 if (flg) flg[j-i] = flagRef(j);
195 if (first_out < 0) first_out = j;
196 if ((j+1) > next_out) next_out = j+1;
197 if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
198 if (dbglev > 0)
199 cout << " TOISeqBuffered::getData(i,n ...) - signalPut() name=" << getName()
200 << " i=" << i << " next_in= " << next_in
201 << " next_out=" << next_out << endl;
202 signalPut(); broadcast();
203 }
204 }
205 return;
206
207}
208
209
210#ifndef NO_SOPHYA
211/* ---- l'interface va etre modifiee, NE PAS UTILISER
212TArray<int_4> TOISeqBuffered::doGetFlag(int iStart, int iEnd) {
213 if (!started) waitGet();
214 cleanWaitGet();
215 if (!isDataAvailNL(iStart, iEnd))
216 throw RangeCheckError("TOISeqBuffered::getFlag(iS,iE) : data not available");
217 TVector<int_4> dat(iEnd - iStart + 1);
218 for (int i=0; i<iEnd-iStart+1; i++) {
219 dat[i] = flagRef(i+iStart);
220 }
221 return dat;
222}
223 l'interface va etre modifiee, NE PAS UTILISER ---- */
224#endif
225
226/*RZCMV
227int_4 TOISeqBuffered::doGetFlag(int i) {
228 if (!started) waitGet();
229 cleanWaitGet();
230 if (isDataDeleted(i))
231 throw RangeCheckError("TOISeqBuffered::doGetFlag(i) : data deleted");
232 while (i >= next_in) waitGet();
233 int_4 dat = flagRef(i);
234 return dat;
235}
236*/
237
238
239void TOISeqBuffered::putData(int i, double value, uint_8 flag) {
240 if (!started) {
241 first_in = next_in = i;
242 next_out = next_in;
243 started = true;
244 }
245 else {
246 if (i != next_in) {
247 if (dbglev > 0)
248 cout << " TOISeqBuffered::putData() - i!=next_in() name=" << getName()
249 << " i=" << i << " next_in= " << next_in
250 << " next_out=" << next_out << endl;
251 string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName();
252 throw RangeCheckError(msg);
253 }
254 if (next_in-next_out >= wsize) {
255 if (dbglev > 0)
256 cout << " TOISeqBuffered::putData() - waitPut() " << getName()
257 << " i=" << i
258 << " next_in= " << next_in << " next_out=" << next_out << endl;
259 waitPut();
260 if (dbglev > 0)
261 cout << " ... Out of waitPut() i=" << i
262 << " next_in= " << next_in << " next_out=" << next_out << endl;
263 }
264 cleanWaitPut();
265 }
266 dataRef(i) = value;
267 flagRef(i) = flag;
268 next_in = i+1;
269 if (isGetWaiting() && (next_in-next_out > wsize/8)) {
270 if (dbglev > 0)
271 cout << " TOISeqBuffered::putData() - signalGet() name=" << getName()
272 << " i=" << i << " next_in= " << next_in
273 << " next_out=" << next_out << endl;
274 signalGet(); broadcast();
275 }
276}
277
278void TOISeqBuffered::putData(int i, int n, double const* val, uint_8 const* flg) {
279 if (!started) {
280 first_in = next_in = i;
281 next_out = next_in;
282 started = true;
283 }
284 else {
285 if (i != next_in) {
286 if (dbglev > 0)
287 cout << " TOISeqBuffered::putData(i,n ...) - i!=next_in() name=" << getName()
288 << " i=" << i << " next_in= " << next_in
289 << " next_out=" << next_out << endl;
290 string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName();
291 throw RangeCheckError(msg);
292 }
293 }
294 for(int j=i; j<i+n; j++) {
295 if (next_in-next_out >= wsize) {
296 if (dbglev > 0)
297 cout << " TOISeqBuffered::putData(i,n ...) - waitPut() " << getName()
298 << " j=" << j
299 << " next_in= " << next_in << " next_out=" << next_out << endl;
300 waitPut();
301 if (dbglev > 0)
302 cout << " ... Out of waitPut() j=" << j
303 << " next_in= " << next_in << " next_out=" << next_out << endl;
304 }
305 cleanWaitPut();
306 dataRef(j) = val[j-i];
307 if (flg) flagRef(j) = flg[j-i];
308 else flagRef(j) = 0;
309 next_in = j+1;
310 }
311 if (isGetWaiting() && (next_in-next_out > wsize/8)) {
312 if (dbglev > 0)
313 cout << " TOISeqBuffered::putData(i,n ...) - signalGet() name=" << getName()
314 << " i=" << i << " next_in= " << next_in
315 << " next_out=" << next_out << endl;
316 signalGet(); broadcast();
317 }
318}
319
320bool TOISeqBuffered::hasSomeData() {
321 lock();
322 bool x = started;
323 unlock();
324 return x;
325}
326
327int TOISeqBuffered::nextDataAvail(int iAfter) {
328 lock();
329 if (iAfter >= next_in ) {unlock(); return -1;}
330 if (iAfter < (next_in-buffsize)) {unlock(); return (next_in-buffsize);}
331 unlock();
332 return iAfter+1;
333}
334
335void TOISeqBuffered::doGetData(int i, double & val, uint_8 & flg)
336{
337 cerr << " TOISeqBuffered::doGetData() not implemented !"
338 << " \n A quoi ca set ??? Reza - Mai 2002 " << endl;
339 throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !");
340}
341
342void TOISeqBuffered::doPutData(int i, double value, uint_8 flag)
343{
344 cerr << " TOISeqBuffered::doGetData() not implemented !"
345 << " \n A quoi ca set ??? Reza - Mai 2002 " << endl;
346 throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !");
347}
Note: See TracBrowser for help on using the repository browser.