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 |
|
---|
12 | TOISeqBuffered::TOISeqBuffered(int wsz) {
|
---|
13 | AllocBuffer(wsz);
|
---|
14 | }
|
---|
15 |
|
---|
16 | TOISeqBuffered::TOISeqBuffered(string nm, int wsz) {
|
---|
17 | AllocBuffer(wsz);
|
---|
18 | setName(nm);
|
---|
19 | }
|
---|
20 |
|
---|
21 | TOISeqBuffered::~TOISeqBuffered() {
|
---|
22 | delete[] data;
|
---|
23 | delete[] flags;
|
---|
24 | }
|
---|
25 |
|
---|
26 | void 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_8[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 |
|
---|
43 | void 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 |
|
---|
60 | TOI::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 |
|
---|
69 | TOI::DataStatus TOISeqBuffered::isDataAvailNL(int i) {
|
---|
70 | return TOI::isDataAvailNL(i);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void TOISeqBuffered::doWontNeedBefore(int i) {
|
---|
74 | next_out = i;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | #ifndef NO_SOPHYA
|
---|
79 | /* ---- l'interface va etre modifiee, NE PAS UTILISER
|
---|
80 | Array TOISeqBuffered::doGetData(int iStart, int iEnd) {
|
---|
81 | // if (iEnd < iStart)
|
---|
82 | // throw RangeCheckError("TOI::getData : iEnd<iStart !");
|
---|
83 | // if (iStart <= out_last)
|
---|
84 | if (!started) waitGet();
|
---|
85 | if (!isDataAvailNL(iStart, iEnd))
|
---|
86 | throw RangeCheckError("TOISeqBuffered::getData(iS,iE) : data not available");
|
---|
87 | cleanWaitGet();
|
---|
88 | Vector dat(iEnd - iStart + 1);
|
---|
89 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
90 | dat(i) = dataRef(i+iStart);
|
---|
91 | }
|
---|
92 | if (first_out < 0) first_out = iStart;
|
---|
93 | if ((iEnd+1) > next_out) next_out = iEnd+1;
|
---|
94 | if (isPutWaiting() && (next_in-next_out < wsize/2 )) signalPut();
|
---|
95 | return dat;
|
---|
96 | }
|
---|
97 | l'interface va etre modifiee, NE PAS UTILISER ---- */
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | void TOISeqBuffered::doGetData(int i, double & val, int_8 & flg) {
|
---|
101 | if (!started) {
|
---|
102 | cout << " TOISeqBuffered::doGetData() - waitGet() Waiting for start ... " << endl;
|
---|
103 | waitGet();
|
---|
104 | }
|
---|
105 | cleanWaitGet();
|
---|
106 | if (isDataDeleted(i)) {
|
---|
107 | if (dbglev > 0)
|
---|
108 | cout << " TOISeqBuffered::doGetData() - DataDeleted() name=" << getName()
|
---|
109 | << " i=" << i << " next_in= " << next_in
|
---|
110 | << " next_out=" << next_out << endl;
|
---|
111 | throw RangeCheckError("TOISeqBuffered::doGetData(i) : data deleted");
|
---|
112 | }
|
---|
113 | while (i >= next_in) {
|
---|
114 | if (i>next_out) next_out = i;
|
---|
115 | if (dbglev > 0)
|
---|
116 | cout << " TOISeqBuffered::doGetData() - waitGet() name=" << getName()
|
---|
117 | << " i=" << i << " next_in= " << next_in
|
---|
118 | << " next_out=" << next_out << endl;
|
---|
119 | waitGet();
|
---|
120 | if (dbglev > 0)
|
---|
121 | cout << " ... Out of waitGet() i=" << i
|
---|
122 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
123 | }
|
---|
124 | cleanWaitGet();
|
---|
125 | val = dataRef(i);
|
---|
126 | flg = flagRef(i);
|
---|
127 | if (first_out < 0) first_out = i;
|
---|
128 | if ((i+1) > next_out) next_out = i+1;
|
---|
129 | if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
|
---|
130 | if (dbglev > 0)
|
---|
131 | cout << " TOISeqBuffered::doGetData() - signalPut() name=" << getName()
|
---|
132 | << " i=" << i << " next_in= " << next_in
|
---|
133 | << " next_out=" << next_out << endl;
|
---|
134 | signalPut();
|
---|
135 | }
|
---|
136 | return;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | #ifndef NO_SOPHYA
|
---|
141 | /* ---- l'interface va etre modifiee, NE PAS UTILISER
|
---|
142 | TArray<int_4> TOISeqBuffered::doGetFlag(int iStart, int iEnd) {
|
---|
143 | if (!started) waitGet();
|
---|
144 | cleanWaitGet();
|
---|
145 | if (!isDataAvailNL(iStart, iEnd))
|
---|
146 | throw RangeCheckError("TOISeqBuffered::getFlag(iS,iE) : data not available");
|
---|
147 | TVector<int_4> dat(iEnd - iStart + 1);
|
---|
148 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
149 | dat[i] = flagRef(i+iStart);
|
---|
150 | }
|
---|
151 | return dat;
|
---|
152 | }
|
---|
153 | l'interface va etre modifiee, NE PAS UTILISER ---- */
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | /*RZCMV
|
---|
157 | int_4 TOISeqBuffered::doGetFlag(int i) {
|
---|
158 | if (!started) waitGet();
|
---|
159 | cleanWaitGet();
|
---|
160 | if (isDataDeleted(i))
|
---|
161 | throw RangeCheckError("TOISeqBuffered::doGetFlag(i) : data deleted");
|
---|
162 | while (i >= next_in) waitGet();
|
---|
163 | int_4 dat = flagRef(i);
|
---|
164 | return dat;
|
---|
165 | }
|
---|
166 | */
|
---|
167 |
|
---|
168 |
|
---|
169 | void TOISeqBuffered::doPutData(int i, double value, int_8 flag) {
|
---|
170 | if (!started) {
|
---|
171 | first_in = next_in = i;
|
---|
172 | next_out = next_in;
|
---|
173 | started = true;
|
---|
174 | }
|
---|
175 | else {
|
---|
176 | if (i != next_in) {
|
---|
177 | if (dbglev > 0)
|
---|
178 | cout << " TOISeqBuffered::doPutData() - i!=next_in() name=" << getName()
|
---|
179 | << " i=" << i << " next_in= " << next_in
|
---|
180 | << " next_out=" << next_out << endl;
|
---|
181 | string msg = "TOISeqBuffered::doPutData() : i!=next_in TOIname=" + getName();
|
---|
182 | throw RangeCheckError(msg);
|
---|
183 | }
|
---|
184 | if (next_in-next_out >= wsize) {
|
---|
185 | if (dbglev > 0)
|
---|
186 | cout << " TOISeqBuffered::doPutData() - waitPut() " << getName()
|
---|
187 | << " i=" << i
|
---|
188 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
189 | waitPut();
|
---|
190 | if (dbglev > 0)
|
---|
191 | cout << " ... Out of waitPut() i=" << i
|
---|
192 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
193 | }
|
---|
194 | cleanWaitPut();
|
---|
195 | }
|
---|
196 | dataRef(i) = value;
|
---|
197 | flagRef(i) = flag;
|
---|
198 | next_in = i+1;
|
---|
199 | if (isGetWaiting() && (next_in-next_out > wsize/8)) {
|
---|
200 | if (dbglev > 0)
|
---|
201 | cout << " TOISeqBuffered::doPutData() - signalGet() name=" << getName()
|
---|
202 | << " i=" << i << " next_in= " << next_in
|
---|
203 | << " next_out=" << next_out << endl;
|
---|
204 | signalGet();
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | bool TOISeqBuffered::hasSomeData() {
|
---|
209 | lock();
|
---|
210 | bool x = started;
|
---|
211 | unlock();
|
---|
212 | return x;
|
---|
213 | }
|
---|
214 |
|
---|
215 | int TOISeqBuffered::nextDataAvail(int iAfter) {
|
---|
216 | lock();
|
---|
217 | if (iAfter >= next_in ) {unlock(); return -1;}
|
---|
218 | if (iAfter < (next_in-buffsize)) {unlock(); return (next_in-buffsize);}
|
---|
219 | unlock();
|
---|
220 | return iAfter+1;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 |
|
---|