// ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL // Eric Aubourg // Christophe Magneville // Reza Ansari // $Id: toiseqbuff.cc,v 1.14 2002-05-30 08:41:25 ansari Exp $ #include "toiprocessor.h" #include "toiseqbuff.h" #include #ifdef WITH_SOPHYA #include "pexceptions.h" #else #include "apexceptions.h" #endif TOISeqBuffered::TOISeqBuffered(int wsz) { data = NULL; flags = NULL; AllocBuffer(wsz); setName("toiseqbuff"); syncOldWay = false; } TOISeqBuffered::TOISeqBuffered(string nm, int wsz) { data = NULL; flags = NULL; AllocBuffer(wsz); setName(nm); syncOldWay = false; } TOISeqBuffered::~TOISeqBuffered() { delete[] data; delete[] flags; } void TOISeqBuffered::AllocBuffer(int wsz) { if (wsz < 128) wsz = 128; wsize = wsz; buffsize = 2*wsz; if (data) delete[] data; if (flags) delete[] flags; data = new double[buffsize]; flags = new uint_8[buffsize]; for(int k=0; k= next_in) return DATA_NOT_YET; if (isDataDeleted(iStart)) return DATA_DELETED; return DATA_OK; } TOI::DataStatus TOISeqBuffered::isDataAvailNL(int i) { return TOI::isDataAvailNL(i); } void TOISeqBuffered::wontNeedBefore(int i) { // $CHECK$ Reza 30/4/2001 - Je ne sais pas a quoi ca sert ! // next_out = i; $CHECK$ Reza 30/4/2001 } #ifndef NO_SOPHYA /* ---- l'interface va etre modifiee, NE PAS UTILISER Array TOISeqBuffered::doGetData(int iStart, int iEnd) { // if (iEnd < iStart) // throw RangeCheckError("TOI::getData : iEnd next_out) next_out = iEnd+1; if (isPutWaiting() && (next_in-next_out < wsize/2 )) signalPut(); return dat; } l'interface va etre modifiee, NE PAS UTILISER ---- */ #endif double TOISeqBuffered::getData(int i) { double val; uint_8 flg; getData(i, val, flg); return(val); } void TOISeqBuffered::getData(int i, double & val, uint_8 & flg) { lock(); if (!started) { cout << " TOISeqBuffered::getData() - waitGet() Waiting for start ... " << endl; waitGet(); } cleanWaitGet(); if (isDataDeleted(i)) { if (dbglev > 0) cout << " TOISeqBuffered::getData() - DataDeleted() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; unlock(); throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted"); } while (i >= next_in) { if (i>next_out) next_out = i; if (dbglev > 0) cout << " TOISeqBuffered::getData() - waitGet() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; waitGet(); if (dbglev > 0) cout << " ... Out of waitGet() i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; } cleanWaitGet(); val = dataRef(i); flg = flagRef(i); if (first_out < 0) first_out = i; if ((i+1) > next_out) next_out = i+1; if (isPutWaiting() && (next_in-next_out < wsize/2 )) { if (dbglev > 0) cout << " TOISeqBuffered::getData() - signalPut() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; signalPut(); } unlock(); if (fgsigput) signal(); return; } void TOISeqBuffered::getData(int i, int n, double* data, uint_8* flg) { lock(); if (!started) { cout << " TOISeqBuffered::getData(i,n ...) - waitGet() Waiting for start ... " << endl; waitGet(); } cleanWaitGet(); if (isDataDeleted(i)) { if (dbglev > 0) cout << " TOISeqBuffered::getData(i,n ...) - DataDeleted() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; unlock(); throw RangeCheckError("TOISeqBuffered::getData(i) : data deleted"); } for(int j=i; j= next_in) { if (j>next_out) next_out = j; if (dbglev > 0) cout << " TOISeqBuffered::getData(i,n ...) - waitGet() name=" << getName() << " j=" << j << " next_in= " << next_in << " next_out=" << next_out << endl; waitGet(); if (dbglev > 0) cout << " ... Out of waitGet() j=" << j << " next_in= " << next_in << " next_out=" << next_out << endl; } cleanWaitGet(); data[j-i] = dataRef(j); if (flg) flg[j-i] = flagRef(j); if (first_out < 0) first_out = j; if ((j+1) > next_out) next_out = j+1; if (isPutWaiting() && (next_in-next_out < wsize/2 )) { if (dbglev > 0) cout << " TOISeqBuffered::getData(i,n ...) - signalPut() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; signalPut(); } } unlock(); if (fgsigput) signal(); return; } #ifndef NO_SOPHYA /* ---- l'interface va etre modifiee, NE PAS UTILISER TArray TOISeqBuffered::doGetFlag(int iStart, int iEnd) { if (!started) waitGet(); cleanWaitGet(); if (!isDataAvailNL(iStart, iEnd)) throw RangeCheckError("TOISeqBuffered::getFlag(iS,iE) : data not available"); TVector dat(iEnd - iStart + 1); for (int i=0; i= next_in) waitGet(); int_4 dat = flagRef(i); return dat; } */ void TOISeqBuffered::putData(int i, double value, uint_8 flag) { lock(); if (!started) { first_in = next_in = i; next_out = next_in; started = true; } else { if (i != next_in) { if (dbglev > 0) cout << " TOISeqBuffered::putData() - i!=next_in() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName(); unlock(); throw RangeCheckError(msg); } if (next_in-next_out >= wsize) { if (dbglev > 0) cout << " TOISeqBuffered::putData() - waitPut() " << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; waitPut(); if (dbglev > 0) cout << " ... Out of waitPut() i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; } cleanWaitPut(); } dataRef(i) = value; flagRef(i) = flag; next_in = i+1; if (isGetWaiting() && (next_in-next_out > wsize/8)) { if (dbglev > 0) cout << " TOISeqBuffered::putData() - signalGet() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; signalGet(); } unlock(); if (fgsigget) signal(); return; } void TOISeqBuffered::putData(int i, int n, double const* val, uint_8 const* flg) { lock(); if (!started) { first_in = next_in = i; next_out = next_in; started = true; } else { if (i != next_in) { if (dbglev > 0) cout << " TOISeqBuffered::putData(i,n ...) - i!=next_in() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; string msg = "TOISeqBuffered::putData() : i!=next_in TOIname=" + getName(); unlock(); throw RangeCheckError(msg); } } for(int j=i; j= wsize) { if (dbglev > 0) cout << " TOISeqBuffered::putData(i,n ...) - waitPut() " << getName() << " j=" << j << " next_in= " << next_in << " next_out=" << next_out << endl; waitPut(); if (dbglev > 0) cout << " ... Out of waitPut() j=" << j << " next_in= " << next_in << " next_out=" << next_out << endl; } cleanWaitPut(); dataRef(j) = val[j-i]; if (flg) flagRef(j) = flg[j-i]; else flagRef(j) = 0; next_in = j+1; } if (isGetWaiting() && (next_in-next_out > wsize/8)) { if (dbglev > 0) cout << " TOISeqBuffered::putData(i,n ...) - signalGet() name=" << getName() << " i=" << i << " next_in= " << next_in << " next_out=" << next_out << endl; signalGet(); } unlock(); if (fgsigget) signal(); return; } bool TOISeqBuffered::hasSomeData() { lock(); bool x = started; unlock(); return x; } int TOISeqBuffered::nextDataAvail(int iAfter) { lock(); if (iAfter >= next_in ) {unlock(); return -1;} if (iAfter < (next_in-buffsize)) {unlock(); return (next_in-buffsize);} unlock(); return iAfter+1; } void TOISeqBuffered::doGetData(int i, double & val, uint_8 & flg) { cerr << " TOISeqBuffered::doGetData() not implemented !" << " \n A quoi ca set ??? Reza - Mai 2002 " << endl; throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !"); } void TOISeqBuffered::doPutData(int i, double value, uint_8 flag) { cerr << " TOISeqBuffered::doGetData() not implemented !" << " \n A quoi ca set ??? Reza - Mai 2002 " << endl; throw NotAvailableOperation("TOISeqBuffered::doGetData() not implemented !"); }