[1738] | 1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
| 2 | // Eric Aubourg
|
---|
| 3 | // Christophe Magneville
|
---|
| 4 | // Reza Ansari
|
---|
[1991] | 5 | // $Id: toiseqbuff.cc,v 1.11 2002-05-08 21:48:38 ansari Exp $
|
---|
[1738] | 6 |
|
---|
[1437] | 7 | #include "toiprocessor.h"
|
---|
| 8 | #include "toiseqbuff.h"
|
---|
| 9 | #include <pthread.h>
|
---|
| 10 |
|
---|
| 11 | #ifdef WITH_SOPHYA
|
---|
| 12 | #include "pexceptions.h"
|
---|
| 13 | #else
|
---|
| 14 | #include "apexceptions.h"
|
---|
| 15 | #endif
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | TOISeqBuffered::TOISeqBuffered(int wsz) {
|
---|
[1484] | 19 | data = NULL;
|
---|
| 20 | flags = NULL;
|
---|
[1437] | 21 | AllocBuffer(wsz);
|
---|
[1484] | 22 | setName("toiseqbuff");
|
---|
[1437] | 23 | }
|
---|
| 24 |
|
---|
| 25 | TOISeqBuffered::TOISeqBuffered(string nm, int wsz) {
|
---|
[1484] | 26 | data = NULL;
|
---|
| 27 | flags = NULL;
|
---|
[1437] | 28 | AllocBuffer(wsz);
|
---|
| 29 | setName(nm);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | TOISeqBuffered::~TOISeqBuffered() {
|
---|
| 33 | delete[] data;
|
---|
| 34 | delete[] flags;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | void TOISeqBuffered::AllocBuffer(int wsz)
|
---|
| 38 | {
|
---|
| 39 | if (wsz < 128) wsz = 128;
|
---|
| 40 | wsize = wsz;
|
---|
| 41 | buffsize = 2*wsz;
|
---|
[1484] | 42 | if (data) delete[] data;
|
---|
| 43 | if (flags) delete[] flags;
|
---|
[1437] | 44 | data = new double[buffsize];
|
---|
[1532] | 45 | flags = new uint_8[buffsize];
|
---|
[1437] | 46 | for(int k=0; k<buffsize; k++) {
|
---|
| 47 | data[k] = defaultValue;
|
---|
| 48 | flags[k] = 0;
|
---|
| 49 | }
|
---|
| 50 | next_in = next_out = -1;
|
---|
| 51 | first_in = first_out = -1;
|
---|
| 52 | started = false;
|
---|
| 53 | dbglev = 0;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[1762] | 56 | void TOISeqBuffered::PrintStatus(::ostream & os) const
|
---|
[1437] | 57 | {
|
---|
[1484] | 58 | os << "---TOISeqBuffered::PrintStatus() - Name=" << getName()
|
---|
| 59 | << "\n WindowSize= " << wsize << " BufferSize= " << buffsize << endl;
|
---|
[1437] | 60 | os << "Index: FirstIn= " << getFirstIn() << " LastIn= " << getLastIn()
|
---|
| 61 | << " Total= " << getLastIn()-getFirstIn()+1 << endl;
|
---|
| 62 | os << "Index: FirstOut= " << getFirstOut() << " LastOut= " << getLastOut()
|
---|
| 63 | << " Total= " << getLastOut()-getFirstOut()+1 << endl;
|
---|
| 64 | os << " WaitStatus: Put/" ;
|
---|
| 65 | if (isPutWaiting()) os << "Waiting " ;
|
---|
| 66 | else os << "Running ";
|
---|
| 67 | os << " PutCountWait= " << getCountWaitPut() << endl;
|
---|
| 68 | os << " WaitStatus: Get/" ;
|
---|
| 69 | if (isGetWaiting()) os << "Waiting " ;
|
---|
| 70 | else os << "Running ";
|
---|
| 71 | os << " GetCountWait= " << getCountWaitGet() << endl;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | TOI::DataStatus TOISeqBuffered::isDataAvailNL(int iStart, int iEnd) {
|
---|
| 75 | if (iEnd < iStart)
|
---|
| 76 | throw RangeCheckError("TOISeqBuffered::isDataAvailNL : iEnd<iStart !");
|
---|
| 77 | if (!started) return DATA_NOT_YET;
|
---|
| 78 | if (iEnd >= next_in) return DATA_NOT_YET;
|
---|
[1442] | 79 | if (isDataDeleted(iStart)) return DATA_DELETED;
|
---|
[1437] | 80 | return DATA_OK;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | TOI::DataStatus TOISeqBuffered::isDataAvailNL(int i) {
|
---|
| 84 | return TOI::isDataAvailNL(i);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[1985] | 87 | void TOISeqBuffered::wontNeedBefore(int i) {
|
---|
[1484] | 88 | // $CHECK$ Reza 30/4/2001 - Je ne sais pas a quoi ca sert !
|
---|
| 89 | // next_out = i; $CHECK$ Reza 30/4/2001
|
---|
[1437] | 90 | }
|
---|
| 91 |
|
---|
| 92 |
|
---|
| 93 | #ifndef NO_SOPHYA
|
---|
[1464] | 94 | /* ---- l'interface va etre modifiee, NE PAS UTILISER
|
---|
[1437] | 95 | Array TOISeqBuffered::doGetData(int iStart, int iEnd) {
|
---|
| 96 | // if (iEnd < iStart)
|
---|
| 97 | // throw RangeCheckError("TOI::getData : iEnd<iStart !");
|
---|
| 98 | // if (iStart <= out_last)
|
---|
| 99 | if (!started) waitGet();
|
---|
| 100 | if (!isDataAvailNL(iStart, iEnd))
|
---|
| 101 | throw RangeCheckError("TOISeqBuffered::getData(iS,iE) : data not available");
|
---|
| 102 | cleanWaitGet();
|
---|
| 103 | Vector dat(iEnd - iStart + 1);
|
---|
| 104 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
| 105 | dat(i) = dataRef(i+iStart);
|
---|
| 106 | }
|
---|
| 107 | if (first_out < 0) first_out = iStart;
|
---|
| 108 | if ((iEnd+1) > next_out) next_out = iEnd+1;
|
---|
| 109 | if (isPutWaiting() && (next_in-next_out < wsize/2 )) signalPut();
|
---|
| 110 | return dat;
|
---|
| 111 | }
|
---|
[1464] | 112 | l'interface va etre modifiee, NE PAS UTILISER ---- */
|
---|
[1437] | 113 | #endif
|
---|
| 114 |
|
---|
[1985] | 115 | double TOISeqBuffered::getData(int i)
|
---|
| 116 | {
|
---|
| 117 | double val;
|
---|
| 118 | uint_8 flg;
|
---|
| 119 | getData(i, val, flg);
|
---|
| 120 | return(val);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | void TOISeqBuffered::getData(int i, double & val, uint_8 & flg) {
|
---|
[1437] | 125 | if (!started) {
|
---|
[1985] | 126 | cout << " TOISeqBuffered::getData() - waitGet() Waiting for start ... " << endl;
|
---|
[1437] | 127 | waitGet();
|
---|
| 128 | }
|
---|
| 129 | cleanWaitGet();
|
---|
[1442] | 130 | if (isDataDeleted(i)) {
|
---|
| 131 | if (dbglev > 0)
|
---|
[1985] | 132 | cout << " TOISeqBuffered::getData() - DataDeleted() name=" << getName()
|
---|
[1442] | 133 | << " i=" << i << " next_in= " << next_in
|
---|
| 134 | << " next_out=" << next_out << endl;
|
---|
[1985] | 135 | throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted");
|
---|
[1442] | 136 | }
|
---|
[1437] | 137 | while (i >= next_in) {
|
---|
| 138 | if (i>next_out) next_out = i;
|
---|
| 139 | if (dbglev > 0)
|
---|
[1985] | 140 | cout << " TOISeqBuffered::getData() - waitGet() name=" << getName()
|
---|
[1437] | 141 | << " i=" << i << " next_in= " << next_in
|
---|
| 142 | << " next_out=" << next_out << endl;
|
---|
| 143 | waitGet();
|
---|
| 144 | if (dbglev > 0)
|
---|
| 145 | cout << " ... Out of waitGet() i=" << i
|
---|
| 146 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
| 147 | }
|
---|
| 148 | cleanWaitGet();
|
---|
[1462] | 149 | val = dataRef(i);
|
---|
| 150 | flg = flagRef(i);
|
---|
[1437] | 151 | if (first_out < 0) first_out = i;
|
---|
| 152 | if ((i+1) > next_out) next_out = i+1;
|
---|
| 153 | if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
|
---|
| 154 | if (dbglev > 0)
|
---|
[1985] | 155 | cout << " TOISeqBuffered::getData() - signalPut() name=" << getName()
|
---|
[1437] | 156 | << " i=" << i << " next_in= " << next_in
|
---|
| 157 | << " next_out=" << next_out << endl;
|
---|
[1991] | 158 | signalPut(); broadcast();
|
---|
[1437] | 159 | }
|
---|
[1462] | 160 | return;
|
---|
[1437] | 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
[1985] | 164 | void TOISeqBuffered::getData(int i, int n, double* data, uint_8* flg)
|
---|
| 165 | {
|
---|
| 166 | if (!started) {
|
---|
| 167 | cout << " TOISeqBuffered::getData(i,n ...) - waitGet() Waiting for start ... " << endl;
|
---|
| 168 | waitGet();
|
---|
| 169 | }
|
---|
| 170 | cleanWaitGet();
|
---|
| 171 | if (isDataDeleted(i)) {
|
---|
| 172 | if (dbglev > 0)
|
---|
| 173 | cout << " TOISeqBuffered::getData(i,n ...) - DataDeleted() name=" << getName()
|
---|
| 174 | << " i=" << i << " next_in= " << next_in
|
---|
| 175 | << " next_out=" << next_out << endl;
|
---|
| 176 | throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted");
|
---|
| 177 | }
|
---|
| 178 | for(int j=i; i<i+n; j++) {
|
---|
| 179 | while (j >= next_in) {
|
---|
| 180 | if (j>next_out) next_out = j;
|
---|
| 181 | if (dbglev > 0)
|
---|
| 182 | cout << " TOISeqBuffered::getData(i,n ...) - waitGet() name=" << getName()
|
---|
| 183 | << " j=" << j << " next_in= " << next_in
|
---|
| 184 | << " next_out=" << next_out << endl;
|
---|
| 185 | waitGet();
|
---|
| 186 | if (dbglev > 0)
|
---|
| 187 | cout << " ... Out of waitGet() j=" << j
|
---|
| 188 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
| 189 | }
|
---|
| 190 | cleanWaitGet();
|
---|
| 191 | data[j-i] = dataRef(j);
|
---|
| 192 | if (flg) flg[j-i] = flagRef(j);
|
---|
| 193 | if (first_out < 0) first_out = j;
|
---|
| 194 | if ((j+1) > next_out) next_out = j+1;
|
---|
| 195 | if (isPutWaiting() && (next_in-next_out < wsize/2 )) {
|
---|
| 196 | if (dbglev > 0)
|
---|
| 197 | cout << " TOISeqBuffered::getData(i,n ...) - signalPut() name=" << getName()
|
---|
| 198 | << " i=" << i << " next_in= " << next_in
|
---|
| 199 | << " next_out=" << next_out << endl;
|
---|
[1991] | 200 | signalPut(); broadcast();
|
---|
[1985] | 201 | }
|
---|
| 202 | }
|
---|
| 203 | return;
|
---|
| 204 |
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 |
|
---|
[1437] | 208 | #ifndef NO_SOPHYA
|
---|
[1464] | 209 | /* ---- l'interface va etre modifiee, NE PAS UTILISER
|
---|
[1437] | 210 | TArray<int_4> TOISeqBuffered::doGetFlag(int iStart, int iEnd) {
|
---|
| 211 | if (!started) waitGet();
|
---|
| 212 | cleanWaitGet();
|
---|
| 213 | if (!isDataAvailNL(iStart, iEnd))
|
---|
| 214 | throw RangeCheckError("TOISeqBuffered::getFlag(iS,iE) : data not available");
|
---|
| 215 | TVector<int_4> dat(iEnd - iStart + 1);
|
---|
| 216 | for (int i=0; i<iEnd-iStart+1; i++) {
|
---|
| 217 | dat[i] = flagRef(i+iStart);
|
---|
| 218 | }
|
---|
| 219 | return dat;
|
---|
| 220 | }
|
---|
[1464] | 221 | l'interface va etre modifiee, NE PAS UTILISER ---- */
|
---|
[1437] | 222 | #endif
|
---|
| 223 |
|
---|
[1462] | 224 | /*RZCMV
|
---|
[1437] | 225 | int_4 TOISeqBuffered::doGetFlag(int i) {
|
---|
| 226 | if (!started) waitGet();
|
---|
| 227 | cleanWaitGet();
|
---|
| 228 | if (isDataDeleted(i))
|
---|
| 229 | throw RangeCheckError("TOISeqBuffered::doGetFlag(i) : data deleted");
|
---|
| 230 | while (i >= next_in) waitGet();
|
---|
| 231 | int_4 dat = flagRef(i);
|
---|
| 232 | return dat;
|
---|
| 233 | }
|
---|
[1462] | 234 | */
|
---|
[1437] | 235 |
|
---|
| 236 |
|
---|
[1985] | 237 | void TOISeqBuffered::putData(int i, double value, uint_8 flag) {
|
---|
[1437] | 238 | if (!started) {
|
---|
| 239 | first_in = next_in = i;
|
---|
| 240 | next_out = next_in;
|
---|
| 241 | started = true;
|
---|
| 242 | }
|
---|
| 243 | else {
|
---|
| 244 | if (i != next_in) {
|
---|
[1442] | 245 | if (dbglev > 0)
|
---|
[1985] | 246 | cout << " TOISeqBuffered::putData() - i!=next_in() name=" << getName()
|
---|
[1442] | 247 | << " i=" << i << " next_in= " << next_in
|
---|
| 248 | << " next_out=" << next_out << endl;
|
---|
[1985] | 249 | string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName();
|
---|
[1437] | 250 | throw RangeCheckError(msg);
|
---|
| 251 | }
|
---|
| 252 | if (next_in-next_out >= wsize) {
|
---|
| 253 | if (dbglev > 0)
|
---|
[1985] | 254 | cout << " TOISeqBuffered::putData() - waitPut() " << getName()
|
---|
[1437] | 255 | << " i=" << i
|
---|
| 256 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
| 257 | waitPut();
|
---|
| 258 | if (dbglev > 0)
|
---|
| 259 | cout << " ... Out of waitPut() i=" << i
|
---|
| 260 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
| 261 | }
|
---|
| 262 | cleanWaitPut();
|
---|
| 263 | }
|
---|
| 264 | dataRef(i) = value;
|
---|
| 265 | flagRef(i) = flag;
|
---|
| 266 | next_in = i+1;
|
---|
| 267 | if (isGetWaiting() && (next_in-next_out > wsize/8)) {
|
---|
| 268 | if (dbglev > 0)
|
---|
[1985] | 269 | cout << " TOISeqBuffered::putData() - signalGet() name=" << getName()
|
---|
[1437] | 270 | << " i=" << i << " next_in= " << next_in
|
---|
| 271 | << " next_out=" << next_out << endl;
|
---|
[1991] | 272 | signalGet(); broadcast();
|
---|
[1437] | 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
[1985] | 276 | void TOISeqBuffered::putData(int i, int n, double const* val, uint_8 const* flg) {
|
---|
| 277 | if (!started) {
|
---|
| 278 | first_in = next_in = i;
|
---|
| 279 | next_out = next_in;
|
---|
| 280 | started = true;
|
---|
| 281 | }
|
---|
| 282 | else {
|
---|
| 283 | if (i != next_in) {
|
---|
| 284 | if (dbglev > 0)
|
---|
| 285 | cout << " TOISeqBuffered::putData(i,n ...) - i!=next_in() name=" << getName()
|
---|
| 286 | << " i=" << i << " next_in= " << next_in
|
---|
| 287 | << " next_out=" << next_out << endl;
|
---|
| 288 | string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName();
|
---|
| 289 | throw RangeCheckError(msg);
|
---|
| 290 | }
|
---|
| 291 | }
|
---|
| 292 | for(int j=i; j<i+n; j++) {
|
---|
| 293 | if (next_in-next_out >= wsize) {
|
---|
| 294 | if (dbglev > 0)
|
---|
| 295 | cout << " TOISeqBuffered::putData(i,n ...) - waitPut() " << getName()
|
---|
| 296 | << " j=" << j
|
---|
| 297 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
| 298 | waitPut();
|
---|
| 299 | if (dbglev > 0)
|
---|
| 300 | cout << " ... Out of waitPut() j=" << j
|
---|
| 301 | << " next_in= " << next_in << " next_out=" << next_out << endl;
|
---|
| 302 | }
|
---|
| 303 | cleanWaitPut();
|
---|
| 304 | dataRef(j) = val[j-i];
|
---|
| 305 | if (flg) flagRef(j) = flg[j-i];
|
---|
| 306 | else flagRef(j) = 0;
|
---|
| 307 | next_in = j+1;
|
---|
| 308 | }
|
---|
| 309 | if (isGetWaiting() && (next_in-next_out > wsize/8)) {
|
---|
| 310 | if (dbglev > 0)
|
---|
| 311 | cout << " TOISeqBuffered::putData(i,n ...) - signalGet() name=" << getName()
|
---|
| 312 | << " i=" << i << " next_in= " << next_in
|
---|
| 313 | << " next_out=" << next_out << endl;
|
---|
[1991] | 314 | signalGet(); broadcast();
|
---|
[1985] | 315 | }
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[1437] | 318 | bool TOISeqBuffered::hasSomeData() {
|
---|
| 319 | lock();
|
---|
| 320 | bool x = started;
|
---|
| 321 | unlock();
|
---|
| 322 | return x;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | int TOISeqBuffered::nextDataAvail(int iAfter) {
|
---|
| 326 | lock();
|
---|
| 327 | if (iAfter >= next_in ) {unlock(); return -1;}
|
---|
| 328 | if (iAfter < (next_in-buffsize)) {unlock(); return (next_in-buffsize);}
|
---|
| 329 | unlock();
|
---|
| 330 | return iAfter+1;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[1985] | 333 | void TOISeqBuffered::doGetData(int i, double & val, uint_8 & flg)
|
---|
| 334 | {
|
---|
| 335 | cerr << " TOISeqBuffered::doGetData() not implemented !"
|
---|
| 336 | << " \n A quoi ca set ??? Reza - Mai 2002 " << endl;
|
---|
| 337 | throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !");
|
---|
| 338 | }
|
---|
[1437] | 339 |
|
---|
[1985] | 340 | void TOISeqBuffered::doPutData(int i, double value, uint_8 flag)
|
---|
| 341 | {
|
---|
| 342 | cerr << " TOISeqBuffered::doGetData() not implemented !"
|
---|
| 343 | << " \n A quoi ca set ??? Reza - Mai 2002 " << endl;
|
---|
| 344 | throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !");
|
---|
| 345 | }
|
---|