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

Last change on this file since 1462 was 1462, checked in by cmv, 24 years ago

changement getData... intermediaire NE COMPILE PAS cmv+rz 10/4/2001

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