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