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

Last change on this file since 1964 was 1762, checked in by aubourg, 24 years ago

toujours et encore magique

File size: 6.7 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.9 2001-11-13 16:22:47 aubourg 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}
24
25TOISeqBuffered::TOISeqBuffered(string nm, int wsz) {
26 data = NULL;
27 flags = NULL;
28 AllocBuffer(wsz);
29 setName(nm);
30}
31
32TOISeqBuffered::~TOISeqBuffered() {
33 delete[] data;
34 delete[] flags;
35}
36
37void TOISeqBuffered::AllocBuffer(int wsz)
38{
39 if (wsz < 128) wsz = 128;
40 wsize = wsz;
41 buffsize = 2*wsz;
42 if (data) delete[] data;
43 if (flags) delete[] flags;
44 data = new double[buffsize];
45 flags = new uint_8[buffsize];
46 for(int k=0; k<buffsize; k++) {
47 data[k] = defaultValue;
48 flags[k] = 0;
49 }
50 next_in = next_out = -1;
51 first_in = first_out = -1;
52 started = false;
53 dbglev = 0;
54}
55
56void TOISeqBuffered::PrintStatus(::ostream & os) const
57{
58 os << "---TOISeqBuffered::PrintStatus() - Name=" << getName()
59 << "\n WindowSize= " << wsize << " BufferSize= " << buffsize << endl;
60 os << "Index: FirstIn= " << getFirstIn() << " LastIn= " << getLastIn()
61 << " Total= " << getLastIn()-getFirstIn()+1 << endl;
62 os << "Index: FirstOut= " << getFirstOut() << " LastOut= " << getLastOut()
63 << " Total= " << getLastOut()-getFirstOut()+1 << endl;
64 os << " WaitStatus: Put/" ;
65 if (isPutWaiting()) os << "Waiting " ;
66 else os << "Running ";
67 os << " PutCountWait= " << getCountWaitPut() << endl;
68 os << " WaitStatus: Get/" ;
69 if (isGetWaiting()) os << "Waiting " ;
70 else os << "Running ";
71 os << " GetCountWait= " << getCountWaitGet() << endl;
72}
73
74TOI::DataStatus TOISeqBuffered::isDataAvailNL(int iStart, int iEnd) {
75 if (iEnd < iStart)
76 throw RangeCheckError("TOISeqBuffered::isDataAvailNL : iEnd<iStart !");
77 if (!started) return DATA_NOT_YET;
78 if (iEnd >= next_in) return DATA_NOT_YET;
79 if (isDataDeleted(iStart)) return DATA_DELETED;
80 return DATA_OK;
81}
82
83TOI::DataStatus TOISeqBuffered::isDataAvailNL(int i) {
84 return TOI::isDataAvailNL(i);
85}
86
87void TOISeqBuffered::doWontNeedBefore(int i) {
88 // $CHECK$ Reza 30/4/2001 - Je ne sais pas a quoi ca sert !
89 // next_out = i; $CHECK$ Reza 30/4/2001
90}
91
92
93#ifndef NO_SOPHYA
94/* ---- l'interface va etre modifiee, NE PAS UTILISER
95Array TOISeqBuffered::doGetData(int iStart, int iEnd) {
96 // if (iEnd < iStart)
97 // throw RangeCheckError("TOI::getData : iEnd<iStart !");
98 // if (iStart <= out_last)
99 if (!started) waitGet();
100 if (!isDataAvailNL(iStart, iEnd))
101 throw RangeCheckError("TOISeqBuffered::getData(iS,iE) : data not available");
102 cleanWaitGet();
103 Vector dat(iEnd - iStart + 1);
104 for (int i=0; i<iEnd-iStart+1; i++) {
105 dat(i) = dataRef(i+iStart);
106 }
107 if (first_out < 0) first_out = iStart;
108 if ((iEnd+1) > next_out) next_out = iEnd+1;
109 if (isPutWaiting() && (next_in-next_out < wsize/2 )) signalPut();
110 return dat;
111}
112 l'interface va etre modifiee, NE PAS UTILISER ---- */
113#endif
114
115void TOISeqBuffered::doGetData(int i, double & val, uint_8 & flg) {
116 if (!started) {
117 cout << " TOISeqBuffered::doGetData() - waitGet() Waiting for start ... " << endl;
118 waitGet();
119 }
120 cleanWaitGet();
121 if (isDataDeleted(i)) {
122 if (dbglev > 0)
123 cout << " TOISeqBuffered::doGetData() - DataDeleted() name=" << getName()
124 << " i=" << i << " next_in= " << next_in
125 << " next_out=" << next_out << endl;
126 throw RangeCheckError("TOISeqBuffered::doGetData(i) : data deleted");
127 }
128 while (i >= next_in) {
129 if (i>next_out) next_out = i;
130 if (dbglev > 0)
131 cout << " TOISeqBuffered::doGetData() - waitGet() name=" << getName()
132 << " i=" << i << " next_in= " << next_in
133 << " next_out=" << next_out << endl;
134 waitGet();
135 if (dbglev > 0)
136 cout << " ... Out of waitGet() i=" << i
137 << " next_in= " << next_in << " next_out=" << next_out << endl;
138 }
139 cleanWaitGet();
140 val = dataRef(i);
141 flg = flagRef(i);
142 if (first_out < 0) first_out = i;
143 if ((i+1) > next_out) next_out = i+1;
144 if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
145 if (dbglev > 0)
146 cout << " TOISeqBuffered::doGetData() - signalPut() name=" << getName()
147 << " i=" << i << " next_in= " << next_in
148 << " next_out=" << next_out << endl;
149 signalPut();
150 }
151 return;
152}
153
154
155#ifndef NO_SOPHYA
156/* ---- l'interface va etre modifiee, NE PAS UTILISER
157TArray<int_4> TOISeqBuffered::doGetFlag(int iStart, int iEnd) {
158 if (!started) waitGet();
159 cleanWaitGet();
160 if (!isDataAvailNL(iStart, iEnd))
161 throw RangeCheckError("TOISeqBuffered::getFlag(iS,iE) : data not available");
162 TVector<int_4> dat(iEnd - iStart + 1);
163 for (int i=0; i<iEnd-iStart+1; i++) {
164 dat[i] = flagRef(i+iStart);
165 }
166 return dat;
167}
168 l'interface va etre modifiee, NE PAS UTILISER ---- */
169#endif
170
171/*RZCMV
172int_4 TOISeqBuffered::doGetFlag(int i) {
173 if (!started) waitGet();
174 cleanWaitGet();
175 if (isDataDeleted(i))
176 throw RangeCheckError("TOISeqBuffered::doGetFlag(i) : data deleted");
177 while (i >= next_in) waitGet();
178 int_4 dat = flagRef(i);
179 return dat;
180}
181*/
182
183
184void TOISeqBuffered::doPutData(int i, double value, uint_8 flag) {
185 if (!started) {
186 first_in = next_in = i;
187 next_out = next_in;
188 started = true;
189 }
190 else {
191 if (i != next_in) {
192 if (dbglev > 0)
193 cout << " TOISeqBuffered::doPutData() - i!=next_in() name=" << getName()
194 << " i=" << i << " next_in= " << next_in
195 << " next_out=" << next_out << endl;
196 string msg = "TOISeqBuffered::doPutData() : i!=next_in TOIname=" + getName();
197 throw RangeCheckError(msg);
198 }
199 if (next_in-next_out >= wsize) {
200 if (dbglev > 0)
201 cout << " TOISeqBuffered::doPutData() - waitPut() " << getName()
202 << " i=" << i
203 << " next_in= " << next_in << " next_out=" << next_out << endl;
204 waitPut();
205 if (dbglev > 0)
206 cout << " ... Out of waitPut() i=" << i
207 << " next_in= " << next_in << " next_out=" << next_out << endl;
208 }
209 cleanWaitPut();
210 }
211 dataRef(i) = value;
212 flagRef(i) = flag;
213 next_in = i+1;
214 if (isGetWaiting() && (next_in-next_out > wsize/8)) {
215 if (dbglev > 0)
216 cout << " TOISeqBuffered::doPutData() - signalGet() name=" << getName()
217 << " i=" << i << " next_in= " << next_in
218 << " next_out=" << next_out << endl;
219 signalGet();
220 }
221}
222
223bool TOISeqBuffered::hasSomeData() {
224 lock();
225 bool x = started;
226 unlock();
227 return x;
228}
229
230int TOISeqBuffered::nextDataAvail(int iAfter) {
231 lock();
232 if (iAfter >= next_in ) {unlock(); return -1;}
233 if (iAfter < (next_in-buffsize)) {unlock(); return (next_in-buffsize);}
234 unlock();
235 return iAfter+1;
236}
237
238
239
Note: See TracBrowser for help on using the repository browser.