| [1738] | 1 | // ArchTOIPipe           (C)     CEA/DAPNIA/SPP IN2P3/LAL | 
|---|
|  | 2 | //                               Eric Aubourg | 
|---|
|  | 3 | //                               Christophe Magneville | 
|---|
|  | 4 | //                               Reza Ansari | 
|---|
| [1804] | 5 | // $Id: toisegment.cc,v 1.25 2001-11-30 21:07:16 aubourg Exp $ | 
|---|
| [1738] | 6 |  | 
|---|
| [1670] | 7 | #include "toisegment.h" | 
|---|
| [1671] | 8 |  | 
|---|
| [1759] | 9 | #include <iostream.h> | 
|---|
| [1697] | 10 |  | 
|---|
| [1690] | 11 | #ifndef MAXINT | 
|---|
|  | 12 | #define MAXINT 2147483647 | 
|---|
|  | 13 | #endif | 
|---|
|  | 14 |  | 
|---|
| [1692] | 15 | static pthread_mutex_t cout_mutex = PTHREAD_MUTEX_INITIALIZER; | 
|---|
|  | 16 | static void cout_lock() {pthread_mutex_lock(&cout_mutex);} | 
|---|
|  | 17 | static void cout_unlock() {pthread_mutex_unlock(&cout_mutex);} | 
|---|
| [1711] | 18 | #define LOG(_xxx_) | 
|---|
|  | 19 | /* | 
|---|
| [1692] | 20 | #define LOG(_xxx_) \ | 
|---|
|  | 21 | cout_lock(); \ | 
|---|
|  | 22 | _xxx_; \ | 
|---|
|  | 23 | cout_unlock(); | 
|---|
| [1711] | 24 | */ | 
|---|
| [1692] | 25 |  | 
|---|
| [1689] | 26 | /******************************/ | 
|---|
|  | 27 | /******* TOISegmented *********/ | 
|---|
|  | 28 | /******************************/ | 
|---|
|  | 29 |  | 
|---|
|  | 30 | TOISegmented::TOISegmented(int bufsz, int maxseg) { | 
|---|
| [1710] | 31 | master = new MasterView(bufsz, maxseg, ""); | 
|---|
| [1689] | 32 | setName("TOISegmented"); | 
|---|
| [1740] | 33 | syncOldWay = false; | 
|---|
| [1689] | 34 | } | 
|---|
|  | 35 |  | 
|---|
|  | 36 | TOISegmented::TOISegmented(string nm, int bufsz, int maxseg) { | 
|---|
| [1710] | 37 | master = new MasterView(bufsz, maxseg, nm); | 
|---|
| [1689] | 38 | setName(nm); | 
|---|
| [1740] | 39 | syncOldWay = false; | 
|---|
| [1689] | 40 | } | 
|---|
|  | 41 |  | 
|---|
| [1699] | 42 | TOISegmented::TOISegmented(char* cnm, int bufsz, int maxseg) { | 
|---|
|  | 43 | string nm = cnm; | 
|---|
| [1710] | 44 | master = new MasterView(bufsz, maxseg, nm); | 
|---|
| [1699] | 45 | setName(nm); | 
|---|
| [1740] | 46 | syncOldWay = false; | 
|---|
| [1699] | 47 | } | 
|---|
|  | 48 |  | 
|---|
| [1689] | 49 | TOISegmented::~TOISegmented() { | 
|---|
|  | 50 | delete master; | 
|---|
|  | 51 | } | 
|---|
|  | 52 |  | 
|---|
|  | 53 |  | 
|---|
| [1692] | 54 | void TOISegmented::addConsumer(TOIProcessor* p) { | 
|---|
|  | 55 | TOI::addConsumer(p); | 
|---|
|  | 56 | master->nConsumers = consumers.size(); | 
|---|
|  | 57 | } | 
|---|
|  | 58 |  | 
|---|
|  | 59 |  | 
|---|
| [1689] | 60 | double TOISegmented::getData(int i) { /* reader thread */ | 
|---|
|  | 61 | return master->getData(i); | 
|---|
|  | 62 | } | 
|---|
|  | 63 |  | 
|---|
|  | 64 | void TOISegmented::getData(int i, double& data, uint_8& flag) { /* reader thread */ | 
|---|
|  | 65 | data = master->getData(i); | 
|---|
|  | 66 | flag = master->getFlag(i); | 
|---|
|  | 67 | } | 
|---|
|  | 68 |  | 
|---|
| [1743] | 69 | void TOISegmented::getData(int i, int n, double* data, uint_8* flg) { /* reader thread */ | 
|---|
|  | 70 | master->getData(i, n, data, flg); | 
|---|
|  | 71 | } | 
|---|
|  | 72 |  | 
|---|
| [1689] | 73 | void TOISegmented::putData(int i, double value, uint_8 flag) { /* writer thread */ | 
|---|
|  | 74 | master->putData(i, value, flag); | 
|---|
|  | 75 | } | 
|---|
|  | 76 |  | 
|---|
| [1743] | 77 | void TOISegmented::putData(int i, int n, double const* val, uint_8 const* flg) { /* writer thread */ | 
|---|
|  | 78 | master->putData(i, n, val, flg); | 
|---|
|  | 79 | } | 
|---|
|  | 80 |  | 
|---|
| [1689] | 81 | void TOISegmented::putDone() { | 
|---|
|  | 82 | master->putDone(); | 
|---|
|  | 83 | } | 
|---|
|  | 84 |  | 
|---|
|  | 85 | void TOISegmented::wontNeedBefore(int i) {  /* reader thread */ | 
|---|
|  | 86 | master->getView()->wontNeedBefore(i); | 
|---|
|  | 87 | } | 
|---|
|  | 88 |  | 
|---|
|  | 89 | TOI::DataStatus TOISegmented::isDataAvail(int i, int j) { | 
|---|
|  | 90 | // return master->getView()->isDataAvail(i, j); | 
|---|
|  | 91 | cout << "TOISegmented::isDataAvail unimplemented" << endl; | 
|---|
|  | 92 | throw PError("TOISegmented::isDataAvail unimplemented"); | 
|---|
|  | 93 | } | 
|---|
|  | 94 |  | 
|---|
|  | 95 | TOI::DataStatus TOISegmented::isDataAvail(int i) { | 
|---|
|  | 96 | return isDataAvail(i,i); | 
|---|
|  | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | TOI::DataStatus TOISegmented::isDataAvailNL(int i, int j) { | 
|---|
|  | 100 | return isDataAvail(i,j); | 
|---|
|  | 101 | } | 
|---|
|  | 102 |  | 
|---|
| [1754] | 103 | TOI::DataStatus TOISegmented::isDataAvailNL(int i) { | 
|---|
|  | 104 | return isDataAvail(i); | 
|---|
|  | 105 | } | 
|---|
|  | 106 |  | 
|---|
| [1689] | 107 | void TOISegmented::waitForData(int iStart, int iEnd) { | 
|---|
|  | 108 | // get will wait... | 
|---|
|  | 109 | } | 
|---|
|  | 110 |  | 
|---|
|  | 111 | void TOISegmented::waitForData(int i) { | 
|---|
|  | 112 | // get will wait... | 
|---|
|  | 113 | } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | void TOISegmented::waitForAnyData() { | 
|---|
|  | 116 | cout << "TOISegmented::waitForAnyData unimplemented" << endl; | 
|---|
|  | 117 | throw PError("TOISegmented::waitForAnyData unimplemented"); | 
|---|
|  | 118 | } | 
|---|
|  | 119 |  | 
|---|
|  | 120 | int TOISegmented::nextDataAvail(int iAfter) { | 
|---|
|  | 121 | cout << "TOISegmented::nextDataAvail" << endl; | 
|---|
|  | 122 | return iAfter+1; | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | bool TOISegmented::hasSomeData() { | 
|---|
|  | 126 | cout << "TOISegmented::hasSomeData" << endl; | 
|---|
|  | 127 | return true; | 
|---|
|  | 128 | } | 
|---|
|  | 129 |  | 
|---|
| [1692] | 130 | void TOISegmented::doPutData(int i, double value, uint_8 flag) { | 
|---|
| [1689] | 131 | cout << "TOISegmented::doPutData unimplemented" << endl; | 
|---|
|  | 132 | throw PError("TOISegmented::doPutData unimplemented"); | 
|---|
|  | 133 | } | 
|---|
|  | 134 |  | 
|---|
|  | 135 | void TOISegmented::doGetData(int i, double& value, uint_8& flag) { | 
|---|
|  | 136 | cout << "TOISegmented::doGetData unimplemented" << endl; | 
|---|
|  | 137 | throw PError("TOISegmented::doGetData unimplemented"); | 
|---|
|  | 138 | } | 
|---|
|  | 139 |  | 
|---|
|  | 140 |  | 
|---|
| [1686] | 141 | /*******************************/ | 
|---|
|  | 142 | /******* BufferSegment *********/ | 
|---|
|  | 143 | /*******************************/ | 
|---|
| [1671] | 144 |  | 
|---|
|  | 145 | TOISegmented::BufferSegment::BufferSegment(int sz) { | 
|---|
|  | 146 | status     = NEW; | 
|---|
|  | 147 | bufferSize = sz; | 
|---|
|  | 148 | sn0        = -1; | 
|---|
|  | 149 |  | 
|---|
|  | 150 | refcount   = 0; | 
|---|
|  | 151 |  | 
|---|
|  | 152 | data       = new double[sz]; | 
|---|
|  | 153 | flags     = new uint_8[sz]; | 
|---|
|  | 154 |  | 
|---|
|  | 155 | pthread_mutex_init(&refcount_mutex, NULL); | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
|  | 158 | TOISegmented::BufferSegment::~BufferSegment() { | 
|---|
|  | 159 | if (refcount > 0) { | 
|---|
|  | 160 | throw(ForbiddenError("TOISegment : delete Buffer with refcount>0")); | 
|---|
|  | 161 | } | 
|---|
| [1700] | 162 | LOG(cout << "Destroying buffersegment sn0 "<< sn0 << endl); | 
|---|
| [1671] | 163 | delete[] data; | 
|---|
|  | 164 | delete[] flags; | 
|---|
|  | 165 | pthread_mutex_destroy(&refcount_mutex); | 
|---|
|  | 166 | } | 
|---|
|  | 167 |  | 
|---|
| [1743] | 168 | void TOISegmented::BufferSegment::getData(int sn, int n, double* d, uint_8* f) { | 
|---|
|  | 169 | checkCommitted(); | 
|---|
|  | 170 | checkInRange(sn); | 
|---|
|  | 171 | checkInRange(sn+n-1); | 
|---|
|  | 172 | memcpy(d, data+(sn-sn0), n*sizeof(double)); | 
|---|
|  | 173 | if (f != NULL) { | 
|---|
|  | 174 | memcpy(f, flags+(sn-sn0), n*sizeof(uint_8)); | 
|---|
|  | 175 | } | 
|---|
|  | 176 | } | 
|---|
|  | 177 |  | 
|---|
| [1689] | 178 | void TOISegmented::BufferSegment::putData(int sn, double d, uint_8 f) { | 
|---|
|  | 179 | /* writer thread*/ | 
|---|
| [1671] | 180 | if (status == NEW) { | 
|---|
|  | 181 | status = WRITE; | 
|---|
|  | 182 | sn0 = sn; | 
|---|
|  | 183 | } | 
|---|
|  | 184 | if (status == COMMITTED) { | 
|---|
|  | 185 | throw(ForbiddenError("TOISegment : putData in committed buffer")); | 
|---|
|  | 186 | } | 
|---|
|  | 187 | checkInRange(sn); | 
|---|
|  | 188 | data[sn-sn0] = d; | 
|---|
|  | 189 | flags[sn-sn0] = f; | 
|---|
|  | 190 | } | 
|---|
|  | 191 |  | 
|---|
| [1743] | 192 | void TOISegmented::BufferSegment::putData(int sn, int n, double const* d, uint_8 const* f) { | 
|---|
| [1744] | 193 | if (status == NEW) { | 
|---|
|  | 194 | status = WRITE; | 
|---|
|  | 195 | sn0 = sn; | 
|---|
|  | 196 | } | 
|---|
|  | 197 | if (status == COMMITTED) { | 
|---|
|  | 198 | throw(ForbiddenError("TOISegment : putData in committed buffer")); | 
|---|
|  | 199 | } | 
|---|
| [1743] | 200 | checkInRange(sn); | 
|---|
|  | 201 | checkInRange(sn+n-1); | 
|---|
|  | 202 | memcpy(data+(sn-sn0), d, n*sizeof(double)); | 
|---|
|  | 203 | if (f != NULL) { | 
|---|
|  | 204 | memcpy(flags+(sn-sn0), f, n*sizeof(uint_8)); | 
|---|
|  | 205 | } else { | 
|---|
|  | 206 | memset(flags+(sn-sn0), 0, n*sizeof(uint_8)); | 
|---|
|  | 207 | } | 
|---|
|  | 208 | } | 
|---|
|  | 209 |  | 
|---|
| [1671] | 210 | void TOISegmented::BufferSegment::incRefCount() { | 
|---|
|  | 211 | pthread_mutex_lock(&refcount_mutex); | 
|---|
|  | 212 | refcount++; | 
|---|
|  | 213 | pthread_mutex_unlock(&refcount_mutex); | 
|---|
|  | 214 | } | 
|---|
|  | 215 |  | 
|---|
|  | 216 | void TOISegmented::BufferSegment::decRefCount() { | 
|---|
|  | 217 | pthread_mutex_lock(&refcount_mutex); | 
|---|
|  | 218 | int nrc = --refcount; | 
|---|
|  | 219 | pthread_mutex_unlock(&refcount_mutex); | 
|---|
|  | 220 | if (nrc<0) | 
|---|
|  | 221 | throw(ForbiddenError("TOISegment : buffer refcount < 0")); | 
|---|
|  | 222 | } | 
|---|
|  | 223 |  | 
|---|
|  | 224 | int TOISegmented::BufferSegment::getRefCount() { | 
|---|
|  | 225 | pthread_mutex_lock(&refcount_mutex); | 
|---|
|  | 226 | int rc = refcount; | 
|---|
|  | 227 | pthread_mutex_unlock(&refcount_mutex); | 
|---|
|  | 228 | return rc; | 
|---|
|  | 229 | } | 
|---|
|  | 230 |  | 
|---|
| [1686] | 231 |  | 
|---|
|  | 232 | /*******************************/ | 
|---|
|  | 233 | /********** BufferView *********/ | 
|---|
|  | 234 | /*******************************/ | 
|---|
|  | 235 |  | 
|---|
|  | 236 | TOISegmented::BufferView::BufferView(MasterView* m) { | 
|---|
|  | 237 | master = m; | 
|---|
|  | 238 | sn0 = -1; | 
|---|
|  | 239 | segmentSize = m->segmentSize; | 
|---|
| [1689] | 240 | firstNeeded = -1; | 
|---|
| [1692] | 241 | waiting = false; | 
|---|
| [1773] | 242 | waitingFor = -1; | 
|---|
| [1686] | 243 | } | 
|---|
|  | 244 |  | 
|---|
|  | 245 | TOISegmented::BufferView::~BufferView() { | 
|---|
|  | 246 | } | 
|---|
|  | 247 |  | 
|---|
| [1692] | 248 | double TOISegmented::BufferView::getData(int sn) { /* Single-thread, reader thread*/ | 
|---|
| [1686] | 249 | ensure(sn); | 
|---|
|  | 250 | int seg = (sn-sn0)/segmentSize; | 
|---|
|  | 251 | return segments[seg]->getData(sn); | 
|---|
|  | 252 | } | 
|---|
|  | 253 |  | 
|---|
| [1692] | 254 | uint_8 TOISegmented::BufferView::getFlag(int sn) { /* Single-thread, reader thread */ | 
|---|
| [1686] | 255 | ensure(sn); | 
|---|
|  | 256 | int seg = (sn-sn0)/segmentSize; | 
|---|
|  | 257 | return segments[seg]->getFlag(sn); | 
|---|
|  | 258 | } | 
|---|
|  | 259 |  | 
|---|
| [1743] | 260 | void TOISegmented::BufferView::getData(int sn, int n, double* dat, uint_8* flg) { /* Single-thread, reader thread */ | 
|---|
|  | 261 | ensure(sn); | 
|---|
|  | 262 | ensure(sn+n-1); | 
|---|
|  | 263 |  | 
|---|
|  | 264 | int  sn1  = sn; | 
|---|
|  | 265 | int  nsam = n; | 
|---|
|  | 266 | double* pdat = dat; | 
|---|
|  | 267 | uint_8* pflg = flg; | 
|---|
|  | 268 |  | 
|---|
|  | 269 | while (true) { | 
|---|
|  | 270 | int seg = (sn1-sn0)/segmentSize; | 
|---|
|  | 271 | BufferSegment* s = segments[seg]; | 
|---|
|  | 272 | int snmax = s->sn0 + s->bufferSize - 1; | 
|---|
|  | 273 | int sn2 = snmax > (sn1+nsam-1) ? (sn1+nsam-1) : snmax; | 
|---|
|  | 274 | int nget = sn2-sn1+1; | 
|---|
|  | 275 | s->getData(sn1, nget, pdat, pflg); | 
|---|
|  | 276 | pdat += nget; | 
|---|
|  | 277 | if (pflg != NULL) pflg += nget; | 
|---|
|  | 278 | nsam -= nget; | 
|---|
|  | 279 | sn1  += nget; | 
|---|
|  | 280 | if (nsam <= 0) break; | 
|---|
|  | 281 | } | 
|---|
|  | 282 | } | 
|---|
|  | 283 |  | 
|---|
| [1692] | 284 | void  TOISegmented::BufferView::ensure(int sn) { /* Single-thread, reader thread */ | 
|---|
| [1686] | 285 | if (sn < sn0) { | 
|---|
| [1750] | 286 | cout << "TOISegmented::BufferView::ensure requested sample before first" << endl; | 
|---|
|  | 287 | cout << "sn " << sn << " sn0 " << sn0 << endl; | 
|---|
| [1692] | 288 | abort(); | 
|---|
| [1686] | 289 | } | 
|---|
|  | 290 |  | 
|---|
| [1692] | 291 | if (sn0 < 0 || | 
|---|
|  | 292 | sn >= sn0 + segmentSize*segments.size()) { | 
|---|
| [1710] | 293 | LOG(cout << master->name << " BufferView " | 
|---|
|  | 294 | << hex << this << dec << ": read fault for " << sn << endl) | 
|---|
| [1686] | 295 | sync(); | 
|---|
| [1709] | 296 | pthread_mutex_lock(&(master->read_wait_mutex)); | 
|---|
| [1692] | 297 | while (sn0<0 || sn >= sn0 + segmentSize*segments.size()) { | 
|---|
| [1773] | 298 | wait(sn); // must be atomic with loop test // $CHECK$ est-ce vrai ? | 
|---|
| [1709] | 299 | pthread_mutex_unlock(&(master->read_wait_mutex)); | 
|---|
| [1710] | 300 | LOG(cout << master->name << " BufferView " << hex << this << dec << ": waiting for " << sn << endl) | 
|---|
| [1686] | 301 | sync(); | 
|---|
| [1709] | 302 | pthread_mutex_lock(&(master->read_wait_mutex)); | 
|---|
| [1686] | 303 | } | 
|---|
| [1709] | 304 | pthread_mutex_unlock(&(master->read_wait_mutex)); | 
|---|
|  | 305 |  | 
|---|
| [1710] | 306 | LOG(cout << master->name << " BufferView " << hex << this << dec << ": resuming for " << sn | 
|---|
| [1692] | 307 | << " now data for " << sn0 << " - " << sn0 + segmentSize*segments.size() | 
|---|
|  | 308 | << " in " << segments.size() << " segments " << endl) | 
|---|
| [1686] | 309 | } | 
|---|
|  | 310 | } | 
|---|
|  | 311 |  | 
|---|
| [1692] | 312 | void TOISegmented::BufferView::sync() { /* Single-thread, reader thread */ | 
|---|
| [1686] | 313 | master->updateView(this); // update me ! | 
|---|
|  | 314 | } | 
|---|
|  | 315 |  | 
|---|
| [1773] | 316 | void TOISegmented::BufferView::wait(int sn) { /* reader thread, master read wait lock taken */ | 
|---|
| [1709] | 317 | //pthread_mutex_lock(&(master->read_wait_mutex)); | 
|---|
| [1692] | 318 | waiting = true; | 
|---|
| [1773] | 319 | waitingFor = sn; | 
|---|
| [1711] | 320 | master->waitingViews++; | 
|---|
| [1692] | 321 | pthread_cond_wait(&(master->read_wait_condv), &(master->read_wait_mutex)); | 
|---|
|  | 322 | waiting = false; | 
|---|
| [1773] | 323 | waitingFor = -1; | 
|---|
| [1711] | 324 | master->waitingViews--; | 
|---|
| [1709] | 325 | //pthread_mutex_unlock(&(master->read_wait_mutex)); | 
|---|
| [1686] | 326 | } | 
|---|
|  | 327 |  | 
|---|
|  | 328 |  | 
|---|
| [1689] | 329 | void TOISegmented::BufferView::wontNeedBefore(int sn) { /* reader thread */ | 
|---|
|  | 330 | if (sn > firstNeeded) { | 
|---|
|  | 331 | firstNeeded = sn; | 
|---|
| [1711] | 332 | // C'est peut-etre le moment de faire unl sync, si on est coince par ailleurs... | 
|---|
|  | 333 | //pthread_mutex_lock(&(master->read_wait_mutex)); | 
|---|
|  | 334 | if (sn >= sn0 + segmentSize){  //  && master->waitingViews>0) { | 
|---|
|  | 335 | //       LOG(cout<<master->name<< " sync on wontneed, waitingViews=" << master->waitingViews << endl); | 
|---|
|  | 336 | LOG(cout<<master->name<< " sync on wontneed, sn = " << sn << " sn0 = " << sn0 << endl); | 
|---|
|  | 337 | sync(); | 
|---|
|  | 338 | } | 
|---|
|  | 339 | //pthread_mutex_unlock(&(master->read_wait_mutex)); | 
|---|
| [1689] | 340 | } | 
|---|
|  | 341 | } | 
|---|
| [1686] | 342 |  | 
|---|
| [1689] | 343 |  | 
|---|
| [1686] | 344 | /*******************************/ | 
|---|
|  | 345 | /********** MasterView *********/ | 
|---|
|  | 346 | /*******************************/ | 
|---|
|  | 347 |  | 
|---|
| [1710] | 348 | TOISegmented::MasterView::MasterView(int bufsz, int maxseg, string nm) { | 
|---|
| [1686] | 349 | currentSegment = NULL; | 
|---|
|  | 350 | maxSegments    = maxseg; | 
|---|
|  | 351 | segmentSize    = bufsz; | 
|---|
|  | 352 | sn0            = -1; | 
|---|
| [1692] | 353 | nConsumers = 0; | 
|---|
| [1710] | 354 | name = nm; | 
|---|
| [1686] | 355 |  | 
|---|
|  | 356 | pthread_mutex_init(&views_mutex, NULL); | 
|---|
| [1692] | 357 | pthread_mutex_init(&read_wait_mutex, NULL); | 
|---|
|  | 358 | pthread_cond_init(&write_wait_condv, NULL); | 
|---|
|  | 359 | pthread_cond_init(&read_wait_condv, NULL); | 
|---|
| [1686] | 360 | pthread_key_create(&buffer_key, BufferDestroy); | 
|---|
|  | 361 |  | 
|---|
| [1689] | 362 | waitingOnWrite = false; | 
|---|
| [1711] | 363 | waitingViews = 0; | 
|---|
| [1686] | 364 | } | 
|---|
|  | 365 |  | 
|---|
|  | 366 | TOISegmented::MasterView::~MasterView() { | 
|---|
|  | 367 | pthread_mutex_destroy(&views_mutex); | 
|---|
| [1692] | 368 | pthread_mutex_destroy(&read_wait_mutex); | 
|---|
|  | 369 | pthread_cond_destroy(&write_wait_condv); | 
|---|
|  | 370 | pthread_cond_destroy(&read_wait_condv); | 
|---|
| [1686] | 371 | pthread_key_delete(buffer_key); | 
|---|
|  | 372 |  | 
|---|
|  | 373 | // There should not be any BufferView left... Check ? | 
|---|
|  | 374 |  | 
|---|
|  | 375 | // decrement count for segments ? | 
|---|
|  | 376 | } | 
|---|
|  | 377 |  | 
|---|
| [1692] | 378 | void TOISegmented::MasterView::putData(int sn, double data, uint_8 flags) { /* writer thread */ | 
|---|
|  | 379 | if (sn0<0) { | 
|---|
| [1750] | 380 | LOG(cout << "***MasterView::putData sn0<0 -> " << sn << endl); | 
|---|
| [1692] | 381 | sn0=sn; | 
|---|
|  | 382 | } | 
|---|
| [1686] | 383 | // can fit in current segment ? | 
|---|
|  | 384 | if (!(currentSegment != NULL && | 
|---|
|  | 385 | sn >= currentSegment->sn0 && | 
|---|
|  | 386 | sn < currentSegment->sn0 + currentSegment->bufferSize)) { | 
|---|
| [1710] | 387 | LOG(cout << name << " MasterView::putData, need extend for " << sn << endl) | 
|---|
| [1686] | 388 | nextSegment(); | 
|---|
|  | 389 | } | 
|---|
|  | 390 | currentSegment->putData(sn, data, flags); | 
|---|
|  | 391 | } | 
|---|
|  | 392 |  | 
|---|
| [1743] | 393 | void TOISegmented::MasterView::putData(int sn, int n, double const* data, uint_8 const* flags) { /* writer thread */ | 
|---|
|  | 394 | if (sn0<0) { | 
|---|
| [1750] | 395 | LOG(cout << "***MasterView::putData sn0<0 -> " << sn << endl); | 
|---|
| [1743] | 396 | sn0=sn; | 
|---|
|  | 397 | } | 
|---|
|  | 398 | double const*  pdat = data; | 
|---|
|  | 399 | uint_8 const*  pflg = flags; | 
|---|
|  | 400 | int     nsam  = n; | 
|---|
|  | 401 | int     sn1   = sn; | 
|---|
|  | 402 | while (true) { | 
|---|
|  | 403 | // maximum that current segment can take | 
|---|
|  | 404 | int snmax = -1; | 
|---|
|  | 405 | if (currentSegment != NULL) { | 
|---|
|  | 406 | snmax = currentSegment->sn0 + currentSegment->bufferSize-1; | 
|---|
|  | 407 | } | 
|---|
|  | 408 | int sn2 = snmax > (sn1+nsam-1) ? (sn1+nsam-1) : snmax; | 
|---|
|  | 409 | if (snmax>0) { | 
|---|
|  | 410 | int nput = sn2-sn1+1; | 
|---|
|  | 411 | currentSegment->putData(sn1, nput, pdat, pflg); | 
|---|
|  | 412 | pdat += nput; | 
|---|
|  | 413 | if (pflg != NULL) pflg += nput; | 
|---|
|  | 414 | nsam -= nput; | 
|---|
|  | 415 | sn1  += nput; | 
|---|
|  | 416 | } | 
|---|
|  | 417 | if (nsam <= 0) break; | 
|---|
|  | 418 | nextSegment(); | 
|---|
|  | 419 | currentSegment->putData(sn1, 0, 0); // dummy, to initialize sn0 in segment : add method ? | 
|---|
|  | 420 | } | 
|---|
|  | 421 | } | 
|---|
|  | 422 |  | 
|---|
| [1692] | 423 | double TOISegmented::MasterView::getData(int sn) { /* reader thread */ | 
|---|
|  | 424 | return getView()->getData(sn); /* thread-specific */ | 
|---|
| [1686] | 425 | } | 
|---|
|  | 426 |  | 
|---|
| [1692] | 427 | uint_8 TOISegmented::MasterView::getFlag(int sn) { /* reader thread */ | 
|---|
| [1686] | 428 | return getView()->getFlag(sn); | 
|---|
|  | 429 | } | 
|---|
|  | 430 |  | 
|---|
| [1743] | 431 | void TOISegmented::MasterView::getData(int sn, int n, double* dat, uint_8* flg) { /* reader thread */ | 
|---|
|  | 432 | getView()->getData(sn, n, dat, flg); | 
|---|
|  | 433 | } | 
|---|
|  | 434 |  | 
|---|
| [1686] | 435 | TOISegmented::BufferView* TOISegmented::MasterView::getView() { /* reader thread */ | 
|---|
|  | 436 | BufferView* bv = (BufferView*) pthread_getspecific(buffer_key); | 
|---|
|  | 437 | if (bv == NULL) { | 
|---|
|  | 438 | bv = createView(); | 
|---|
| [1692] | 439 | LOG(cout << "creating new view " << hex << bv << dec << endl) | 
|---|
| [1686] | 440 | pthread_setspecific(buffer_key, bv); | 
|---|
|  | 441 | } | 
|---|
|  | 442 | return bv; | 
|---|
|  | 443 | } | 
|---|
| [1689] | 444 |  | 
|---|
| [1692] | 445 | void TOISegmented::MasterView::signalWaitingViews() { /* any thread */ /* views locked */ | 
|---|
|  | 446 | pthread_mutex_lock(&read_wait_mutex); | 
|---|
|  | 447 | pthread_cond_broadcast(&read_wait_condv); | 
|---|
|  | 448 | pthread_mutex_unlock(&read_wait_mutex); | 
|---|
|  | 449 | } | 
|---|
| [1689] | 450 |  | 
|---|
| [1692] | 451 | void TOISegmented::MasterView::signalWrite() { /* reader thread */ /* views locked */ | 
|---|
|  | 452 | if (waitingOnWrite) { | 
|---|
| [1710] | 453 | LOG(cout << name << " MasterView : signal for wait on write" << endl) | 
|---|
| [1692] | 454 | pthread_cond_signal(&write_wait_condv); // only one thread can be sleeping | 
|---|
| [1689] | 455 | } | 
|---|
|  | 456 | } | 
|---|
|  | 457 |  | 
|---|
|  | 458 | void TOISegmented::MasterView::putDone() { | 
|---|
|  | 459 | nextSegment(); // cree un segment inutile, a nettoyer | 
|---|
|  | 460 | } | 
|---|
|  | 461 |  | 
|---|
|  | 462 | void TOISegmented::MasterView::nextSegment() { /* writer thread */ | 
|---|
|  | 463 | // The current segment, if any, is now committed. A new | 
|---|
|  | 464 | // blank buffer is allocated, if any. | 
|---|
| [1692] | 465 | pthread_mutex_lock(&views_mutex); | 
|---|
| [1689] | 466 |  | 
|---|
| [1692] | 467 | LOG(cout << "MasterView::nextSegment " | 
|---|
|  | 468 | << segments.size()+1 << "/" << maxSegments << endl) | 
|---|
| [1689] | 469 |  | 
|---|
|  | 470 | if (currentSegment != NULL) { | 
|---|
|  | 471 | currentSegment->status = BufferSegment::COMMITTED; | 
|---|
|  | 472 | segments.push_back(currentSegment); | 
|---|
|  | 473 | } | 
|---|
|  | 474 |  | 
|---|
|  | 475 | currentSegment = NULL; | 
|---|
|  | 476 | while (segments.size() >= maxSegments) { | 
|---|
|  | 477 | waitForCleaning(); | 
|---|
|  | 478 | } | 
|---|
|  | 479 |  | 
|---|
|  | 480 | currentSegment = new BufferSegment(segmentSize); | 
|---|
| [1700] | 481 | currentSegment->incRefCount(); | 
|---|
| [1689] | 482 | signalWaitingViews(); // they can ask to be updated !! | 
|---|
| [1692] | 483 | pthread_mutex_unlock(&views_mutex); | 
|---|
| [1689] | 484 | } | 
|---|
|  | 485 |  | 
|---|
| [1692] | 486 | void TOISegmented::MasterView::waitForCleaning() { /* writer thread */ /* views locked */ | 
|---|
| [1710] | 487 | LOG(cout << name << " MasterView : write wait for clean for " << sn0 << endl) | 
|---|
| [1689] | 488 | waitingOnWrite = true; | 
|---|
|  | 489 | checkDeadLock(); | 
|---|
| [1692] | 490 | pthread_cond_wait(&write_wait_condv, &views_mutex); | 
|---|
| [1710] | 491 | LOG(cout << name << " MasterView : wait done" << endl) | 
|---|
| [1689] | 492 | } | 
|---|
|  | 493 |  | 
|---|
|  | 494 | TOISegmented::BufferView* TOISegmented::MasterView::createView() { /* reader thread */ | 
|---|
|  | 495 | BufferView* bv =  new BufferView(this); | 
|---|
| [1692] | 496 | pthread_mutex_lock(&views_mutex); | 
|---|
| [1690] | 497 | allViews.insert(bv); | 
|---|
| [1692] | 498 | pthread_mutex_unlock(&views_mutex); | 
|---|
| [1689] | 499 | updateView(bv); | 
|---|
|  | 500 | return bv; | 
|---|
|  | 501 | } | 
|---|
|  | 502 |  | 
|---|
|  | 503 | void TOISegmented::MasterView::updateView(BufferView* bv) { /* reader thread */ | 
|---|
|  | 504 | pthread_mutex_lock(&views_mutex); | 
|---|
|  | 505 |  | 
|---|
| [1711] | 506 | //  int oldBegin = bv->sn0; | 
|---|
|  | 507 | //  int oldEnd   = bv->sn0 + bv->segmentSize * bv->segments.size(); | 
|---|
|  | 508 | int oldBegin = sn0; | 
|---|
|  | 509 | int oldEnd   = sn0 + bv->segmentSize * segments.size(); | 
|---|
| [1689] | 510 |  | 
|---|
|  | 511 | for (vector<BufferSegment*>::iterator i = bv->segments.begin(); | 
|---|
|  | 512 | i != bv->segments.end(); i++) { | 
|---|
|  | 513 | (*i)->decRefCount(); | 
|---|
|  | 514 | } | 
|---|
|  | 515 |  | 
|---|
|  | 516 | bv->segments.clear(); | 
|---|
|  | 517 |  | 
|---|
| [1690] | 518 | // utiliser firstNeeded de toutes les vues pour faire le menage chez | 
|---|
| [1689] | 519 | // nous. | 
|---|
| [1690] | 520 |  | 
|---|
| [1692] | 521 | // A condition que tous les consumers se soient fait connaitre... | 
|---|
| [1689] | 522 |  | 
|---|
| [1692] | 523 | if (nConsumers == allViews.size()) { | 
|---|
|  | 524 | int firstNeeded = MAXINT; | 
|---|
|  | 525 | for (set<BufferView*>::iterator i = allViews.begin(); | 
|---|
|  | 526 | i != allViews.end(); i++) { | 
|---|
| [1711] | 527 | LOG(cout << name << " View firstneeded " << (*i)->firstNeeded << endl); | 
|---|
| [1692] | 528 | if ((*i)->firstNeeded < firstNeeded) firstNeeded = (*i)->firstNeeded; | 
|---|
|  | 529 | } | 
|---|
|  | 530 |  | 
|---|
| [1710] | 531 | LOG(cout << name << " MasterView : firstNeeded = " << firstNeeded << endl); | 
|---|
| [1692] | 532 |  | 
|---|
|  | 533 | vector<BufferSegment*>::iterator j = segments.begin(); | 
|---|
|  | 534 | bool clean = false; | 
|---|
| [1763] | 535 | {for (vector<BufferSegment*>::iterator i = segments.begin(); | 
|---|
| [1692] | 536 | i != segments.end(); i++) { | 
|---|
| [1700] | 537 | //LOG(cout << "Updating : rc = " << (*i)->getRefCount() << " sn0 = " << (*i)->sn0 << endl;); | 
|---|
|  | 538 | if (((*i)->sn0+(*i)->bufferSize <= firstNeeded) && ((*i)->getRefCount() == 1)) { | 
|---|
| [1692] | 539 | clean = true; | 
|---|
| [1700] | 540 | (*i)->decRefCount(); | 
|---|
|  | 541 | delete (*i); | 
|---|
| [1692] | 542 | j = i; | 
|---|
|  | 543 | } | 
|---|
| [1763] | 544 | }} | 
|---|
| [1700] | 545 | j++; | 
|---|
| [1690] | 546 | if (clean) { | 
|---|
|  | 547 | segments.erase(segments.begin(),j); | 
|---|
|  | 548 | sn0 = (*segments.begin())->sn0; | 
|---|
| [1692] | 549 | LOG(cout << "MasterView : purged until " << sn0 << endl); | 
|---|
| [1690] | 550 | } | 
|---|
| [1692] | 551 | } else { | 
|---|
|  | 552 | LOG(cout << "MasterView : not yet all consumer thread known "<< allViews.size() | 
|---|
|  | 553 | << "/" << nConsumers  << endl); | 
|---|
|  | 554 | } | 
|---|
| [1690] | 555 |  | 
|---|
| [1763] | 556 | {for (vector<BufferSegment*>::iterator i = segments.begin(); | 
|---|
| [1689] | 557 | i != segments.end(); i++) { | 
|---|
|  | 558 | if ( (*i)->sn0+(*i)->bufferSize > bv->firstNeeded ) { | 
|---|
|  | 559 | (*i)->incRefCount(); | 
|---|
|  | 560 | bv->segments.push_back(*i); | 
|---|
|  | 561 | } | 
|---|
| [1763] | 562 | }} | 
|---|
| [1689] | 563 |  | 
|---|
|  | 564 | bv->sn0 = -1; | 
|---|
|  | 565 | int newEnd = -1; | 
|---|
|  | 566 | if (segments.size() > 0) { | 
|---|
|  | 567 | bv->sn0 = bv->segments[0]->sn0; | 
|---|
|  | 568 | newEnd = bv->sn0 + bv->segmentSize * bv->segments.size(); | 
|---|
|  | 569 | } | 
|---|
|  | 570 |  | 
|---|
| [1711] | 571 | if (sn0 > oldBegin) { // nettoyage de fait, reveiller le writer thread si besoin | 
|---|
| [1692] | 572 | signalWrite(); | 
|---|
| [1689] | 573 | } | 
|---|
|  | 574 |  | 
|---|
| [1710] | 575 | LOG(cout << name << " sync for " << hex << bv << dec << " : " | 
|---|
| [1692] | 576 | << oldBegin << " - " << oldEnd << "  -->  " | 
|---|
| [1711] | 577 | << sn0 << " - " << newEnd << endl); | 
|---|
| [1692] | 578 |  | 
|---|
|  | 579 | if (newEnd > oldEnd) { // Nouveautes, reveiller les reader threads si besoin | 
|---|
| [1689] | 580 | signalWaitingViews(); | 
|---|
|  | 581 | } | 
|---|
| [1692] | 582 | pthread_mutex_unlock(&views_mutex); | 
|---|
| [1689] | 583 | } | 
|---|
|  | 584 |  | 
|---|
| [1692] | 585 | void TOISegmented::MasterView::checkDeadLock() { /* views locked */ | 
|---|
| [1689] | 586 | // There is a possible deadlock if no view can free old segments | 
|---|
|  | 587 | // and we are waiting for write. | 
|---|
|  | 588 |  | 
|---|
|  | 589 | // we need to record "wont need before" for each view, and | 
|---|
|  | 590 | // signal deadlock if any view that needs first segment data is sleeping | 
|---|
|  | 591 | // while we are asleep | 
|---|
|  | 592 |  | 
|---|
| [1692] | 593 | pthread_mutex_lock(&read_wait_mutex); | 
|---|
|  | 594 | if (!waitingOnWrite) { | 
|---|
|  | 595 | pthread_mutex_unlock(&read_wait_mutex); | 
|---|
|  | 596 | return; // no problem, there is an active writer | 
|---|
|  | 597 | } | 
|---|
| [1689] | 598 |  | 
|---|
|  | 599 | // Is any sleeping view needing our first segment ? | 
|---|
|  | 600 |  | 
|---|
| [1692] | 601 | for (set<BufferView*>::iterator i=allViews.begin(); | 
|---|
|  | 602 | i != allViews.end(); i++) { | 
|---|
|  | 603 | if ((*i)->waiting && (*i)->firstNeeded < sn0+segmentSize) { | 
|---|
| [1689] | 604 | cout << "**** DEADLOCK detected ****" << endl; | 
|---|
|  | 605 | cout << "We are waiting on write (buffer is full)"<< endl; | 
|---|
|  | 606 | cout << "but a waiting reader still needs our first segment" << endl; | 
|---|
|  | 607 | cout << "restart with bigger buffers" << endl; | 
|---|
| [1775] | 608 |  | 
|---|
|  | 609 | cout << "master has range " << sn0 << " - " | 
|---|
|  | 610 | << sn0+segments.size()*segmentSize-1 <<endl; | 
|---|
|  | 611 | cout << "in " << segments.size() << " segments" << endl; | 
|---|
|  | 612 | cout << "waiting bufferview is waiting for " << (*i)->waitingFor | 
|---|
|  | 613 | << " and still needs " << (*i)->firstNeeded << endl; | 
|---|
| [1804] | 614 | cout << "it has sn0= " << (*i)->sn0 << " nseg = " << (*i)->segments.size() | 
|---|
|  | 615 | << " snlast = " << (*i)->sn0+(*i)->segments.size()*(*i)->segmentSize -1 | 
|---|
|  | 616 | << endl; | 
|---|
|  | 617 |  | 
|---|
| [1689] | 618 | abort(); | 
|---|
|  | 619 | } | 
|---|
|  | 620 | } | 
|---|
| [1692] | 621 | pthread_mutex_unlock(&read_wait_mutex); | 
|---|
| [1689] | 622 | } | 
|---|
|  | 623 |  | 
|---|
|  | 624 |  | 
|---|
|  | 625 |  | 
|---|
|  | 626 | void TOISegmented::MasterView::BufferDestroy(void* p) { | 
|---|
|  | 627 | BufferView* bv = (BufferView*) p; | 
|---|
|  | 628 | delete bv; | 
|---|
|  | 629 | } | 
|---|