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