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