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

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

Ajout de lock()/unlock() ds toiseqbuff.cc - Reza 9/5/2002

File size: 10.4 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.13 2002-05-09 20:22:37 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 lock();
128 if (!started) {
129 cout << " TOISeqBuffered::getData() - waitGet() Waiting for start ... " << endl;
130 waitGet();
131 }
132 cleanWaitGet();
133 if (isDataDeleted(i)) {
134 if (dbglev > 0)
135 cout << " TOISeqBuffered::getData() - DataDeleted() name=" << getName()
136 << " i=" << i << " next_in= " << next_in
137 << " next_out=" << next_out << endl;
138 unlock();
139 throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted");
140 }
141 while (i >= next_in) {
142 if (i>next_out) next_out = i;
143 if (dbglev > 0)
144 cout << " TOISeqBuffered::getData() - waitGet() name=" << getName()
145 << " i=" << i << " next_in= " << next_in
146 << " next_out=" << next_out << endl;
147 waitGet();
148 if (dbglev > 0)
149 cout << " ... Out of waitGet() i=" << i
150 << " next_in= " << next_in << " next_out=" << next_out << endl;
151 }
152 cleanWaitGet();
153 val = dataRef(i);
154 flg = flagRef(i);
155 if (first_out < 0) first_out = i;
156 if ((i+1) > next_out) next_out = i+1;
157 if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
158 if (dbglev > 0)
159 cout << " TOISeqBuffered::getData() - signalPut() name=" << getName()
160 << " i=" << i << " next_in= " << next_in
161 << " next_out=" << next_out << endl;
162 signalPut();
163 }
164 unlock();
165 if (fgsigput) signal();
166 return;
167}
168
169
170void TOISeqBuffered::getData(int i, int n, double* data, uint_8* flg)
171{
172 lock();
173 if (!started) {
174 cout << " TOISeqBuffered::getData(i,n ...) - waitGet() Waiting for start ... " << endl;
175 waitGet();
176 }
177 cleanWaitGet();
178 if (isDataDeleted(i)) {
179 if (dbglev > 0)
180 cout << " TOISeqBuffered::getData(i,n ...) - DataDeleted() name=" << getName()
181 << " i=" << i << " next_in= " << next_in
182 << " next_out=" << next_out << endl;
183 unlock();
184 throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted");
185 }
186 for(int j=i; i<i+n; j++) {
187 while (j >= next_in) {
188 if (j>next_out) next_out = j;
189 if (dbglev > 0)
190 cout << " TOISeqBuffered::getData(i,n ...) - waitGet() name=" << getName()
191 << " j=" << j << " next_in= " << next_in
192 << " next_out=" << next_out << endl;
193 waitGet();
194 if (dbglev > 0)
195 cout << " ... Out of waitGet() j=" << j
196 << " next_in= " << next_in << " next_out=" << next_out << endl;
197 }
198 cleanWaitGet();
199 data[j-i] = dataRef(j);
200 if (flg) flg[j-i] = flagRef(j);
201 if (first_out < 0) first_out = j;
202 if ((j+1) > next_out) next_out = j+1;
203 if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
204 if (dbglev > 0)
205 cout << " TOISeqBuffered::getData(i,n ...) - signalPut() name=" << getName()
206 << " i=" << i << " next_in= " << next_in
207 << " next_out=" << next_out << endl;
208 signalPut();
209 }
210 }
211 unlock();
212 if (fgsigput) signal();
213 return;
214}
215
216
217#ifndef NO_SOPHYA
218/* ---- l'interface va etre modifiee, NE PAS UTILISER
219TArray<int_4> TOISeqBuffered::doGetFlag(int iStart, int iEnd) {
220 if (!started) waitGet();
221 cleanWaitGet();
222 if (!isDataAvailNL(iStart, iEnd))
223 throw RangeCheckError("TOISeqBuffered::getFlag(iS,iE) : data not available");
224 TVector<int_4> dat(iEnd - iStart + 1);
225 for (int i=0; i<iEnd-iStart+1; i++) {
226 dat[i] = flagRef(i+iStart);
227 }
228 return dat;
229}
230 l'interface va etre modifiee, NE PAS UTILISER ---- */
231#endif
232
233/*RZCMV
234int_4 TOISeqBuffered::doGetFlag(int i) {
235 if (!started) waitGet();
236 cleanWaitGet();
237 if (isDataDeleted(i))
238 throw RangeCheckError("TOISeqBuffered::doGetFlag(i) : data deleted");
239 while (i >= next_in) waitGet();
240 int_4 dat = flagRef(i);
241 return dat;
242}
243*/
244
245
246void TOISeqBuffered::putData(int i, double value, uint_8 flag) {
247 lock();
248 if (!started) {
249 first_in = next_in = i;
250 next_out = next_in;
251 started = true;
252 }
253 else {
254 if (i != next_in) {
255 if (dbglev > 0)
256 cout << " TOISeqBuffered::putData() - i!=next_in() name=" << getName()
257 << " i=" << i << " next_in= " << next_in
258 << " next_out=" << next_out << endl;
259 string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName();
260 unlock();
261 throw RangeCheckError(msg);
262 }
263 if (next_in-next_out >= wsize) {
264 if (dbglev > 0)
265 cout << " TOISeqBuffered::putData() - waitPut() " << getName()
266 << " i=" << i
267 << " next_in= " << next_in << " next_out=" << next_out << endl;
268 waitPut();
269 if (dbglev > 0)
270 cout << " ... Out of waitPut() i=" << i
271 << " next_in= " << next_in << " next_out=" << next_out << endl;
272 }
273 cleanWaitPut();
274 }
275 dataRef(i) = value;
276 flagRef(i) = flag;
277 next_in = i+1;
278 if (isGetWaiting() && (next_in-next_out > wsize/8)) {
279 if (dbglev > 0)
280 cout << " TOISeqBuffered::putData() - signalGet() name=" << getName()
281 << " i=" << i << " next_in= " << next_in
282 << " next_out=" << next_out << endl;
283 signalGet();
284 }
285 unlock();
286 if (fgsigget) signal();
287 return;
288}
289
290void TOISeqBuffered::putData(int i, int n, double const* val, uint_8 const* flg)
291{
292 lock();
293 if (!started) {
294 first_in = next_in = i;
295 next_out = next_in;
296 started = true;
297 }
298 else {
299 if (i != next_in) {
300 if (dbglev > 0)
301 cout << " TOISeqBuffered::putData(i,n ...) - i!=next_in() name=" << getName()
302 << " i=" << i << " next_in= " << next_in
303 << " next_out=" << next_out << endl;
304 string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName();
305 unlock();
306 throw RangeCheckError(msg);
307 }
308 }
309 for(int j=i; j<i+n; j++) {
310 if (next_in-next_out >= wsize) {
311 if (dbglev > 0)
312 cout << " TOISeqBuffered::putData(i,n ...) - waitPut() " << getName()
313 << " j=" << j
314 << " next_in= " << next_in << " next_out=" << next_out << endl;
315 waitPut();
316 if (dbglev > 0)
317 cout << " ... Out of waitPut() j=" << j
318 << " next_in= " << next_in << " next_out=" << next_out << endl;
319 }
320 cleanWaitPut();
321 dataRef(j) = val[j-i];
322 if (flg) flagRef(j) = flg[j-i];
323 else flagRef(j) = 0;
324 next_in = j+1;
325 }
326 if (isGetWaiting() && (next_in-next_out > wsize/8)) {
327 if (dbglev > 0)
328 cout << " TOISeqBuffered::putData(i,n ...) - signalGet() name=" << getName()
329 << " i=" << i << " next_in= " << next_in
330 << " next_out=" << next_out << endl;
331 signalGet();
332 }
333 unlock();
334 if (fgsigget) signal();
335 return;
336}
337
338bool TOISeqBuffered::hasSomeData() {
339 lock();
340 bool x = started;
341 unlock();
342 return x;
343}
344
345int TOISeqBuffered::nextDataAvail(int iAfter) {
346 lock();
347 if (iAfter >= next_in ) {unlock(); return -1;}
348 if (iAfter < (next_in-buffsize)) {unlock(); return (next_in-buffsize);}
349 unlock();
350 return iAfter+1;
351}
352
353void TOISeqBuffered::doGetData(int i, double & val, uint_8 & flg)
354{
355 cerr << " TOISeqBuffered::doGetData() not implemented !"
356 << " \n A quoi ca set ??? Reza - Mai 2002 " << endl;
357 throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !");
358}
359
360void TOISeqBuffered::doPutData(int i, double value, uint_8 flag)
361{
362 cerr << " TOISeqBuffered::doGetData() not implemented !"
363 << " \n A quoi ca set ??? Reza - Mai 2002 " << endl;
364 throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !");
365}
Note: See TracBrowser for help on using the repository browser.