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

Last change on this file since 1442 was 1442, checked in by ansari, 25 years ago

Ajout de processeurs simples - Deglitcher + Filtre, + programme test - Reza 15/3/2001

File size: 6.0 KB
RevLine 
[1437]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;
[1442]65 if (isDataDeleted(iStart)) return DATA_DELETED;
[1437]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
98double TOISeqBuffered::doGetData(int i) {
99 if (!started) {
100 cout << " TOISeqBuffered::doGetData() - waitGet() Waiting for start ... " << endl;
101 waitGet();
102 }
103 cleanWaitGet();
[1442]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;
[1437]109 throw RangeCheckError("TOISeqBuffered::doGetData(i) : data deleted");
[1442]110 }
[1437]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 double dat = dataRef(i);
124 if (first_out < 0) first_out = i;
125 if ((i+1) > next_out) next_out = i+1;
126 if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
127 if (dbglev > 0)
128 cout << " TOISeqBuffered::doGetData() - signalPut() name=" << getName()
129 << " i=" << i << " next_in= " << next_in
130 << " next_out=" << next_out << endl;
131 signalPut();
132 }
133 return dat;
134}
135
136
137#ifndef NO_SOPHYA
138TArray<int_4> TOISeqBuffered::doGetFlag(int iStart, int iEnd) {
139 if (!started) waitGet();
140 cleanWaitGet();
141 if (!isDataAvailNL(iStart, iEnd))
142 throw RangeCheckError("TOISeqBuffered::getFlag(iS,iE) : data not available");
143 TVector<int_4> dat(iEnd - iStart + 1);
144 for (int i=0; i<iEnd-iStart+1; i++) {
145 dat[i] = flagRef(i+iStart);
146 }
147 return dat;
148}
149#endif
150
151int_4 TOISeqBuffered::doGetFlag(int i) {
152 if (!started) waitGet();
153 cleanWaitGet();
154 if (isDataDeleted(i))
155 throw RangeCheckError("TOISeqBuffered::doGetFlag(i) : data deleted");
156 while (i >= next_in) waitGet();
157 int_4 dat = flagRef(i);
158 return dat;
159}
160
161
162void TOISeqBuffered::doPutData(int i, double value, int_4 flag) {
163 if (!started) {
164 first_in = next_in = i;
165 next_out = next_in;
166 started = true;
167 }
168 else {
169 if (i != next_in) {
[1442]170 if (dbglev > 0)
171 cout << " TOISeqBuffered::doPutData() - i!=next_in() name=" << getName()
172 << " i=" << i << " next_in= " << next_in
173 << " next_out=" << next_out << endl;
[1437]174 string msg = "TOISeqBuffered::doPutData() : i!=next_in TOIname=" + getName();
175 throw RangeCheckError(msg);
176 }
177 if (next_in-next_out >= wsize) {
178 if (dbglev > 0)
179 cout << " TOISeqBuffered::doPutData() - waitPut() " << getName()
180 << " i=" << i
181 << " next_in= " << next_in << " next_out=" << next_out << endl;
182 waitPut();
183 if (dbglev > 0)
184 cout << " ... Out of waitPut() i=" << i
185 << " next_in= " << next_in << " next_out=" << next_out << endl;
186 }
187 cleanWaitPut();
188 }
189 dataRef(i) = value;
190 flagRef(i) = flag;
191 next_in = i+1;
192 if (isGetWaiting() && (next_in-next_out > wsize/8)) {
193 if (dbglev > 0)
194 cout << " TOISeqBuffered::doPutData() - signalGet() name=" << getName()
195 << " i=" << i << " next_in= " << next_in
196 << " next_out=" << next_out << endl;
197 signalGet();
198 }
199}
200
201bool TOISeqBuffered::hasSomeData() {
202 lock();
203 bool x = started;
204 unlock();
205 return x;
206}
207
208int TOISeqBuffered::nextDataAvail(int iAfter) {
209 lock();
210 if (iAfter >= next_in ) {unlock(); return -1;}
211 if (iAfter < (next_in-buffsize)) {unlock(); return (next_in-buffsize);}
212 unlock();
213 return iAfter+1;
214}
215
216/* A faire, le nettoyage (heuristique selon demandes ?, guide ? ) */
217
218
Note: See TracBrowser for help on using the repository browser.