[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();
|
---|
[1692] | 221 | while (sn0<0 || sn >= sn0 + segmentSize*segments.size()) {
|
---|
[1686] | 222 | wait();
|
---|
[1692] | 223 | LOG(cout << "BufferView " << hex << this << dec << ": waiting for " << sn << endl)
|
---|
[1686] | 224 | sync();
|
---|
| 225 | }
|
---|
[1692] | 226 | LOG(cout << "BufferView " << hex << this << dec << ": resuming for " << sn
|
---|
| 227 | << " now data for " << sn0 << " - " << sn0 + segmentSize*segments.size()
|
---|
| 228 | << " in " << segments.size() << " segments " << endl)
|
---|
[1686] | 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
[1692] | 232 | void TOISegmented::BufferView::sync() { /* Single-thread, reader thread */
|
---|
[1686] | 233 | master->updateView(this); // update me !
|
---|
| 234 | }
|
---|
| 235 |
|
---|
[1692] | 236 | void TOISegmented::BufferView::wait() { /* reader thread */
|
---|
| 237 | pthread_mutex_lock(&(master->read_wait_mutex));
|
---|
| 238 | waiting = true;
|
---|
| 239 | pthread_cond_wait(&(master->read_wait_condv), &(master->read_wait_mutex));
|
---|
| 240 | waiting = false;
|
---|
| 241 | pthread_mutex_unlock(&(master->read_wait_mutex));
|
---|
[1686] | 242 | }
|
---|
| 243 |
|
---|
| 244 |
|
---|
[1689] | 245 | void TOISegmented::BufferView::wontNeedBefore(int sn) { /* reader thread */
|
---|
| 246 | if (sn > firstNeeded) {
|
---|
| 247 | firstNeeded = sn;
|
---|
| 248 | }
|
---|
| 249 | }
|
---|
[1686] | 250 |
|
---|
[1689] | 251 |
|
---|
[1686] | 252 | /*******************************/
|
---|
| 253 | /********** MasterView *********/
|
---|
| 254 | /*******************************/
|
---|
| 255 |
|
---|
| 256 | TOISegmented::MasterView::MasterView(int bufsz, int maxseg) {
|
---|
| 257 | currentSegment = NULL;
|
---|
| 258 | maxSegments = maxseg;
|
---|
| 259 | segmentSize = bufsz;
|
---|
| 260 | sn0 = -1;
|
---|
[1692] | 261 | nConsumers = 0;
|
---|
[1686] | 262 |
|
---|
| 263 | pthread_mutex_init(&views_mutex, NULL);
|
---|
[1692] | 264 | pthread_mutex_init(&read_wait_mutex, NULL);
|
---|
| 265 | pthread_cond_init(&write_wait_condv, NULL);
|
---|
| 266 | pthread_cond_init(&read_wait_condv, NULL);
|
---|
[1686] | 267 | pthread_key_create(&buffer_key, BufferDestroy);
|
---|
| 268 |
|
---|
[1689] | 269 | waitingOnWrite = false;
|
---|
[1686] | 270 | }
|
---|
| 271 |
|
---|
| 272 | TOISegmented::MasterView::~MasterView() {
|
---|
| 273 | pthread_mutex_destroy(&views_mutex);
|
---|
[1692] | 274 | pthread_mutex_destroy(&read_wait_mutex);
|
---|
| 275 | pthread_cond_destroy(&write_wait_condv);
|
---|
| 276 | pthread_cond_destroy(&read_wait_condv);
|
---|
[1686] | 277 | pthread_key_delete(buffer_key);
|
---|
| 278 |
|
---|
| 279 | // There should not be any BufferView left... Check ?
|
---|
| 280 |
|
---|
| 281 | // decrement count for segments ?
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[1692] | 284 | void TOISegmented::MasterView::putData(int sn, double data, uint_8 flags) { /* writer thread */
|
---|
| 285 | if (sn0<0) {
|
---|
| 286 | LOG(cout << "***MasterView::putData sn0<0" << endl)
|
---|
| 287 | sn0=sn;
|
---|
| 288 | }
|
---|
[1686] | 289 | // can fit in current segment ?
|
---|
| 290 | if (!(currentSegment != NULL &&
|
---|
| 291 | sn >= currentSegment->sn0 &&
|
---|
| 292 | sn < currentSegment->sn0 + currentSegment->bufferSize)) {
|
---|
[1692] | 293 | LOG(cout << "MasterView::putData, need extend for " << sn << endl)
|
---|
[1686] | 294 | nextSegment();
|
---|
| 295 | }
|
---|
| 296 | currentSegment->putData(sn, data, flags);
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[1692] | 299 | double TOISegmented::MasterView::getData(int sn) { /* reader thread */
|
---|
| 300 | return getView()->getData(sn); /* thread-specific */
|
---|
[1686] | 301 | }
|
---|
| 302 |
|
---|
[1692] | 303 | uint_8 TOISegmented::MasterView::getFlag(int sn) { /* reader thread */
|
---|
[1686] | 304 | return getView()->getFlag(sn);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | TOISegmented::BufferView* TOISegmented::MasterView::getView() { /* reader thread */
|
---|
| 308 | BufferView* bv = (BufferView*) pthread_getspecific(buffer_key);
|
---|
| 309 | if (bv == NULL) {
|
---|
| 310 | bv = createView();
|
---|
[1692] | 311 | LOG(cout << "creating new view " << hex << bv << dec << endl)
|
---|
[1686] | 312 | pthread_setspecific(buffer_key, bv);
|
---|
| 313 | }
|
---|
| 314 | return bv;
|
---|
| 315 | }
|
---|
[1689] | 316 |
|
---|
[1692] | 317 | void TOISegmented::MasterView::signalWaitingViews() { /* any thread */ /* views locked */
|
---|
| 318 | pthread_mutex_lock(&read_wait_mutex);
|
---|
| 319 | pthread_cond_broadcast(&read_wait_condv);
|
---|
| 320 | pthread_mutex_unlock(&read_wait_mutex);
|
---|
| 321 | }
|
---|
[1689] | 322 |
|
---|
[1692] | 323 | void TOISegmented::MasterView::signalWrite() { /* reader thread */ /* views locked */
|
---|
| 324 | if (waitingOnWrite) {
|
---|
| 325 | LOG(cout << "MasterView : signal for wait on write" << endl)
|
---|
| 326 | pthread_cond_signal(&write_wait_condv); // only one thread can be sleeping
|
---|
[1689] | 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | void TOISegmented::MasterView::putDone() {
|
---|
| 331 | nextSegment(); // cree un segment inutile, a nettoyer
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | void TOISegmented::MasterView::nextSegment() { /* writer thread */
|
---|
| 335 | // The current segment, if any, is now committed. A new
|
---|
| 336 | // blank buffer is allocated, if any.
|
---|
[1692] | 337 | pthread_mutex_lock(&views_mutex);
|
---|
[1689] | 338 |
|
---|
[1692] | 339 | LOG(cout << "MasterView::nextSegment "
|
---|
| 340 | << segments.size()+1 << "/" << maxSegments << endl)
|
---|
[1689] | 341 |
|
---|
| 342 | if (currentSegment != NULL) {
|
---|
| 343 | currentSegment->status = BufferSegment::COMMITTED;
|
---|
| 344 | segments.push_back(currentSegment);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | currentSegment = NULL;
|
---|
| 348 | while (segments.size() >= maxSegments) {
|
---|
| 349 | waitForCleaning();
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | currentSegment = new BufferSegment(segmentSize);
|
---|
[1700] | 353 | currentSegment->incRefCount();
|
---|
[1689] | 354 | signalWaitingViews(); // they can ask to be updated !!
|
---|
[1692] | 355 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 356 | }
|
---|
| 357 |
|
---|
[1692] | 358 | void TOISegmented::MasterView::waitForCleaning() { /* writer thread */ /* views locked */
|
---|
| 359 | LOG(cout << "MasterView : write wait for clean for " << sn0 << endl)
|
---|
[1689] | 360 | waitingOnWrite = true;
|
---|
| 361 | checkDeadLock();
|
---|
[1692] | 362 | pthread_cond_wait(&write_wait_condv, &views_mutex);
|
---|
| 363 | LOG(cout << "MasterView : wait done" << endl)
|
---|
[1689] | 364 | }
|
---|
| 365 |
|
---|
| 366 | TOISegmented::BufferView* TOISegmented::MasterView::createView() { /* reader thread */
|
---|
| 367 | BufferView* bv = new BufferView(this);
|
---|
[1692] | 368 | pthread_mutex_lock(&views_mutex);
|
---|
[1690] | 369 | allViews.insert(bv);
|
---|
[1692] | 370 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 371 | updateView(bv);
|
---|
| 372 | return bv;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | void TOISegmented::MasterView::updateView(BufferView* bv) { /* reader thread */
|
---|
| 376 | pthread_mutex_lock(&views_mutex);
|
---|
| 377 |
|
---|
| 378 | int oldBegin = bv->sn0;
|
---|
| 379 | int oldEnd = bv->sn0 + bv->segmentSize * bv->segments.size();
|
---|
| 380 |
|
---|
| 381 | for (vector<BufferSegment*>::iterator i = bv->segments.begin();
|
---|
| 382 | i != bv->segments.end(); i++) {
|
---|
| 383 | (*i)->decRefCount();
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | bv->segments.clear();
|
---|
| 387 |
|
---|
[1690] | 388 | // utiliser firstNeeded de toutes les vues pour faire le menage chez
|
---|
[1689] | 389 | // nous.
|
---|
[1690] | 390 |
|
---|
[1692] | 391 | // A condition que tous les consumers se soient fait connaitre...
|
---|
[1689] | 392 |
|
---|
[1692] | 393 | if (nConsumers == allViews.size()) {
|
---|
| 394 | int firstNeeded = MAXINT;
|
---|
| 395 | for (set<BufferView*>::iterator i = allViews.begin();
|
---|
| 396 | i != allViews.end(); i++) {
|
---|
| 397 | if ((*i)->firstNeeded < firstNeeded) firstNeeded = (*i)->firstNeeded;
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | LOG(cout << "MasterView : firstNeeded = " << firstNeeded << endl);
|
---|
| 401 |
|
---|
| 402 | vector<BufferSegment*>::iterator j = segments.begin();
|
---|
| 403 | bool clean = false;
|
---|
| 404 | for (vector<BufferSegment*>::iterator i = segments.begin();
|
---|
| 405 | i != segments.end(); i++) {
|
---|
[1700] | 406 | //LOG(cout << "Updating : rc = " << (*i)->getRefCount() << " sn0 = " << (*i)->sn0 << endl;);
|
---|
| 407 | if (((*i)->sn0+(*i)->bufferSize <= firstNeeded) && ((*i)->getRefCount() == 1)) {
|
---|
[1692] | 408 | clean = true;
|
---|
[1700] | 409 | (*i)->decRefCount();
|
---|
| 410 | delete (*i);
|
---|
[1692] | 411 | j = i;
|
---|
| 412 | }
|
---|
| 413 | }
|
---|
[1700] | 414 | j++;
|
---|
[1690] | 415 | if (clean) {
|
---|
| 416 | segments.erase(segments.begin(),j);
|
---|
| 417 | sn0 = (*segments.begin())->sn0;
|
---|
[1692] | 418 | LOG(cout << "MasterView : purged until " << sn0 << endl);
|
---|
[1690] | 419 | }
|
---|
[1692] | 420 | } else {
|
---|
| 421 | LOG(cout << "MasterView : not yet all consumer thread known "<< allViews.size()
|
---|
| 422 | << "/" << nConsumers << endl);
|
---|
| 423 | }
|
---|
[1690] | 424 |
|
---|
| 425 | for (vector<BufferSegment*>::iterator i = segments.begin();
|
---|
[1689] | 426 | i != segments.end(); i++) {
|
---|
| 427 | if ( (*i)->sn0+(*i)->bufferSize > bv->firstNeeded ) {
|
---|
| 428 | (*i)->incRefCount();
|
---|
| 429 | bv->segments.push_back(*i);
|
---|
| 430 | }
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | bv->sn0 = -1;
|
---|
| 434 | int newEnd = -1;
|
---|
| 435 | if (segments.size() > 0) {
|
---|
| 436 | bv->sn0 = bv->segments[0]->sn0;
|
---|
| 437 | newEnd = bv->sn0 + bv->segmentSize * bv->segments.size();
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[1692] | 440 | if (bv->sn0 > oldBegin) { // nettoyage de fait, reveiller le writer thread si besoin
|
---|
| 441 | signalWrite();
|
---|
[1689] | 442 | }
|
---|
| 443 |
|
---|
[1692] | 444 | LOG(cout << "sync for " << hex << bv << dec << " : "
|
---|
| 445 | << oldBegin << " - " << oldEnd << " --> "
|
---|
| 446 | << bv->sn0 << " - " << newEnd << endl);
|
---|
| 447 |
|
---|
| 448 | if (newEnd > oldEnd) { // Nouveautes, reveiller les reader threads si besoin
|
---|
[1689] | 449 | signalWaitingViews();
|
---|
| 450 | }
|
---|
[1692] | 451 | pthread_mutex_unlock(&views_mutex);
|
---|
[1689] | 452 | }
|
---|
| 453 |
|
---|
[1692] | 454 | void TOISegmented::MasterView::checkDeadLock() { /* views locked */
|
---|
[1689] | 455 | // There is a possible deadlock if no view can free old segments
|
---|
| 456 | // and we are waiting for write.
|
---|
| 457 |
|
---|
| 458 | // we need to record "wont need before" for each view, and
|
---|
| 459 | // signal deadlock if any view that needs first segment data is sleeping
|
---|
| 460 | // while we are asleep
|
---|
| 461 |
|
---|
[1692] | 462 | pthread_mutex_lock(&read_wait_mutex);
|
---|
| 463 | if (!waitingOnWrite) {
|
---|
| 464 | pthread_mutex_unlock(&read_wait_mutex);
|
---|
| 465 | return; // no problem, there is an active writer
|
---|
| 466 | }
|
---|
[1689] | 467 |
|
---|
| 468 | // Is any sleeping view needing our first segment ?
|
---|
| 469 |
|
---|
[1692] | 470 | for (set<BufferView*>::iterator i=allViews.begin();
|
---|
| 471 | i != allViews.end(); i++) {
|
---|
| 472 | if ((*i)->waiting && (*i)->firstNeeded < sn0+segmentSize) {
|
---|
[1689] | 473 | cout << "**** DEADLOCK detected ****" << endl;
|
---|
| 474 | cout << "We are waiting on write (buffer is full)"<< endl;
|
---|
| 475 | cout << "but a waiting reader still needs our first segment" << endl;
|
---|
| 476 | cout << "restart with bigger buffers" << endl;
|
---|
| 477 | abort();
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
[1692] | 480 | pthread_mutex_unlock(&read_wait_mutex);
|
---|
[1689] | 481 | }
|
---|
| 482 |
|
---|
| 483 |
|
---|
| 484 |
|
---|
| 485 | void TOISegmented::MasterView::BufferDestroy(void* p) {
|
---|
| 486 | BufferView* bv = (BufferView*) p;
|
---|
| 487 | delete bv;
|
---|
| 488 | }
|
---|