[1738] | 1 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
| 2 | // Eric Aubourg
|
---|
| 3 | // Christophe Magneville
|
---|
| 4 | // Reza Ansari
|
---|
[1964] | 5 | // $Id: toisegment.cc,v 1.27 2002-04-16 08:28:53 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) {
|
---|
[1944] | 160 | cerr << "TOISegment : delete Buffer with refcount>0" << endl;
|
---|
[1671] | 161 | throw(ForbiddenError("TOISegment : delete Buffer with refcount>0"));
|
---|
| 162 | }
|
---|
[1700] | 163 | LOG(cout << "Destroying buffersegment sn0 "<< sn0 << endl);
|
---|
[1671] | 164 | delete[] data;
|
---|
| 165 | delete[] flags;
|
---|
| 166 | pthread_mutex_destroy(&refcount_mutex);
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[1743] | 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 |
|
---|
[1689] | 179 | void TOISegmented::BufferSegment::putData(int sn, double d, uint_8 f) {
|
---|
| 180 | /* writer thread*/
|
---|
[1671] | 181 | if (status == NEW) {
|
---|
| 182 | status = WRITE;
|
---|
| 183 | sn0 = sn;
|
---|
| 184 | }
|
---|
| 185 | if (status == COMMITTED) {
|
---|
[1944] | 186 | cerr << "TOISegment : putData in committed buffer" << endl;
|
---|
[1671] | 187 | throw(ForbiddenError("TOISegment : putData in committed buffer"));
|
---|
| 188 | }
|
---|
| 189 | checkInRange(sn);
|
---|
| 190 | data[sn-sn0] = d;
|
---|
| 191 | flags[sn-sn0] = f;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
[1743] | 194 | void TOISegmented::BufferSegment::putData(int sn, int n, double const* d, uint_8 const* f) {
|
---|
[1744] | 195 | if (status == NEW) {
|
---|
| 196 | status = WRITE;
|
---|
| 197 | sn0 = sn;
|
---|
| 198 | }
|
---|
| 199 | if (status == COMMITTED) {
|
---|
[1944] | 200 | cerr << "TOISegment : putData in committed buffer" << endl;
|
---|
[1744] | 201 | throw(ForbiddenError("TOISegment : putData in committed buffer"));
|
---|
| 202 | }
|
---|
[1964] | 203 | if (n==0) return;
|
---|
[1743] | 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 |
|
---|
[1671] | 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);
|
---|
[1944] | 224 | if (nrc<0) {
|
---|
| 225 | cerr << "TOISegment : buffer refcount < 0" << endl;
|
---|
[1671] | 226 | throw(ForbiddenError("TOISegment : buffer refcount < 0"));
|
---|
[1944] | 227 | }
|
---|
[1671] | 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 |
|
---|
[1686] | 237 |
|
---|
| 238 | /*******************************/
|
---|
| 239 | /********** BufferView *********/
|
---|
| 240 | /*******************************/
|
---|
| 241 |
|
---|
| 242 | TOISegmented::BufferView::BufferView(MasterView* m) {
|
---|
| 243 | master = m;
|
---|
| 244 | sn0 = -1;
|
---|
| 245 | segmentSize = m->segmentSize;
|
---|
[1689] | 246 | firstNeeded = -1;
|
---|
[1692] | 247 | waiting = false;
|
---|
[1773] | 248 | waitingFor = -1;
|
---|
[1686] | 249 | }
|
---|
| 250 |
|
---|
| 251 | TOISegmented::BufferView::~BufferView() {
|
---|
| 252 | }
|
---|
| 253 |
|
---|
[1692] | 254 | double TOISegmented::BufferView::getData(int sn) { /* Single-thread, reader thread*/
|
---|
[1686] | 255 | ensure(sn);
|
---|
| 256 | int seg = (sn-sn0)/segmentSize;
|
---|
| 257 | return segments[seg]->getData(sn);
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[1692] | 260 | uint_8 TOISegmented::BufferView::getFlag(int sn) { /* Single-thread, reader thread */
|
---|
[1686] | 261 | ensure(sn);
|
---|
| 262 | int seg = (sn-sn0)/segmentSize;
|
---|
| 263 | return segments[seg]->getFlag(sn);
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[1743] | 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 |
|
---|
[1692] | 290 | void TOISegmented::BufferView::ensure(int sn) { /* Single-thread, reader thread */
|
---|
[1686] | 291 | if (sn < sn0) {
|
---|
[1750] | 292 | cout << "TOISegmented::BufferView::ensure requested sample before first" << endl;
|
---|
| 293 | cout << "sn " << sn << " sn0 " << sn0 << endl;
|
---|
[1692] | 294 | abort();
|
---|
[1686] | 295 | }
|
---|
| 296 |
|
---|
[1692] | 297 | if (sn0 < 0 ||
|
---|
| 298 | sn >= sn0 + segmentSize*segments.size()) {
|
---|
[1710] | 299 | LOG(cout << master->name << " BufferView "
|
---|
| 300 | << hex << this << dec << ": read fault for " << sn << endl)
|
---|
[1686] | 301 | sync();
|
---|
[1709] | 302 | pthread_mutex_lock(&(master->read_wait_mutex));
|
---|
[1692] | 303 | while (sn0<0 || sn >= sn0 + segmentSize*segments.size()) {
|
---|
[1773] | 304 | wait(sn); // must be atomic with loop test // $CHECK$ est-ce vrai ?
|
---|
[1709] | 305 | pthread_mutex_unlock(&(master->read_wait_mutex));
|
---|
[1710] | 306 | LOG(cout << master->name << " BufferView " << hex << this << dec << ": waiting for " << sn << endl)
|
---|
[1686] | 307 | sync();
|
---|
[1709] | 308 | pthread_mutex_lock(&(master->read_wait_mutex));
|
---|
[1686] | 309 | }
|
---|
[1709] | 310 | pthread_mutex_unlock(&(master->read_wait_mutex));
|
---|
| 311 |
|
---|
[1710] | 312 | LOG(cout << master->name << " BufferView " << hex << this << dec << ": resuming for " << sn
|
---|
[1692] | 313 | << " now data for " << sn0 << " - " << sn0 + segmentSize*segments.size()
|
---|
| 314 | << " in " << segments.size() << " segments " << endl)
|
---|
[1686] | 315 | }
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[1692] | 318 | void TOISegmented::BufferView::sync() { /* Single-thread, reader thread */
|
---|
[1686] | 319 | master->updateView(this); // update me !
|
---|
| 320 | }
|
---|
| 321 |
|
---|
[1773] | 322 | void TOISegmented::BufferView::wait(int sn) { /* reader thread, master read wait lock taken */
|
---|
[1709] | 323 | //pthread_mutex_lock(&(master->read_wait_mutex));
|
---|
[1692] | 324 | waiting = true;
|
---|
[1773] | 325 | waitingFor = sn;
|
---|
[1711] | 326 | master->waitingViews++;
|
---|
[1692] | 327 | pthread_cond_wait(&(master->read_wait_condv), &(master->read_wait_mutex));
|
---|
| 328 | waiting = false;
|
---|
[1773] | 329 | waitingFor = -1;
|
---|
[1711] | 330 | master->waitingViews--;
|
---|
[1709] | 331 | //pthread_mutex_unlock(&(master->read_wait_mutex));
|
---|
[1686] | 332 | }
|
---|
| 333 |
|
---|
| 334 |
|
---|
[1689] | 335 | void TOISegmented::BufferView::wontNeedBefore(int sn) { /* reader thread */
|
---|
| 336 | if (sn > firstNeeded) {
|
---|
| 337 | firstNeeded = sn;
|
---|
[1711] | 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));
|
---|
[1689] | 346 | }
|
---|
| 347 | }
|
---|
[1686] | 348 |
|
---|
[1689] | 349 |
|
---|
[1686] | 350 | /*******************************/
|
---|
| 351 | /********** MasterView *********/
|
---|
| 352 | /*******************************/
|
---|
| 353 |
|
---|
[1710] | 354 | TOISegmented::MasterView::MasterView(int bufsz, int maxseg, string nm) {
|
---|
[1686] | 355 | currentSegment = NULL;
|
---|
| 356 | maxSegments = maxseg;
|
---|
| 357 | segmentSize = bufsz;
|
---|
| 358 | sn0 = -1;
|
---|
[1692] | 359 | nConsumers = 0;
|
---|
[1710] | 360 | name = nm;
|
---|
[1686] | 361 |
|
---|
| 362 | pthread_mutex_init(&views_mutex, NULL);
|
---|
[1692] | 363 | pthread_mutex_init(&read_wait_mutex, NULL);
|
---|
| 364 | pthread_cond_init(&write_wait_condv, NULL);
|
---|
| 365 | pthread_cond_init(&read_wait_condv, NULL);
|
---|
[1686] | 366 | pthread_key_create(&buffer_key, BufferDestroy);
|
---|
| 367 |
|
---|
[1689] | 368 | waitingOnWrite = false;
|
---|
[1711] | 369 | waitingViews = 0;
|
---|
[1686] | 370 | }
|
---|
| 371 |
|
---|
| 372 | TOISegmented::MasterView::~MasterView() {
|
---|
| 373 | pthread_mutex_destroy(&views_mutex);
|
---|
[1692] | 374 | pthread_mutex_destroy(&read_wait_mutex);
|
---|
| 375 | pthread_cond_destroy(&write_wait_condv);
|
---|
| 376 | pthread_cond_destroy(&read_wait_condv);
|
---|
[1686] | 377 | pthread_key_delete(buffer_key);
|
---|
| 378 |
|
---|
| 379 | // There should not be any BufferView left... Check ?
|
---|
| 380 |
|
---|
| 381 | // decrement count for segments ?
|
---|
| 382 | }
|
---|
| 383 |
|
---|
[1692] | 384 | void TOISegmented::MasterView::putData(int sn, double data, uint_8 flags) { /* writer thread */
|
---|
| 385 | if (sn0<0) {
|
---|
[1750] | 386 | LOG(cout << "***MasterView::putData sn0<0 -> " << sn << endl);
|
---|
[1692] | 387 | sn0=sn;
|
---|
| 388 | }
|
---|
[1686] | 389 | // can fit in current segment ?
|
---|
| 390 | if (!(currentSegment != NULL &&
|
---|
| 391 | sn >= currentSegment->sn0 &&
|
---|
| 392 | sn < currentSegment->sn0 + currentSegment->bufferSize)) {
|
---|
[1710] | 393 | LOG(cout << name << " MasterView::putData, need extend for " << sn << endl)
|
---|
[1686] | 394 | nextSegment();
|
---|
| 395 | }
|
---|
| 396 | currentSegment->putData(sn, data, flags);
|
---|
| 397 | }
|
---|
| 398 |
|
---|
[1743] | 399 | void TOISegmented::MasterView::putData(int sn, int n, double const* data, uint_8 const* flags) { /* writer thread */
|
---|
| 400 | if (sn0<0) {
|
---|
[1750] | 401 | LOG(cout << "***MasterView::putData sn0<0 -> " << sn << endl);
|
---|
[1743] | 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;
|
---|
[1964] | 415 | int nput = sn2-sn1+1;
|
---|
| 416 | if (snmax>0 && nput>0) {
|
---|
[1743] | 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 |
|
---|
[1692] | 429 | double TOISegmented::MasterView::getData(int sn) { /* reader thread */
|
---|
| 430 | return getView()->getData(sn); /* thread-specific */
|
---|
[1686] | 431 | }
|
---|
| 432 |
|
---|
[1692] | 433 | uint_8 TOISegmented::MasterView::getFlag(int sn) { /* reader thread */
|
---|
[1686] | 434 | return getView()->getFlag(sn);
|
---|
| 435 | }
|
---|
| 436 |
|
---|
[1743] | 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 |
|
---|
[1686] | 441 | TOISegmented::BufferView* TOISegmented::MasterView::getView() { /* reader thread */
|
---|
| 442 | BufferView* bv = (BufferView*) pthread_getspecific(buffer_key);
|
---|
| 443 | if (bv == NULL) {
|
---|
| 444 | bv = createView();
|
---|
[1692] | 445 | LOG(cout << "creating new view " << hex << bv << dec << endl)
|
---|
[1686] | 446 | pthread_setspecific(buffer_key, bv);
|
---|
| 447 | }
|
---|
| 448 | return bv;
|
---|
| 449 | }
|
---|
[1689] | 450 |
|
---|
[1692] | 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 | }
|
---|
[1689] | 456 |
|
---|
[1692] | 457 | void TOISegmented::MasterView::signalWrite() { /* reader thread */ /* views locked */
|
---|
| 458 | if (waitingOnWrite) {
|
---|
[1710] | 459 | LOG(cout << name << " MasterView : signal for wait on write" << endl)
|
---|
[1692] | 460 | pthread_cond_signal(&write_wait_condv); // only one thread can be sleeping
|
---|
[1689] | 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.
|
---|
[1692] | 471 | pthread_mutex_lock(&views_mutex);
|
---|
[1689] | 472 |
|
---|
[1692] | 473 | LOG(cout << "MasterView::nextSegment "
|
---|
| 474 | << segments.size()+1 << "/" << maxSegments << endl)
|
---|
[1689] | 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);
|
---|
[1700] | 487 | currentSegment->incRefCount();
|
---|
[1689] | 488 | signalWaitingViews(); // they can ask to be updated !!
|
---|
[1692] | 489 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 490 | }
|
---|
| 491 |
|
---|
[1692] | 492 | void TOISegmented::MasterView::waitForCleaning() { /* writer thread */ /* views locked */
|
---|
[1710] | 493 | LOG(cout << name << " MasterView : write wait for clean for " << sn0 << endl)
|
---|
[1689] | 494 | waitingOnWrite = true;
|
---|
| 495 | checkDeadLock();
|
---|
[1692] | 496 | pthread_cond_wait(&write_wait_condv, &views_mutex);
|
---|
[1710] | 497 | LOG(cout << name << " MasterView : wait done" << endl)
|
---|
[1689] | 498 | }
|
---|
| 499 |
|
---|
| 500 | TOISegmented::BufferView* TOISegmented::MasterView::createView() { /* reader thread */
|
---|
| 501 | BufferView* bv = new BufferView(this);
|
---|
[1692] | 502 | pthread_mutex_lock(&views_mutex);
|
---|
[1690] | 503 | allViews.insert(bv);
|
---|
[1692] | 504 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 505 | updateView(bv);
|
---|
| 506 | return bv;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | void TOISegmented::MasterView::updateView(BufferView* bv) { /* reader thread */
|
---|
| 510 | pthread_mutex_lock(&views_mutex);
|
---|
| 511 |
|
---|
[1711] | 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();
|
---|
[1689] | 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 |
|
---|
[1690] | 524 | // utiliser firstNeeded de toutes les vues pour faire le menage chez
|
---|
[1689] | 525 | // nous.
|
---|
[1690] | 526 |
|
---|
[1692] | 527 | // A condition que tous les consumers se soient fait connaitre...
|
---|
[1689] | 528 |
|
---|
[1692] | 529 | if (nConsumers == allViews.size()) {
|
---|
| 530 | int firstNeeded = MAXINT;
|
---|
| 531 | for (set<BufferView*>::iterator i = allViews.begin();
|
---|
| 532 | i != allViews.end(); i++) {
|
---|
[1711] | 533 | LOG(cout << name << " View firstneeded " << (*i)->firstNeeded << endl);
|
---|
[1692] | 534 | if ((*i)->firstNeeded < firstNeeded) firstNeeded = (*i)->firstNeeded;
|
---|
| 535 | }
|
---|
| 536 |
|
---|
[1710] | 537 | LOG(cout << name << " MasterView : firstNeeded = " << firstNeeded << endl);
|
---|
[1692] | 538 |
|
---|
| 539 | vector<BufferSegment*>::iterator j = segments.begin();
|
---|
| 540 | bool clean = false;
|
---|
[1763] | 541 | {for (vector<BufferSegment*>::iterator i = segments.begin();
|
---|
[1692] | 542 | i != segments.end(); i++) {
|
---|
[1700] | 543 | //LOG(cout << "Updating : rc = " << (*i)->getRefCount() << " sn0 = " << (*i)->sn0 << endl;);
|
---|
| 544 | if (((*i)->sn0+(*i)->bufferSize <= firstNeeded) && ((*i)->getRefCount() == 1)) {
|
---|
[1692] | 545 | clean = true;
|
---|
[1700] | 546 | (*i)->decRefCount();
|
---|
| 547 | delete (*i);
|
---|
[1692] | 548 | j = i;
|
---|
| 549 | }
|
---|
[1763] | 550 | }}
|
---|
[1700] | 551 | j++;
|
---|
[1690] | 552 | if (clean) {
|
---|
| 553 | segments.erase(segments.begin(),j);
|
---|
| 554 | sn0 = (*segments.begin())->sn0;
|
---|
[1692] | 555 | LOG(cout << "MasterView : purged until " << sn0 << endl);
|
---|
[1690] | 556 | }
|
---|
[1692] | 557 | } else {
|
---|
| 558 | LOG(cout << "MasterView : not yet all consumer thread known "<< allViews.size()
|
---|
| 559 | << "/" << nConsumers << endl);
|
---|
| 560 | }
|
---|
[1690] | 561 |
|
---|
[1763] | 562 | {for (vector<BufferSegment*>::iterator i = segments.begin();
|
---|
[1689] | 563 | i != segments.end(); i++) {
|
---|
| 564 | if ( (*i)->sn0+(*i)->bufferSize > bv->firstNeeded ) {
|
---|
| 565 | (*i)->incRefCount();
|
---|
| 566 | bv->segments.push_back(*i);
|
---|
| 567 | }
|
---|
[1763] | 568 | }}
|
---|
[1689] | 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 |
|
---|
[1711] | 577 | if (sn0 > oldBegin) { // nettoyage de fait, reveiller le writer thread si besoin
|
---|
[1692] | 578 | signalWrite();
|
---|
[1689] | 579 | }
|
---|
| 580 |
|
---|
[1710] | 581 | LOG(cout << name << " sync for " << hex << bv << dec << " : "
|
---|
[1692] | 582 | << oldBegin << " - " << oldEnd << " --> "
|
---|
[1711] | 583 | << sn0 << " - " << newEnd << endl);
|
---|
[1692] | 584 |
|
---|
| 585 | if (newEnd > oldEnd) { // Nouveautes, reveiller les reader threads si besoin
|
---|
[1689] | 586 | signalWaitingViews();
|
---|
| 587 | }
|
---|
[1692] | 588 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 589 | }
|
---|
| 590 |
|
---|
[1692] | 591 | void TOISegmented::MasterView::checkDeadLock() { /* views locked */
|
---|
[1689] | 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 |
|
---|
[1692] | 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 | }
|
---|
[1689] | 604 |
|
---|
| 605 | // Is any sleeping view needing our first segment ?
|
---|
| 606 |
|
---|
[1692] | 607 | for (set<BufferView*>::iterator i=allViews.begin();
|
---|
| 608 | i != allViews.end(); i++) {
|
---|
| 609 | if ((*i)->waiting && (*i)->firstNeeded < sn0+segmentSize) {
|
---|
[1689] | 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;
|
---|
[1775] | 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;
|
---|
[1804] | 620 | cout << "it has sn0= " << (*i)->sn0 << " nseg = " << (*i)->segments.size()
|
---|
| 621 | << " snlast = " << (*i)->sn0+(*i)->segments.size()*(*i)->segmentSize -1
|
---|
| 622 | << endl;
|
---|
| 623 |
|
---|
[1689] | 624 | abort();
|
---|
| 625 | }
|
---|
| 626 | }
|
---|
[1692] | 627 | pthread_mutex_unlock(&read_wait_mutex);
|
---|
[1689] | 628 | }
|
---|
| 629 |
|
---|
| 630 |
|
---|
| 631 |
|
---|
| 632 | void TOISegmented::MasterView::BufferDestroy(void* p) {
|
---|
| 633 | BufferView* bv = (BufferView*) p;
|
---|
| 634 | delete bv;
|
---|
| 635 | }
|
---|