[1670] | 1 | #include "toisegment.h"
|
---|
[1671] | 2 |
|
---|
[1697] | 3 | #include <iostream.h>
|
---|
| 4 |
|
---|
[1690] | 5 | #ifndef MAXINT
|
---|
| 6 | #define MAXINT 2147483647
|
---|
| 7 | #endif
|
---|
| 8 |
|
---|
[1692] | 9 | static pthread_mutex_t cout_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
| 10 | static void cout_lock() {pthread_mutex_lock(&cout_mutex);}
|
---|
| 11 | static void cout_unlock() {pthread_mutex_unlock(&cout_mutex);}
|
---|
[1693] | 12 | #define LOG(_xxx_)
|
---|
| 13 | /*
|
---|
[1692] | 14 | #define LOG(_xxx_) \
|
---|
| 15 | cout_lock(); \
|
---|
| 16 | _xxx_; \
|
---|
| 17 | cout_unlock();
|
---|
[1693] | 18 | */
|
---|
[1692] | 19 |
|
---|
[1689] | 20 | /******************************/
|
---|
| 21 | /******* TOISegmented *********/
|
---|
| 22 | /******************************/
|
---|
| 23 |
|
---|
| 24 | TOISegmented::TOISegmented(int bufsz, int maxseg) {
|
---|
| 25 | master = new MasterView(bufsz, maxseg);
|
---|
| 26 | setName("TOISegmented");
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | TOISegmented::TOISegmented(string nm, int bufsz, int maxseg) {
|
---|
| 30 | master = new MasterView(bufsz, maxseg);
|
---|
| 31 | setName(nm);
|
---|
| 32 | }
|
---|
| 33 |
|
---|
[1699] | 34 | TOISegmented::TOISegmented(char* cnm, int bufsz, int maxseg) {
|
---|
| 35 | string nm = cnm;
|
---|
| 36 | master = new MasterView(bufsz, maxseg);
|
---|
| 37 | setName(nm);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[1689] | 40 | TOISegmented::~TOISegmented() {
|
---|
| 41 | delete master;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 |
|
---|
[1692] | 45 | void TOISegmented::addConsumer(TOIProcessor* p) {
|
---|
| 46 | TOI::addConsumer(p);
|
---|
| 47 | master->nConsumers = consumers.size();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
[1689] | 51 | double TOISegmented::getData(int i) { /* reader thread */
|
---|
| 52 | return master->getData(i);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | void TOISegmented::getData(int i, double& data, uint_8& flag) { /* reader thread */
|
---|
| 56 | data = master->getData(i);
|
---|
| 57 | flag = master->getFlag(i);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | void TOISegmented::putData(int i, double value, uint_8 flag) { /* writer thread */
|
---|
| 61 | master->putData(i, value, flag);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | void TOISegmented::putDone() {
|
---|
| 65 | master->putDone();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | void TOISegmented::wontNeedBefore(int i) { /* reader thread */
|
---|
| 69 | master->getView()->wontNeedBefore(i);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | TOI::DataStatus TOISegmented::isDataAvail(int i, int j) {
|
---|
| 73 | // return master->getView()->isDataAvail(i, j);
|
---|
| 74 | cout << "TOISegmented::isDataAvail unimplemented" << endl;
|
---|
| 75 | throw PError("TOISegmented::isDataAvail unimplemented");
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | TOI::DataStatus TOISegmented::isDataAvail(int i) {
|
---|
| 79 | return isDataAvail(i,i);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | TOI::DataStatus TOISegmented::isDataAvailNL(int i, int j) {
|
---|
| 83 | return isDataAvail(i,j);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void TOISegmented::waitForData(int iStart, int iEnd) {
|
---|
| 87 | // get will wait...
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void TOISegmented::waitForData(int i) {
|
---|
| 91 | // get will wait...
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | void TOISegmented::waitForAnyData() {
|
---|
| 95 | cout << "TOISegmented::waitForAnyData unimplemented" << endl;
|
---|
| 96 | throw PError("TOISegmented::waitForAnyData unimplemented");
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | int TOISegmented::nextDataAvail(int iAfter) {
|
---|
| 100 | cout << "TOISegmented::nextDataAvail" << endl;
|
---|
| 101 | return iAfter+1;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | bool TOISegmented::hasSomeData() {
|
---|
| 105 | cout << "TOISegmented::hasSomeData" << endl;
|
---|
| 106 | return true;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[1692] | 109 | void TOISegmented::doPutData(int i, double value, uint_8 flag) {
|
---|
[1689] | 110 | cout << "TOISegmented::doPutData unimplemented" << endl;
|
---|
| 111 | throw PError("TOISegmented::doPutData unimplemented");
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | void TOISegmented::doGetData(int i, double& value, uint_8& flag) {
|
---|
| 115 | cout << "TOISegmented::doGetData unimplemented" << endl;
|
---|
| 116 | throw PError("TOISegmented::doGetData unimplemented");
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
[1686] | 120 | /*******************************/
|
---|
| 121 | /******* BufferSegment *********/
|
---|
| 122 | /*******************************/
|
---|
[1671] | 123 |
|
---|
| 124 | TOISegmented::BufferSegment::BufferSegment(int sz) {
|
---|
| 125 | status = NEW;
|
---|
| 126 | bufferSize = sz;
|
---|
| 127 | sn0 = -1;
|
---|
| 128 |
|
---|
| 129 | refcount = 0;
|
---|
| 130 |
|
---|
| 131 | data = new double[sz];
|
---|
| 132 | flags = new uint_8[sz];
|
---|
| 133 |
|
---|
| 134 | pthread_mutex_init(&refcount_mutex, NULL);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | TOISegmented::BufferSegment::~BufferSegment() {
|
---|
| 138 | if (refcount > 0) {
|
---|
| 139 | throw(ForbiddenError("TOISegment : delete Buffer with refcount>0"));
|
---|
| 140 | }
|
---|
[1700] | 141 | LOG(cout << "Destroying buffersegment sn0 "<< sn0 << endl);
|
---|
[1671] | 142 | delete[] data;
|
---|
| 143 | delete[] flags;
|
---|
| 144 | pthread_mutex_destroy(&refcount_mutex);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[1689] | 147 | void TOISegmented::BufferSegment::putData(int sn, double d, uint_8 f) {
|
---|
| 148 | /* writer thread*/
|
---|
[1671] | 149 | if (status == NEW) {
|
---|
| 150 | status = WRITE;
|
---|
| 151 | sn0 = sn;
|
---|
| 152 | }
|
---|
| 153 | if (status == COMMITTED) {
|
---|
| 154 | throw(ForbiddenError("TOISegment : putData in committed buffer"));
|
---|
| 155 | }
|
---|
| 156 | checkInRange(sn);
|
---|
| 157 | data[sn-sn0] = d;
|
---|
| 158 | flags[sn-sn0] = f;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | void TOISegmented::BufferSegment::incRefCount() {
|
---|
| 162 | pthread_mutex_lock(&refcount_mutex);
|
---|
| 163 | refcount++;
|
---|
| 164 | pthread_mutex_unlock(&refcount_mutex);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | void TOISegmented::BufferSegment::decRefCount() {
|
---|
| 168 | pthread_mutex_lock(&refcount_mutex);
|
---|
| 169 | int nrc = --refcount;
|
---|
| 170 | pthread_mutex_unlock(&refcount_mutex);
|
---|
| 171 | if (nrc<0)
|
---|
| 172 | throw(ForbiddenError("TOISegment : buffer refcount < 0"));
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | int TOISegmented::BufferSegment::getRefCount() {
|
---|
| 176 | pthread_mutex_lock(&refcount_mutex);
|
---|
| 177 | int rc = refcount;
|
---|
| 178 | pthread_mutex_unlock(&refcount_mutex);
|
---|
| 179 | return rc;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[1686] | 182 |
|
---|
| 183 | /*******************************/
|
---|
| 184 | /********** BufferView *********/
|
---|
| 185 | /*******************************/
|
---|
| 186 |
|
---|
| 187 | TOISegmented::BufferView::BufferView(MasterView* m) {
|
---|
| 188 | master = m;
|
---|
| 189 | sn0 = -1;
|
---|
| 190 | segmentSize = m->segmentSize;
|
---|
[1689] | 191 | firstNeeded = -1;
|
---|
[1692] | 192 | waiting = false;
|
---|
[1686] | 193 | }
|
---|
| 194 |
|
---|
| 195 | TOISegmented::BufferView::~BufferView() {
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[1692] | 198 | double TOISegmented::BufferView::getData(int sn) { /* Single-thread, reader thread*/
|
---|
[1686] | 199 | ensure(sn);
|
---|
| 200 | int seg = (sn-sn0)/segmentSize;
|
---|
| 201 | return segments[seg]->getData(sn);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[1692] | 204 | uint_8 TOISegmented::BufferView::getFlag(int sn) { /* Single-thread, reader thread */
|
---|
[1686] | 205 | ensure(sn);
|
---|
| 206 | int seg = (sn-sn0)/segmentSize;
|
---|
| 207 | return segments[seg]->getFlag(sn);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
[1692] | 210 | void TOISegmented::BufferView::ensure(int sn) { /* Single-thread, reader thread */
|
---|
[1686] | 211 | if (sn < sn0) {
|
---|
[1692] | 212 | LOG(cout << "TOISegmented::BufferView::ensure requested sample before first" << endl);
|
---|
| 213 | LOG(cout << "sn " << sn << " sn0 " << sn0 << endl);
|
---|
| 214 | abort();
|
---|
[1686] | 215 | }
|
---|
| 216 |
|
---|
[1692] | 217 | if (sn0 < 0 ||
|
---|
| 218 | sn >= sn0 + segmentSize*segments.size()) {
|
---|
| 219 | LOG(cout << "BufferView " << hex << this << dec << ": read fault for " << sn << endl)
|
---|
[1686] | 220 | sync();
|
---|
[1709] | 221 | pthread_mutex_lock(&(master->read_wait_mutex));
|
---|
[1692] | 222 | while (sn0<0 || sn >= sn0 + segmentSize*segments.size()) {
|
---|
[1709] | 223 | wait(); // must be atomic with loop test
|
---|
| 224 | pthread_mutex_unlock(&(master->read_wait_mutex));
|
---|
[1692] | 225 | LOG(cout << "BufferView " << hex << this << dec << ": waiting for " << sn << endl)
|
---|
[1686] | 226 | sync();
|
---|
[1709] | 227 | pthread_mutex_lock(&(master->read_wait_mutex));
|
---|
[1686] | 228 | }
|
---|
[1709] | 229 | pthread_mutex_unlock(&(master->read_wait_mutex));
|
---|
| 230 |
|
---|
[1692] | 231 | LOG(cout << "BufferView " << hex << this << dec << ": resuming for " << sn
|
---|
| 232 | << " now data for " << sn0 << " - " << sn0 + segmentSize*segments.size()
|
---|
| 233 | << " in " << segments.size() << " segments " << endl)
|
---|
[1686] | 234 | }
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[1692] | 237 | void TOISegmented::BufferView::sync() { /* Single-thread, reader thread */
|
---|
[1686] | 238 | master->updateView(this); // update me !
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[1709] | 241 | void TOISegmented::BufferView::wait() { /* reader thread, master read wait lock taken */
|
---|
| 242 | //pthread_mutex_lock(&(master->read_wait_mutex));
|
---|
[1692] | 243 | waiting = true;
|
---|
| 244 | pthread_cond_wait(&(master->read_wait_condv), &(master->read_wait_mutex));
|
---|
| 245 | waiting = false;
|
---|
[1709] | 246 | //pthread_mutex_unlock(&(master->read_wait_mutex));
|
---|
[1686] | 247 | }
|
---|
| 248 |
|
---|
| 249 |
|
---|
[1689] | 250 | void TOISegmented::BufferView::wontNeedBefore(int sn) { /* reader thread */
|
---|
| 251 | if (sn > firstNeeded) {
|
---|
| 252 | firstNeeded = sn;
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
[1686] | 255 |
|
---|
[1689] | 256 |
|
---|
[1686] | 257 | /*******************************/
|
---|
| 258 | /********** MasterView *********/
|
---|
| 259 | /*******************************/
|
---|
| 260 |
|
---|
| 261 | TOISegmented::MasterView::MasterView(int bufsz, int maxseg) {
|
---|
| 262 | currentSegment = NULL;
|
---|
| 263 | maxSegments = maxseg;
|
---|
| 264 | segmentSize = bufsz;
|
---|
| 265 | sn0 = -1;
|
---|
[1692] | 266 | nConsumers = 0;
|
---|
[1686] | 267 |
|
---|
| 268 | pthread_mutex_init(&views_mutex, NULL);
|
---|
[1692] | 269 | pthread_mutex_init(&read_wait_mutex, NULL);
|
---|
| 270 | pthread_cond_init(&write_wait_condv, NULL);
|
---|
| 271 | pthread_cond_init(&read_wait_condv, NULL);
|
---|
[1686] | 272 | pthread_key_create(&buffer_key, BufferDestroy);
|
---|
| 273 |
|
---|
[1689] | 274 | waitingOnWrite = false;
|
---|
[1686] | 275 | }
|
---|
| 276 |
|
---|
| 277 | TOISegmented::MasterView::~MasterView() {
|
---|
| 278 | pthread_mutex_destroy(&views_mutex);
|
---|
[1692] | 279 | pthread_mutex_destroy(&read_wait_mutex);
|
---|
| 280 | pthread_cond_destroy(&write_wait_condv);
|
---|
| 281 | pthread_cond_destroy(&read_wait_condv);
|
---|
[1686] | 282 | pthread_key_delete(buffer_key);
|
---|
| 283 |
|
---|
| 284 | // There should not be any BufferView left... Check ?
|
---|
| 285 |
|
---|
| 286 | // decrement count for segments ?
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[1692] | 289 | void TOISegmented::MasterView::putData(int sn, double data, uint_8 flags) { /* writer thread */
|
---|
| 290 | if (sn0<0) {
|
---|
| 291 | LOG(cout << "***MasterView::putData sn0<0" << endl)
|
---|
| 292 | sn0=sn;
|
---|
| 293 | }
|
---|
[1686] | 294 | // can fit in current segment ?
|
---|
| 295 | if (!(currentSegment != NULL &&
|
---|
| 296 | sn >= currentSegment->sn0 &&
|
---|
| 297 | sn < currentSegment->sn0 + currentSegment->bufferSize)) {
|
---|
[1692] | 298 | LOG(cout << "MasterView::putData, need extend for " << sn << endl)
|
---|
[1686] | 299 | nextSegment();
|
---|
| 300 | }
|
---|
| 301 | currentSegment->putData(sn, data, flags);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[1692] | 304 | double TOISegmented::MasterView::getData(int sn) { /* reader thread */
|
---|
| 305 | return getView()->getData(sn); /* thread-specific */
|
---|
[1686] | 306 | }
|
---|
| 307 |
|
---|
[1692] | 308 | uint_8 TOISegmented::MasterView::getFlag(int sn) { /* reader thread */
|
---|
[1686] | 309 | return getView()->getFlag(sn);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | TOISegmented::BufferView* TOISegmented::MasterView::getView() { /* reader thread */
|
---|
| 313 | BufferView* bv = (BufferView*) pthread_getspecific(buffer_key);
|
---|
| 314 | if (bv == NULL) {
|
---|
| 315 | bv = createView();
|
---|
[1692] | 316 | LOG(cout << "creating new view " << hex << bv << dec << endl)
|
---|
[1686] | 317 | pthread_setspecific(buffer_key, bv);
|
---|
| 318 | }
|
---|
| 319 | return bv;
|
---|
| 320 | }
|
---|
[1689] | 321 |
|
---|
[1692] | 322 | void TOISegmented::MasterView::signalWaitingViews() { /* any thread */ /* views locked */
|
---|
| 323 | pthread_mutex_lock(&read_wait_mutex);
|
---|
| 324 | pthread_cond_broadcast(&read_wait_condv);
|
---|
| 325 | pthread_mutex_unlock(&read_wait_mutex);
|
---|
| 326 | }
|
---|
[1689] | 327 |
|
---|
[1692] | 328 | void TOISegmented::MasterView::signalWrite() { /* reader thread */ /* views locked */
|
---|
| 329 | if (waitingOnWrite) {
|
---|
| 330 | LOG(cout << "MasterView : signal for wait on write" << endl)
|
---|
| 331 | pthread_cond_signal(&write_wait_condv); // only one thread can be sleeping
|
---|
[1689] | 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | void TOISegmented::MasterView::putDone() {
|
---|
| 336 | nextSegment(); // cree un segment inutile, a nettoyer
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | void TOISegmented::MasterView::nextSegment() { /* writer thread */
|
---|
| 340 | // The current segment, if any, is now committed. A new
|
---|
| 341 | // blank buffer is allocated, if any.
|
---|
[1692] | 342 | pthread_mutex_lock(&views_mutex);
|
---|
[1689] | 343 |
|
---|
[1692] | 344 | LOG(cout << "MasterView::nextSegment "
|
---|
| 345 | << segments.size()+1 << "/" << maxSegments << endl)
|
---|
[1689] | 346 |
|
---|
| 347 | if (currentSegment != NULL) {
|
---|
| 348 | currentSegment->status = BufferSegment::COMMITTED;
|
---|
| 349 | segments.push_back(currentSegment);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | currentSegment = NULL;
|
---|
| 353 | while (segments.size() >= maxSegments) {
|
---|
| 354 | waitForCleaning();
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | currentSegment = new BufferSegment(segmentSize);
|
---|
[1700] | 358 | currentSegment->incRefCount();
|
---|
[1689] | 359 | signalWaitingViews(); // they can ask to be updated !!
|
---|
[1692] | 360 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 361 | }
|
---|
| 362 |
|
---|
[1692] | 363 | void TOISegmented::MasterView::waitForCleaning() { /* writer thread */ /* views locked */
|
---|
| 364 | LOG(cout << "MasterView : write wait for clean for " << sn0 << endl)
|
---|
[1689] | 365 | waitingOnWrite = true;
|
---|
| 366 | checkDeadLock();
|
---|
[1692] | 367 | pthread_cond_wait(&write_wait_condv, &views_mutex);
|
---|
| 368 | LOG(cout << "MasterView : wait done" << endl)
|
---|
[1689] | 369 | }
|
---|
| 370 |
|
---|
| 371 | TOISegmented::BufferView* TOISegmented::MasterView::createView() { /* reader thread */
|
---|
| 372 | BufferView* bv = new BufferView(this);
|
---|
[1692] | 373 | pthread_mutex_lock(&views_mutex);
|
---|
[1690] | 374 | allViews.insert(bv);
|
---|
[1692] | 375 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 376 | updateView(bv);
|
---|
| 377 | return bv;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | void TOISegmented::MasterView::updateView(BufferView* bv) { /* reader thread */
|
---|
| 381 | pthread_mutex_lock(&views_mutex);
|
---|
| 382 |
|
---|
| 383 | int oldBegin = bv->sn0;
|
---|
| 384 | int oldEnd = bv->sn0 + bv->segmentSize * bv->segments.size();
|
---|
| 385 |
|
---|
| 386 | for (vector<BufferSegment*>::iterator i = bv->segments.begin();
|
---|
| 387 | i != bv->segments.end(); i++) {
|
---|
| 388 | (*i)->decRefCount();
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | bv->segments.clear();
|
---|
| 392 |
|
---|
[1690] | 393 | // utiliser firstNeeded de toutes les vues pour faire le menage chez
|
---|
[1689] | 394 | // nous.
|
---|
[1690] | 395 |
|
---|
[1692] | 396 | // A condition que tous les consumers se soient fait connaitre...
|
---|
[1689] | 397 |
|
---|
[1692] | 398 | if (nConsumers == allViews.size()) {
|
---|
| 399 | int firstNeeded = MAXINT;
|
---|
| 400 | for (set<BufferView*>::iterator i = allViews.begin();
|
---|
| 401 | i != allViews.end(); i++) {
|
---|
| 402 | if ((*i)->firstNeeded < firstNeeded) firstNeeded = (*i)->firstNeeded;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | LOG(cout << "MasterView : firstNeeded = " << firstNeeded << endl);
|
---|
| 406 |
|
---|
| 407 | vector<BufferSegment*>::iterator j = segments.begin();
|
---|
| 408 | bool clean = false;
|
---|
| 409 | for (vector<BufferSegment*>::iterator i = segments.begin();
|
---|
| 410 | i != segments.end(); i++) {
|
---|
[1700] | 411 | //LOG(cout << "Updating : rc = " << (*i)->getRefCount() << " sn0 = " << (*i)->sn0 << endl;);
|
---|
| 412 | if (((*i)->sn0+(*i)->bufferSize <= firstNeeded) && ((*i)->getRefCount() == 1)) {
|
---|
[1692] | 413 | clean = true;
|
---|
[1700] | 414 | (*i)->decRefCount();
|
---|
| 415 | delete (*i);
|
---|
[1692] | 416 | j = i;
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
[1700] | 419 | j++;
|
---|
[1690] | 420 | if (clean) {
|
---|
| 421 | segments.erase(segments.begin(),j);
|
---|
| 422 | sn0 = (*segments.begin())->sn0;
|
---|
[1692] | 423 | LOG(cout << "MasterView : purged until " << sn0 << endl);
|
---|
[1690] | 424 | }
|
---|
[1692] | 425 | } else {
|
---|
| 426 | LOG(cout << "MasterView : not yet all consumer thread known "<< allViews.size()
|
---|
| 427 | << "/" << nConsumers << endl);
|
---|
| 428 | }
|
---|
[1690] | 429 |
|
---|
| 430 | for (vector<BufferSegment*>::iterator i = segments.begin();
|
---|
[1689] | 431 | i != segments.end(); i++) {
|
---|
| 432 | if ( (*i)->sn0+(*i)->bufferSize > bv->firstNeeded ) {
|
---|
| 433 | (*i)->incRefCount();
|
---|
| 434 | bv->segments.push_back(*i);
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | bv->sn0 = -1;
|
---|
| 439 | int newEnd = -1;
|
---|
| 440 | if (segments.size() > 0) {
|
---|
| 441 | bv->sn0 = bv->segments[0]->sn0;
|
---|
| 442 | newEnd = bv->sn0 + bv->segmentSize * bv->segments.size();
|
---|
| 443 | }
|
---|
| 444 |
|
---|
[1692] | 445 | if (bv->sn0 > oldBegin) { // nettoyage de fait, reveiller le writer thread si besoin
|
---|
| 446 | signalWrite();
|
---|
[1689] | 447 | }
|
---|
| 448 |
|
---|
[1692] | 449 | LOG(cout << "sync for " << hex << bv << dec << " : "
|
---|
| 450 | << oldBegin << " - " << oldEnd << " --> "
|
---|
| 451 | << bv->sn0 << " - " << newEnd << endl);
|
---|
| 452 |
|
---|
| 453 | if (newEnd > oldEnd) { // Nouveautes, reveiller les reader threads si besoin
|
---|
[1689] | 454 | signalWaitingViews();
|
---|
| 455 | }
|
---|
[1692] | 456 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 457 | }
|
---|
| 458 |
|
---|
[1692] | 459 | void TOISegmented::MasterView::checkDeadLock() { /* views locked */
|
---|
[1689] | 460 | // There is a possible deadlock if no view can free old segments
|
---|
| 461 | // and we are waiting for write.
|
---|
| 462 |
|
---|
| 463 | // we need to record "wont need before" for each view, and
|
---|
| 464 | // signal deadlock if any view that needs first segment data is sleeping
|
---|
| 465 | // while we are asleep
|
---|
| 466 |
|
---|
[1692] | 467 | pthread_mutex_lock(&read_wait_mutex);
|
---|
| 468 | if (!waitingOnWrite) {
|
---|
| 469 | pthread_mutex_unlock(&read_wait_mutex);
|
---|
| 470 | return; // no problem, there is an active writer
|
---|
| 471 | }
|
---|
[1689] | 472 |
|
---|
| 473 | // Is any sleeping view needing our first segment ?
|
---|
| 474 |
|
---|
[1692] | 475 | for (set<BufferView*>::iterator i=allViews.begin();
|
---|
| 476 | i != allViews.end(); i++) {
|
---|
| 477 | if ((*i)->waiting && (*i)->firstNeeded < sn0+segmentSize) {
|
---|
[1689] | 478 | cout << "**** DEADLOCK detected ****" << endl;
|
---|
| 479 | cout << "We are waiting on write (buffer is full)"<< endl;
|
---|
| 480 | cout << "but a waiting reader still needs our first segment" << endl;
|
---|
| 481 | cout << "restart with bigger buffers" << endl;
|
---|
| 482 | abort();
|
---|
| 483 | }
|
---|
| 484 | }
|
---|
[1692] | 485 | pthread_mutex_unlock(&read_wait_mutex);
|
---|
[1689] | 486 | }
|
---|
| 487 |
|
---|
| 488 |
|
---|
| 489 |
|
---|
| 490 | void TOISegmented::MasterView::BufferDestroy(void* p) {
|
---|
| 491 | BufferView* bv = (BufferView*) p;
|
---|
| 492 | delete bv;
|
---|
| 493 | }
|
---|