[1670] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | #ifndef TOISEGMENT_H
|
---|
| 3 | #define TOISEGMENT_H
|
---|
| 4 |
|
---|
| 5 | #include "toi.h"
|
---|
| 6 |
|
---|
| 7 | // ------------ TOISegmented ------------------------------
|
---|
| 8 | // Classe de TOI pour echantillonage regulier, avec buffer
|
---|
| 9 | // segmente pour optimiser le multithread et limiter les
|
---|
| 10 | // verrous.
|
---|
| 11 | // Il faut que les fournisseurs fassent arriver les donnees
|
---|
| 12 | // par samplenum croissant et continu.
|
---|
| 13 | // --------------------------------------------------------
|
---|
| 14 |
|
---|
| 15 | class TOISegmented : public TOIRegular {
|
---|
| 16 | public:
|
---|
| 17 | TOISegmented();
|
---|
| 18 | TOISegmented(string nm);
|
---|
| 19 | ~TOISegmented();
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | protected:
|
---|
| 23 | class BufferSegment {
|
---|
| 24 | public:
|
---|
| 25 | BufferSegment(int sz);
|
---|
| 26 | ~BufferSegment();
|
---|
| 27 | static const int NEW = 0;
|
---|
| 28 | static const int WRITE = 1;
|
---|
| 29 | static const int COMMITTED = 2;
|
---|
| 30 |
|
---|
| 31 | int getStatus() {return status;}
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | void putData(int sn, double data, uint_8 flag);
|
---|
| 35 | inline double getData(int sn);
|
---|
| 36 | inline uint_8 getFlag(int sn);
|
---|
| 37 |
|
---|
| 38 | private:
|
---|
| 39 | void checkCommitted() {
|
---|
| 40 | if (status != COMMITTED)
|
---|
| 41 | throw(ForbiddenError("TOISegment: Read on not committed buffer segment"));
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | void checkInRange(int sn) {
|
---|
| 45 | if (sn < sn0 || sn >= sn+bufferSize)
|
---|
| 46 | throw(RangeCheckError("TOISegment: out of range access in buffer segment"));
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | int status; // NEW, WRITE, COMMITTED
|
---|
| 51 | int bufferSize;
|
---|
| 52 | int sn0; // Samplenum du premier echantillon
|
---|
| 53 |
|
---|
| 54 | int refcount; // Nombre de vues qui utilisent
|
---|
| 55 | pthread_mutex_t refcount_mutex;
|
---|
| 56 |
|
---|
| 57 | double* data;
|
---|
| 58 | uint_8* flags;
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | // Master view, gere le lock...
|
---|
| 62 | class MasterView {
|
---|
| 63 | public:
|
---|
| 64 | MasterView();
|
---|
| 65 | ~MasterView();
|
---|
| 66 |
|
---|
| 67 | protected:
|
---|
| 68 | int s0;
|
---|
| 69 |
|
---|
| 70 | pthread_mutex_t mutex; // lock for master buffer list access
|
---|
| 71 | pthread_cond_t condv; // waiting for new buffer
|
---|
| 72 | };
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | // per-thread read-only view of a buffer set
|
---|
| 76 | class BufferView {
|
---|
| 77 | public:
|
---|
| 78 |
|
---|
| 79 | void sync();
|
---|
| 80 | protected:
|
---|
| 81 | MasterView master;
|
---|
| 82 | };
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | // Inline methods
|
---|
| 86 |
|
---|
| 87 | double TOISegmented::BufferSegment::getData(int sn) {
|
---|
| 88 | checkCommitted();
|
---|
| 89 | checkInRange(sn);
|
---|
| 90 | return data[sn-sn0];
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | uint_8 TOISegmented::BufferSegment::getFlag(int sn) {
|
---|
| 94 | checkCommitted();
|
---|
| 95 | checkInRange(sn);
|
---|
| 96 | return flags[sn-sn0];
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | #endif
|
---|