| 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: toiprocessor.h,v 1.19 2002-05-14 13:06:57 ansari Exp $ | 
|---|
| 8 |  | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 | #ifndef TOIPROCESSOR_H | 
|---|
| 12 | #define TOIPROCESSOR_H | 
|---|
| 13 |  | 
|---|
| 14 | #include "config.h" | 
|---|
| 15 |  | 
|---|
| 16 | #include <pthread.h> | 
|---|
| 17 |  | 
|---|
| 18 | #include <string> | 
|---|
| 19 | #include <map> | 
|---|
| 20 | #include <iostream.h> | 
|---|
| 21 |  | 
|---|
| 22 | #ifndef NO_SOPHYA | 
|---|
| 23 | #include "tarray.h" | 
|---|
| 24 | using namespace SOPHYA; | 
|---|
| 25 | #endif | 
|---|
| 26 |  | 
|---|
| 27 | #include "toi.h" | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | class TOIProcessor { | 
|---|
| 31 | public: | 
|---|
| 32 | virtual void init(); | 
|---|
| 33 | // Les methodes qui suivent peuvent etre appelees par init | 
|---|
| 34 | protected: | 
|---|
| 35 | int declareInput(string toi); | 
|---|
| 36 | int declareOutput(string toi); | 
|---|
| 37 |  | 
|---|
| 38 | // void declareMultInput(string toi); | 
|---|
| 39 | // Question : multiple output ? | 
|---|
| 40 |  | 
|---|
| 41 | void chkinit() { | 
|---|
| 42 | if (!inited) {init(); afterinit(); inited=true;} | 
|---|
| 43 | } | 
|---|
| 44 | bool inited; | 
|---|
| 45 | virtual void afterinit(); | 
|---|
| 46 |  | 
|---|
| 47 | public: | 
|---|
| 48 | virtual void run(); | 
|---|
| 49 |  | 
|---|
| 50 | virtual int   getMinOut(); | 
|---|
| 51 | virtual int   getMaxOut(); | 
|---|
| 52 | // Les methodes qui suivent peut etre appelees par run | 
|---|
| 53 | protected: | 
|---|
| 54 | #ifndef NO_SOPHYA | 
|---|
| 55 | /* ---- l'interface va etre modifiee, NE PAS UTILISER | 
|---|
| 56 | Array         getData(int toiIndex, int iStart, int iEnd); | 
|---|
| 57 | Array         getError(int toiIndex, int iStart, int iEnd); | 
|---|
| 58 | TArray<int_4> getFlag(int toiIndex, int iStart, int iEnd); | 
|---|
| 59 | l'interface va etre modifiee, NE PAS UTILISER ---- */ | 
|---|
| 60 | #endif | 
|---|
| 61 | // si multiple input (indexed input), tableau 2D. | 
|---|
| 62 |  | 
|---|
| 63 | // Les methodes qui suivent ne peuvent etre | 
|---|
| 64 | // utilisees que sur des entrees simples | 
|---|
| 65 | double        getData(int toiIndex, int i); | 
|---|
| 66 | void          getData(int toiIndex, int i, double &data, uint_8 &flag); | 
|---|
| 67 |  | 
|---|
| 68 | void          getData(int toiIndex, int i, int n, double* d); | 
|---|
| 69 | void          getData(int toiIndex, int i, int n, double* d, uint_8* f); | 
|---|
| 70 |  | 
|---|
| 71 | //RZCMV  double        getError(int toiIndex, int i); | 
|---|
| 72 | //RZCMV  int_4         getFlag(int toiIndex, int i); | 
|---|
| 73 |  | 
|---|
| 74 | void          wontNeedBefore(int i); | 
|---|
| 75 | int           wontNeedValue; | 
|---|
| 76 | void          setNeededHistory(int nsamples); // -1 : disable | 
|---|
| 77 |  | 
|---|
| 78 | void          putData(int toiIndex, int i, double value, uint_8 flag=0); | 
|---|
| 79 | //RZCMV  void          putDataError(int toiIndex, int i, double value, | 
|---|
| 80 | //RZCMV                            double error, int_4 flag=0); | 
|---|
| 81 |  | 
|---|
| 82 | void          putData(int toiIndex, int i, int n, double const* val, | 
|---|
| 83 | uint_8 const* flg=0); | 
|---|
| 84 |  | 
|---|
| 85 | // Gestion des bornes pour les transformations de TOIs... | 
|---|
| 86 | public: | 
|---|
| 87 | // Methodes passees en public par Reza, 14/5/2002 | 
|---|
| 88 | virtual int   calcMinOut(); // Protected ? | 
|---|
| 89 | virtual int   calcMaxOut(); // Protected ? | 
|---|
| 90 |  | 
|---|
| 91 | virtual int   getMinIn(); | 
|---|
| 92 | virtual int   getMaxIn(); | 
|---|
| 93 | protected: | 
|---|
| 94 | // Implementation par defaut | 
|---|
| 95 | int           minOut;    // Cache for calcMinOut() value. Protected ? | 
|---|
| 96 | int           maxOut;    // Cache for calcMaxOut() value. Protected ? | 
|---|
| 97 |  | 
|---|
| 98 | int           upExtra;  // MaxIn - MaxOut : extra samples for processing | 
|---|
| 99 | int           lowExtra; // MinOut - MinIn : extra samples for processing | 
|---|
| 100 |  | 
|---|
| 101 | int           forcedMinIn; // to process a subset of data | 
|---|
| 102 | int           forcedMaxIn; // to process a subset of data | 
|---|
| 103 | void          setSampleSubset(int min, int max); | 
|---|
| 104 |  | 
|---|
| 105 | int           neededHistory; | 
|---|
| 106 |  | 
|---|
| 107 | protected: | 
|---|
| 108 | int           getInputTOIIndex(string toi); | 
|---|
| 109 | int           getOutputTOIIndex(string toi); | 
|---|
| 110 |  | 
|---|
| 111 | //  Methodes rajoutees par Reza 11/3/2001 | 
|---|
| 112 | TOI*          getInputTOI(int toiIndex); | 
|---|
| 113 | TOI*          getOutputTOI(int toiIndex); | 
|---|
| 114 | bool          checkInputTOIIndex(int toiIndex); | 
|---|
| 115 | bool          checkOutputTOIIndex(int toiIndex); | 
|---|
| 116 |  | 
|---|
| 117 | //  Fin rajout Reza 11/3/2001 | 
|---|
| 118 |  | 
|---|
| 119 | void          autoWontNeed(int iCur); | 
|---|
| 120 | int           lastAWN; | 
|---|
| 121 |  | 
|---|
| 122 | public: | 
|---|
| 123 | // Appele par les assembleurs de pipeline | 
|---|
| 124 | virtual void  addInput(string name, TOI* toi); | 
|---|
| 125 | virtual void  addOutput(string name, TOI* toi); | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 | int getNIn()  {chkinit(); return inIx.size();} | 
|---|
| 129 | int getNOut() {chkinit(); return outIx.size();} | 
|---|
| 130 | string getOutName(int i); | 
|---|
| 131 | string getInName(int i); | 
|---|
| 132 |  | 
|---|
| 133 | //  Methodes rajoutees par Reza 11/3/2001 | 
|---|
| 134 | virtual void  PrintStatus(::ostream & os) ; // const plus tard | 
|---|
| 135 | //  Fin rajout Reza 11/3/2001 | 
|---|
| 136 |  | 
|---|
| 137 | virtual void  start(); | 
|---|
| 138 |  | 
|---|
| 139 | TOIProcessor(); | 
|---|
| 140 | virtual ~TOIProcessor(); | 
|---|
| 141 |  | 
|---|
| 142 | void setMinSn(int n) {forcedMinIn = n;} | 
|---|
| 143 | void setMaxSn(int n) {forcedMaxIn = n;} | 
|---|
| 144 |  | 
|---|
| 145 | protected: | 
|---|
| 146 |  | 
|---|
| 147 | map<string, int> inIx; | 
|---|
| 148 | map<string, int> outIx; | 
|---|
| 149 | TOI**  inTOIs; | 
|---|
| 150 | TOI**  outTOIs; | 
|---|
| 151 |  | 
|---|
| 152 | string name; | 
|---|
| 153 | // Thread handling | 
|---|
| 154 | pthread_t        thread; | 
|---|
| 155 | pthread_cond_t   dataReady; | 
|---|
| 156 | pthread_mutex_t  mutex; | 
|---|
| 157 | pthread_mutexattr_t mutattr; | 
|---|
| 158 |  | 
|---|
| 159 | void lock() { | 
|---|
| 160 | pthread_mutex_lock(&mutex); | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | void unlock() { | 
|---|
| 164 | pthread_mutex_unlock(&mutex); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | void wait()   { | 
|---|
| 168 | pthread_cond_wait(&dataReady, &mutex); | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | void notify(); | 
|---|
| 172 |  | 
|---|
| 173 |  | 
|---|
| 174 | friend class TOI; | 
|---|
| 175 |  | 
|---|
| 176 | static void* ThreadStart(void *); | 
|---|
| 177 | void warnPutDone(); | 
|---|
| 178 | }; | 
|---|
| 179 |  | 
|---|
| 180 | inline ::ostream & operator << (::ostream & os, TOIProcessor /*const*/ & toip) | 
|---|
| 181 | { toip.PrintStatus(os); return os; } | 
|---|
| 182 |  | 
|---|
| 183 |  | 
|---|
| 184 | #endif | 
|---|
| 185 |  | 
|---|