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

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

Protections diverses dans TOIProcessor et FITSTOIReader/Writer
Ajout d'un TOI (TOISeqBuffered) avec gestion d'un buffer entre put/get
Ajout de processeurs de test (RZTOIProc...) , programme test associe
et SMakefile (pour compil avec SOPHYA)

Reza 12/3/2001

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