1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
2 |
|
---|
3 | // ArchTOIPipe (C) CEA/DAPNIA/SPP IN2P3/LAL
|
---|
4 | // Eric Aubourg
|
---|
5 | // Christophe Magneville
|
---|
6 | // Reza Ansari
|
---|
7 | // $Id: toisegment.h,v 1.11 2001-11-09 23:13:15 aubourg Exp $
|
---|
8 |
|
---|
9 | #ifndef TOISEGMENT_H
|
---|
10 | #define TOISEGMENT_H
|
---|
11 |
|
---|
12 | #include <vector>
|
---|
13 | #include <set>
|
---|
14 |
|
---|
15 | #include "toi.h"
|
---|
16 |
|
---|
17 | // ------------ TOISegmented ------------------------------
|
---|
18 | // Classe de TOI pour echantillonage regulier, avec buffer
|
---|
19 | // segmente pour optimiser le multithread et limiter les
|
---|
20 | // verrous.
|
---|
21 | // Il faut que les fournisseurs fassent arriver les donnees
|
---|
22 | // par samplenum croissant et continu.
|
---|
23 | // --------------------------------------------------------
|
---|
24 |
|
---|
25 | class TOISegmented : public TOIRegular {
|
---|
26 | public:
|
---|
27 | TOISegmented(int bufsz=256, int maxseg=20);
|
---|
28 | TOISegmented(string nm, int bufsz=256, int maxseg=20);
|
---|
29 | TOISegmented(char* cnm, int bufsz=256, int maxseg=20);
|
---|
30 | ~TOISegmented();
|
---|
31 |
|
---|
32 | virtual double getData(int i);
|
---|
33 | virtual void getData(int i, double& data, uint_8& flag);
|
---|
34 | virtual void getData(int i, int n, double* data, uint_8* flg=0);
|
---|
35 | virtual void putData(int i, double value, uint_8 flag=0);
|
---|
36 | virtual void putData(int i, int n, double const* val, uint_8 const* flg=0);
|
---|
37 | virtual void wontNeedBefore(int i);
|
---|
38 | virtual void putDone();
|
---|
39 | virtual void addConsumer(TOIProcessor*);
|
---|
40 |
|
---|
41 | // Methodes ignorees car on reimplemente les methodes de base
|
---|
42 | virtual DataStatus isDataAvail(int iStart, int iEnd);
|
---|
43 | virtual DataStatus isDataAvail(int i);
|
---|
44 | virtual DataStatus isDataAvailNL(int iStart, int iEnd); // abstract
|
---|
45 | virtual void waitForData(int iStart, int iEnd);
|
---|
46 | virtual void waitForData(int i);
|
---|
47 | virtual void waitForAnyData();
|
---|
48 | virtual int nextDataAvail(int iAfter); // abstract
|
---|
49 | virtual bool hasSomeData(); // abstract
|
---|
50 | virtual void doGetData(int i, double& data, uint_8& flag); // abs
|
---|
51 | virtual void doPutData(int i, double value, uint_8 flag=0); // abs
|
---|
52 |
|
---|
53 |
|
---|
54 | protected:
|
---|
55 | class BufferSegment;
|
---|
56 | class BufferView;
|
---|
57 | class MasterView;
|
---|
58 |
|
---|
59 | MasterView* master;
|
---|
60 |
|
---|
61 |
|
---|
62 | class BufferSegment {
|
---|
63 | public:
|
---|
64 | BufferSegment(int sz);
|
---|
65 | ~BufferSegment();
|
---|
66 | static const int NEW = 0;
|
---|
67 | static const int WRITE = 1; // single-thread write access
|
---|
68 | static const int COMMITTED = 2; // multiple read without lock are ok
|
---|
69 |
|
---|
70 | int getStatus() {return status;}
|
---|
71 | void incRefCount();
|
---|
72 | void decRefCount();
|
---|
73 | int getRefCount();
|
---|
74 |
|
---|
75 | void putData(int sn, double data, uint_8 flag);
|
---|
76 | void putData(int sn, int n, double const* data, uint_8 const* flag);
|
---|
77 | inline double getData(int sn);
|
---|
78 | inline uint_8 getFlag(int sn);
|
---|
79 | void getData(int sn, int n, double* data, uint_8* flag);
|
---|
80 |
|
---|
81 | bool isPastEnd(int sn) {
|
---|
82 | return sn >= sn+bufferSize;
|
---|
83 | }
|
---|
84 |
|
---|
85 | private:
|
---|
86 | void checkCommitted() {
|
---|
87 | if (status != COMMITTED)
|
---|
88 | throw(ForbiddenError("TOISegment: Read on not committed buffer segment"));
|
---|
89 | }
|
---|
90 |
|
---|
91 | void checkInRange(int sn) {
|
---|
92 | if (sn < sn0 || sn >= sn+bufferSize)
|
---|
93 | throw(RangeCheckError("TOISegment: out of range access in buffer segment"));
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | int status; // NEW, WRITE, COMMITTED
|
---|
98 | int bufferSize;
|
---|
99 | int sn0; // Samplenum du premier echantillon
|
---|
100 |
|
---|
101 | int refcount; // Nombre de vues qui utilisent
|
---|
102 | pthread_mutex_t refcount_mutex; // Pour modification refcount
|
---|
103 |
|
---|
104 | double* data;
|
---|
105 | uint_8* flags;
|
---|
106 |
|
---|
107 | friend class BufferView;
|
---|
108 | friend class MasterView;
|
---|
109 | };
|
---|
110 |
|
---|
111 | // Master view, gere le lock, et l'ecriture
|
---|
112 | class MasterView {
|
---|
113 | public:
|
---|
114 | MasterView(int bufsz=256, int maxseg=20, string nm="");
|
---|
115 | ~MasterView();
|
---|
116 |
|
---|
117 | void putData(int sn, double data, uint_8 flag);
|
---|
118 | double getData(int sn);
|
---|
119 | uint_8 getFlag(int sn);
|
---|
120 | void getData(int i, int n, double* data, uint_8* flg);
|
---|
121 | void putData(int i, int n, double const* val, uint_8 const* flg);
|
---|
122 | BufferView* getView(); // thread-specific
|
---|
123 | void putDone();
|
---|
124 |
|
---|
125 | protected:
|
---|
126 |
|
---|
127 | friend class BufferView;
|
---|
128 | friend class TOISegmented;
|
---|
129 | string name;
|
---|
130 | void signalWaitingViews(); // views are waiting on read
|
---|
131 | void signalWrite(); // we are waiting on write
|
---|
132 | void nextSegment();
|
---|
133 | void waitForCleaning();
|
---|
134 | BufferView* createView();
|
---|
135 | void updateView(BufferView*); // called on reader thread of the view
|
---|
136 |
|
---|
137 | BufferSegment* currentSegment;
|
---|
138 |
|
---|
139 | int maxSegments;
|
---|
140 | int segmentSize;
|
---|
141 | int sn0; // First sn in first segment
|
---|
142 | vector<BufferSegment*> segments; // Committed segments
|
---|
143 | int nConsumers;
|
---|
144 |
|
---|
145 | pthread_mutex_t views_mutex; // lock for master buffer list access
|
---|
146 | pthread_cond_t write_wait_condv; // waiting for cleaning (on writer thread)
|
---|
147 | pthread_key_t buffer_key; // thread-specific buffer view
|
---|
148 | static void BufferDestroy(void *);
|
---|
149 |
|
---|
150 | pthread_mutex_t read_wait_mutex;
|
---|
151 | pthread_cond_t read_wait_condv;
|
---|
152 |
|
---|
153 | bool waitingOnWrite; // wait on writer thread
|
---|
154 | int waitingViews;
|
---|
155 |
|
---|
156 | set<BufferView*> allViews;
|
---|
157 |
|
---|
158 | void checkDeadLock();
|
---|
159 | };
|
---|
160 |
|
---|
161 |
|
---|
162 | // per-thread read-only view of a buffer set
|
---|
163 | class BufferView {
|
---|
164 | public:
|
---|
165 | BufferView(MasterView*);
|
---|
166 | ~BufferView();
|
---|
167 |
|
---|
168 | double getData(int sn);
|
---|
169 | uint_8 getFlag(int sn);
|
---|
170 | void getData(int i, int n, double* data, uint_8* flg);
|
---|
171 |
|
---|
172 | void wontNeedBefore(int sn);
|
---|
173 |
|
---|
174 | protected:
|
---|
175 | void wait(); // Passe en attente d'un nouveau segment -- lecture
|
---|
176 | void sync(); // recupere les nouveaux segments, resync avec master
|
---|
177 | void ensure(int sn);
|
---|
178 |
|
---|
179 | bool waiting;
|
---|
180 |
|
---|
181 | friend class MasterView;
|
---|
182 | MasterView* master;
|
---|
183 | vector<BufferSegment*> segments; // Committed
|
---|
184 | int sn0;
|
---|
185 | int segmentSize;
|
---|
186 | int firstNeeded;
|
---|
187 | };
|
---|
188 |
|
---|
189 |
|
---|
190 |
|
---|
191 | };
|
---|
192 |
|
---|
193 | /***********************************/
|
---|
194 | /* Inline methods -- BufferSegment */
|
---|
195 | /***********************************/
|
---|
196 |
|
---|
197 | double TOISegmented::BufferSegment::getData(int sn) {
|
---|
198 | checkCommitted();
|
---|
199 | checkInRange(sn);
|
---|
200 | return data[sn-sn0];
|
---|
201 | }
|
---|
202 |
|
---|
203 | uint_8 TOISegmented::BufferSegment::getFlag(int sn) {
|
---|
204 | checkCommitted();
|
---|
205 | checkInRange(sn);
|
---|
206 | return flags[sn-sn0];
|
---|
207 | }
|
---|
208 |
|
---|
209 | #endif
|
---|