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

Last change on this file since 1700 was 1532, checked in by aubourg, 24 years ago

flags uint_8

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