From 57d65fd59cbed72d68814ac4522464cbdd30f346 Mon Sep 17 00:00:00 2001 From: l_maliakal_d Date: Wed, 4 Sep 2013 13:10:14 +0000 Subject: [PATCH] semaphores , no usleep in receiver git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@660 951219d9-93cf-4727-9268-0efd64621fa3 --- .../eigerDetectorServer/svnInfoEiger.h | 6 +- .../gotthardDetectorServer/svnInfoGotthard.h | 6 +- .../moenchDetectorServer/svnInfoMoench.h | 6 +- .../multiSlsDetector/multiSlsDetector.cpp | 25 ++ .../multiSlsDetector/multiSlsDetector.h | 6 + .../mythenDetectorServer/svnInfoMythen.h | 6 +- .../slsDetector/slsDetector.cpp | 23 ++ slsDetectorSoftware/slsDetector/slsDetector.h | 6 + .../slsDetector/slsDetectorBase.h | 6 + .../slsDetector/slsDetectorUtils.cpp | 21 +- slsDetectorSoftware/slsDetector/svnInfoLib.h | 6 +- .../slsDetectorAnalysis/postProcessing.cpp | 17 +- .../slsReceiver/slsReceiverFunctionList.cpp | 391 +++++++++--------- .../slsReceiver/slsReceiverFunctionList.h | 10 +- .../slsReceiver/slsReceiver_funcs.cpp | 27 ++ .../slsReceiver/slsReceiver_funcs.h | 4 + .../slsReceiver/svnInfoReceiver.h | 6 +- 17 files changed, 338 insertions(+), 234 deletions(-) diff --git a/slsDetectorSoftware/eigerDetectorServer/svnInfoEiger.h b/slsDetectorSoftware/eigerDetectorServer/svnInfoEiger.h index e263acc67..ca8f93ebe 100644 --- a/slsDetectorSoftware/eigerDetectorServer/svnInfoEiger.h +++ b/slsDetectorSoftware/eigerDetectorServer/svnInfoEiger.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware/eigerDetectorServer" //#define SVNREPPATH "" #define SVNREPUUID "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x658 +//#define SVNREV 0x659 //#define SVNKIND "" //#define SVNSCHED "" #define SVNAUTH "l_maliakal_d" -#define SVNREV 0x658 -#define SVNDATE 0x20130827 +#define SVNREV 0x659 +#define SVNDATE 0x20130829 // diff --git a/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h b/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h index ccd7d74a7..f15897329 100644 --- a/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h +++ b/slsDetectorSoftware/gotthardDetectorServer/svnInfoGotthard.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware/gotthardDetectorServer" //#define SVNREPPATH "" #define SVNREPUUID "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x658 +//#define SVNREV 0x659 //#define SVNKIND "" //#define SVNSCHED "" #define SVNAUTH "l_maliakal_d" -#define SVNREV 0x658 -#define SVNDATE 0x20130827 +#define SVNREV 0x659 +#define SVNDATE 0x20130829 // diff --git a/slsDetectorSoftware/moenchDetectorServer/svnInfoMoench.h b/slsDetectorSoftware/moenchDetectorServer/svnInfoMoench.h index f84eb370a..e38e6a6b8 100644 --- a/slsDetectorSoftware/moenchDetectorServer/svnInfoMoench.h +++ b/slsDetectorSoftware/moenchDetectorServer/svnInfoMoench.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware/moenchDetectorServer" //#define SVNREPPATH "" #define SVNREPUUID "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x658 +//#define SVNREV 0x659 //#define SVNKIND "" //#define SVNSCHED "" #define SVNAUTH "l_maliakal_d" -#define SVNREV 0x658 -#define SVNDATE 0x20130827 +#define SVNREV 0x659 +#define SVNDATE 0x20130829 // diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index 35acb95ef..c1ba4aa5c 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -4346,7 +4346,32 @@ int multiSlsDetector::stopReceiver(){ } +slsDetectorDefs::runStatus multiSlsDetector::startReceiverReadout(){ + runStatus s,s1; + + if (thisMultiDetector->masterPosition>=0) + if (detectors[thisMultiDetector->masterPosition]){ + s = detectors[thisMultiDetector->masterPosition]->startReceiverReadout(); + if(detectors[thisMultiDetector->masterPosition]->getErrorMask()) + setErrorMask(getErrorMask()|(1<masterPosition)); + return s; + } + + if (detectors[0]) s=detectors[0]->startReceiverReadout(); + + for (int i=0; inumberOfDetectors; i++) { + s1=detectors[i]->startReceiverReadout(); + if(detectors[i]->getErrorMask()) + setErrorMask(getErrorMask()|(1<getInt(fnum,retval); + if(retval!=-1) + s=(runStatus)retval; + if(ret==FORCE_UPDATE) + ret=updateReceiver(); + } + + return s; +} + + int slsDetector::detectorSendToReceiver(bool set){ int fnum; if(set) fnum=F_START_RECEIVER; diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index d288574e4..02a150886 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -1474,6 +1474,12 @@ class slsDetector : public slsDetectorUtils, public energyConversion { */ int stopReceiver(); + /** Sets the receiver to start any readout remaining in the fifo and + * change status to transmitting. + * The status changes to run_finished when fifo is empty + */ + runStatus startReceiverReadout(); + /** Sets(false) or Resets(true) the CPU bit in detector \returns OK or FAIL */ diff --git a/slsDetectorSoftware/slsDetector/slsDetectorBase.h b/slsDetectorSoftware/slsDetector/slsDetectorBase.h index 525843e3a..241bf0552 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorBase.h +++ b/slsDetectorSoftware/slsDetector/slsDetectorBase.h @@ -485,6 +485,12 @@ class slsDetectorBase : public virtual slsDetectorDefs, public virtual errorDef */ virtual int setReadReceiverFrequency(int getFromReceiver, int i=-1)=0; + /** Sets the receiver to start any readout remaining in the fifo and + * change status to transmitting. + * The status changes to run_finished when fifo is empty + */ + virtual runStatus startReceiverReadout()=0; + /** returns detector type string from detector type index \param t string can be Mythen, Pilatus, Eiger, Gotthard, Agipd, Unknown diff --git a/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp b/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp index 477cf24a2..484b12289 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp @@ -311,8 +311,9 @@ void slsDetectorUtils::acquire(int delflag){ break; - pthread_mutex_lock(&mg); + if(setReceiverOnline()==OFFLINE_FLAG){ + pthread_mutex_lock(&mg); // wait until data processing thread has finished the data #ifdef VERBOSE @@ -329,11 +330,21 @@ void slsDetectorUtils::acquire(int delflag){ if((*correctionMask)&(1< prevCaught) newData=true; else @@ -485,6 +487,8 @@ void* postProcessing::processData(int delflag) { pthread_mutex_lock(&mg); int* receiverData = readFrameFromReceiver(currentfName,currentfIndex);//if(currentfIndex!=-1)cout<<"--currentfIndex:"<ShutDownSocket(); pthread_join(listening_thread,NULL); pthread_join(writing_thread,NULL); } //change status - while(1){ - if(!pthread_mutex_trylock(&(status_mutex))){ - status = IDLE; - pthread_mutex_unlock(&(status_mutex)); - break; - } - } + pthread_mutex_lock(&status_mutex); + status = IDLE; + pthread_mutex_unlock(&(status_mutex)); + + //semaphore destroy + sem_post(&smp); + sem_destroy(&smp); + + cout << "Receiver Stopped.\nStatus:" << status << endl; return OK; } @@ -404,7 +395,7 @@ int slsReceiverFunctionList::startListening(){ // from the manual sysctl -w net.core.rmem_max=16777216 sysctl -w net.core.netdev_max_backlog=250000 - */ + */ //creating udp socket if (strchr(eth,'.')!=NULL) strcpy(eth,""); @@ -423,26 +414,19 @@ int slsReceiverFunctionList::startListening(){ #ifdef VERBOSE std::cout<< "Could not create UDP socket "<< server_port << std::endl; #endif - while(1){ - if(!pthread_mutex_trylock(&(status_mutex))){ - listening_thread_running = -1; - pthread_mutex_unlock(&(status_mutex)); - break; - } - } + pthread_mutex_lock(&status_mutex); + listening_thread_running = -1; + pthread_mutex_unlock(&status_mutex); break; } while (receiver_threads_running) { - while(1){ - if(!pthread_mutex_trylock(&(status_mutex))){ - listening_thread_running = 1; - pthread_mutex_unlock(&(status_mutex)); - break; - } - } + pthread_mutex_lock(&status_mutex); + listening_thread_running = 1; + pthread_mutex_unlock(&(status_mutex)); + if (!fifofree->isEmpty()) { fifofree->pop(buffer); @@ -455,9 +439,9 @@ int slsReceiverFunctionList::startListening(){ //start for each scan if(!measurementStarted){ if(!frameIndexOffset) - startFrameIndex = ((uint32_t)(*((uint32_t*)buffer))); + startFrameIndex = ((uint32_t)(*((uint32_t*)buffer))); else - startFrameIndex = ((((uint32_t)(*((uint32_t*)buffer))) & (frameIndexMask)) >> frameIndexOffset); + startFrameIndex = ((((uint32_t)(*((uint32_t*)buffer))) & (frameIndexMask)) >> frameIndexOffset); cout<<"startFrameIndex:"<Disconnect(); @@ -539,6 +520,8 @@ int slsReceiverFunctionList::startWriting(){ if(sfilefd) sfilefd=NULL; strcpy(savefilename,""); + + //reset this before each acq or you send old data guiData = NULL; guiDataReady=0; @@ -563,6 +546,7 @@ int slsReceiverFunctionList::startWriting(){ cout << "Ready!" << endl; + //will always run till acquisition over and then runs till fifo is empty while(receiver_threads_running || (!fifo->isEmpty())){ //start a new file @@ -581,13 +565,9 @@ int slsReceiverFunctionList::startWriting(){ if (NULL == (sfilefd = fopen((const char *) (savefilename), "w"))){ cout << "Error: Could not create file " << savefilename << endl; - while(1){ - if(!pthread_mutex_trylock(&(status_mutex))){ - writing_thread_running = -1; - pthread_mutex_unlock(&(status_mutex)); - break; - } - } + pthread_mutex_lock(&status_mutex); + writing_thread_running = -1; + pthread_mutex_unlock(&(status_mutex)); break; } @@ -612,13 +592,10 @@ int slsReceiverFunctionList::startWriting(){ } } - while(1){ - if(!pthread_mutex_trylock(&(status_mutex))){ - writing_thread_running = 1; - pthread_mutex_unlock(&(status_mutex)); - break; - } - } + pthread_mutex_lock(&status_mutex); + writing_thread_running = 1; + pthread_mutex_unlock(&(status_mutex)); + //if(prevframenum != 0){ if(framesCaught){ @@ -628,99 +605,93 @@ int slsReceiverFunctionList::startWriting(){ } - //pop fifo if(!fifo->isEmpty()){ if(fifo->pop(wbuf)){ - framesCaught++; - totalFramesCaught++; - if(!frameIndexOffset) - currframenum = (uint32_t)(*((uint32_t*)wbuf)); - else - currframenum = (((uint32_t)(*((uint32_t*)wbuf))) & (frameIndexMask)) >> frameIndexOffset; + framesCaught++; + totalFramesCaught++; + if(!frameIndexOffset) + currframenum = (uint32_t)(*((uint32_t*)wbuf)); + else + currframenum = (((uint32_t)(*((uint32_t*)wbuf))) & (frameIndexMask)) >> frameIndexOffset; - //cout<<"**************curreframenm:"<push(wbuf); + } + } + framesInFile++; + fifofree->push(wbuf); } } else{//cout<<"************************fifo empty**********************************"< #include #include +#include /** @@ -202,6 +203,10 @@ public: */ int setNFrameToGui(int i){if(i>=0) nFrameToGui = i; return nFrameToGui;}; + /** set status to transmitting and + * when fifo is empty later, sets status to run_finished */ + void startReadout(); + private: /** detector type */ @@ -385,8 +390,11 @@ private: /** frame index offset */ int frameIndexOffset; + /** semaphore to synchronize writer and guireader threads */ + sem_t smp; - + /** guiDataReady mutex */ + pthread_mutex_t dataReadyMutex; public: diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp index 6785be3fd..c0d4410cf 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.cpp @@ -285,6 +285,7 @@ int slsReceiverFuncs::function_table(){ flist[F_ENABLE_FILE_WRITE] = &slsReceiverFuncs::enable_file_write; flist[F_GET_ID] = &slsReceiverFuncs::get_version; flist[F_CONFIGURE_MAC] = &slsReceiverFuncs::set_short_frame; + flist[F_START_READOUT] = &slsReceiverFuncs::start_readout; //General Functions flist[F_LOCK_SERVER] = &slsReceiverFuncs::lock_receiver; @@ -1374,7 +1375,33 @@ int slsReceiverFuncs::get_version(){ +int slsReceiverFuncs::start_readout(){ + ret=OK; + enum runStatus retval; + // execute action if the arguments correctly arrived +#ifdef SLS_RECEIVER_FUNCTION_LIST + slsReceiverList->startReadout(); + retval = slsReceiverList->getStatus(); + if((retval == TRANSMITTING) || (retval == RUN_FINISHED)) + ret = OK; + else + ret = FAIL; +#endif + + if(socket->differentClients){ + cout << "Force update" << endl; + ret=FORCE_UPDATE; + } + + // send answer + socket->SendDataOnly(&ret,sizeof(ret)); + socket->SendDataOnly(&retval,sizeof(retval)); + //return ok/fail + return ret; + + +} diff --git a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h index 92a6adc0c..3a8b68acb 100644 --- a/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h +++ b/slsDetectorSoftware/slsReceiver/slsReceiver_funcs.h @@ -143,6 +143,10 @@ public: /** Get Version */ int get_version(); + /** set status to transmitting and + * when fifo is empty later, sets status to run_finished */ + int start_readout(); + //General Functions /** Locks Receiver */ diff --git a/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h b/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h index 5bd1bda91..df8c76efa 100644 --- a/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h +++ b/slsDetectorSoftware/slsReceiver/svnInfoReceiver.h @@ -2,10 +2,10 @@ #define SVNURL "file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware/slsReceiver" //#define SVNREPPATH "" #define SVNREPUUID "951219d9-93cf-4727-9268-0efd64621fa3" -//#define SVNREV 0x658 +//#define SVNREV 0x659 //#define SVNKIND "" //#define SVNSCHED "" #define SVNAUTH "l_maliakal_d" -#define SVNREV 0x658 -#define SVNDATE 0x20130827 +#define SVNREV 0x659 +#define SVNDATE 0x20130829 //