00001 #include <iostream>
00002
00003 #include "RodCrate/RodModule.h"
00004 #include "RodCrate/BocCard.h"
00005 #include "RodCrate/TimModule.h"
00006
00007 #include "crateImpl.h"
00008 #include "primListWrapper.h"
00009
00010 namespace SctApi {
00011 #if USE_THREADS
00012 void CrateImpl::pollingLoop() {
00013 std::cout << "POLL: Starting Polling loop\n";
00014
00015
00016 bool synchRods = false;
00017
00018 int synched = 0;
00019
00020 int waitCount = 0;
00021
00022
00023 while(!m_stopPolling) {
00024
00025 if(!m_enablePolling) {
00026 do {
00027 usleep(1000);
00028 } while(!m_enablePolling);
00029 }
00030
00031
00032 int rodMask = 0;
00033
00034 int rods = 0;
00035
00036
00037 #warning "This takes priority over all other lists..."
00038
00039 {
00040 boost::mutex::scoped_lock lock(primQueue_mutex);
00041 if(listQueueAll.size() > 0) {
00042
00043 if(synchRods == false) {
00044 synchRods = true;
00045 synched = 0;
00046
00047 }
00048 }
00049 }
00050
00051
00052 for(RodMap::iterator i=rodMap.begin();i!=rodMap.end();i++){
00053 SctPixelRod::RodModule &rod = *(i->second.second);
00054 RodStatus &status = *(i->second.first);
00055
00056 {
00057 boost::mutex::scoped_lock statLock(status.mutex);
00058 SctPixelRod::PrimState oldPrimState = rod.getPrimState();
00059
00060 try {
00061 boost::mutex::scoped_lock lock(vmeMutex);
00062
00063 rod.primHandler();
00064 } catch (SctPixelRod::BaseException &ex) {
00065 std::cerr << "Exception in polling loop (prim handler): " << ex << std::endl;
00066 #warning "Should do something here!"
00067 } catch (...) {
00068 std::cerr << "Unknown exception in polling loop (prim handler)\n";
00069 #warning "Should do something here!"
00070 }
00071
00072
00073
00074
00075 SctPixelRod::PrimState newPState = rod.getPrimState();
00076
00077 if((newPState == SctPixelRod::PRIM_IDLE || newPState == SctPixelRod::PRIM_WAITING)) {
00078 if(synchRods) {
00079
00080 synched |= 1 << i->first;
00081
00082
00083 } else if(status.currState == MYPRIM_STATE_LOADED) {
00084 boost::shared_ptr<PrimListWrapper> nextPrimList;
00085 {
00086 boost::mutex::scoped_lock lock(primQueue_mutex);
00087 if(status.listQueue.size() > 0) {
00088
00089 nextPrimList = status.listQueue.front();
00090 status.listQueue.pop_front();
00091 }
00092 }
00093
00094 if(nextPrimList) {
00095 try {
00096 boost::mutex::scoped_lock lock(vmeMutex);
00097
00098 rod.sendPrimList(nextPrimList->list.get());
00099 } catch (SctPixelRod::BaseException &ex) {
00100 std::cerr << "Exception in polling loop (send prim list): " << ex << std::endl;
00101 #warning "Should do something here!"
00102 } catch(...) {
00103 std::cerr << "Unknown exception in polling loop (send prim list)\n";
00104 #warning "Should do something here!"
00105 }
00106 }
00107 } else {
00108
00109 }
00110 }
00111
00112 if(oldPrimState != newPState) {
00113
00114
00115
00116 switch(newPState) {
00117 case SctPixelRod::PRIM_LOADED:
00118 case SctPixelRod::PRIM_EXECUTING:
00119 status.currState = MYPRIM_STATE_EXECUTING;
00120
00121 break;
00122 case SctPixelRod::PRIM_IDLE:
00123 case SctPixelRod::PRIM_WAITING:
00124 status.currState = MYPRIM_STATE_COMPLETE;
00125
00126 break;
00127 case SctPixelRod::PRIM_PAUSED:
00128 break;
00129 }
00130 }
00131
00132 try {
00133 boost::mutex::scoped_lock lock(vmeMutex);
00134
00135 SctPixelRod::TextBuffState newTState = rod.textHandler();
00136
00137 while (newTState == SctPixelRod::TEXT_RQ_SET) {
00138 doTextBuffer(rod);
00139
00140
00141 newTState = rod.textHandler();
00142 }
00143 } catch (SctPixelRod::BaseException &ex) {
00144 std::cerr << "Exception in polling loop (text handler): " << ex << std::endl;
00145 #warning "Should do something here!"
00146 } catch (...) {
00147 std::cerr << "Unknown exception in polling loop (text handler)\n";
00148 #warning "Should do something here!"
00149 }
00150
00151 }
00152
00153
00154 sched_yield();
00155
00156 rods ++;
00157 rodMask |= 1 << i->first;
00158 }
00159
00160
00161
00162
00163 if(synchRods && synched == rodMask) {
00164
00165
00166 boost::shared_ptr<PrimListWrapper> globalPrimList;
00167 {
00168 boost::mutex::scoped_lock lock(primQueue_mutex);
00169 globalPrimList = listQueueAll.front();
00170 listQueueAll.pop_front();
00171 }
00172
00173 if(globalPrimList) {
00174
00175 for(RodMap::iterator i=rodMap.begin();i!=rodMap.end();i++){
00176 SctPixelRod::RodModule &rod = *(i->second.second);
00177
00178
00179
00180 RodStatus &status = *(i->second.first);
00181
00182 {
00183 boost::mutex::scoped_lock statLock(status.mutex);
00184 try {
00185 boost::mutex::scoped_lock lock(vmeMutex);
00186
00187 rod.sendPrimList(globalPrimList->list.get());
00188
00189
00190
00191
00192 status.currState = MYPRIM_STATE_LOADED;
00193 } catch (SctPixelRod::BaseException &ex) {
00194 std::cerr << "Exception in polling loop (global send prim list): " << ex << std::endl;
00195 #warning "Should do something here!"
00196 } catch (...) {
00197 std::cerr << "Unknown exception in polling loop (global send prim list)\n";
00198 #warning "Should do something here!"
00199 }
00200 }
00201 }
00202 }
00203
00204
00205 synchRods = false;
00206 }
00207
00208 if(m_stopPolling) {
00209
00210 break;
00211 }
00212
00213 {
00214
00215 boost::mutex::scoped_lock lock(primQueue_mutex);
00216
00217 bool doWait = true;
00218
00219
00220 for(RodMap::iterator i=rodMap.begin();i!=rodMap.end();i++){
00221
00222 RodStatus &status = *(i->second.first);
00223
00224 if(status.currState == MYPRIM_STATE_COMPLETE) {
00225 if(status.listQueue.size() != 0) {
00226
00227 doWait = false;
00228 break;
00229 }
00230 } else {
00231
00232 doWait = false;
00233 break;
00234 }
00235 }
00236
00237
00238 if(doWait) {
00239
00240 waitCount ++;
00241 if(waitCount>5) {
00242 usleep(10);
00243 } else if(waitCount>10) {
00244 usleep(100);
00245 } else if(waitCount>20) {
00246 sleep(1);
00247
00248
00249
00250
00251
00252
00253 }
00254 }
00255 }
00256
00257
00258
00259
00260 }
00261 }
00262 #endif
00263 }