From b0dd82c6670b04f5fd30712856d5d974c6b439b4 Mon Sep 17 00:00:00 2001 From: Dhanya Thattil <33750417+thattil@users.noreply.github.com> Date: Thu, 17 Sep 2020 13:55:20 +0200 Subject: [PATCH] removing sem_wait in acquire (#182) --- slsDetectorSoftware/src/DetectorImpl.cpp | 72 +++++------------------- slsDetectorSoftware/src/DetectorImpl.h | 12 +--- 2 files changed, 16 insertions(+), 68 deletions(-) diff --git a/slsDetectorSoftware/src/DetectorImpl.cpp b/slsDetectorSoftware/src/DetectorImpl.cpp index b576b0237..2d701598a 100644 --- a/slsDetectorSoftware/src/DetectorImpl.cpp +++ b/slsDetectorSoftware/src/DetectorImpl.cpp @@ -435,7 +435,6 @@ void DetectorImpl::readFrameFromReceiver() { bool gapPixels = multi_shm()->gapPixels; LOG(logDEBUG) << "Gap pixels: " << gapPixels; - int nX = 0; int nY = 0; int nDetPixelsX = 0; @@ -464,7 +463,6 @@ void DetectorImpl::readFrameFromReceiver() { runningList[i] = false; } } - int numConnected = numRunning; bool data = false; bool completeImage = false; char *image = nullptr; @@ -482,14 +480,7 @@ void DetectorImpl::readFrameFromReceiver() { uint32_t currentSubFrameIndex = -1, coordX = -1, coordY = -1, flippedDataX = -1; - // wait for real time acquisition to start - bool running = true; - sem_wait(&sem_newRTAcquisition); - if (getJoinThreadFlag()) { - running = false; - } - - while (running) { + while (numRunning != 0) { // reset data data = false; if (multiframe != nullptr) { @@ -658,26 +649,6 @@ void DetectorImpl::readFrameFromReceiver() { pCallbackArg); delete thisData; } - - // all done - if (numRunning == 0) { - // let main thread know that all dummy packets have been received - //(also from external process), - // main thread can now proceed to measurement finished call back - sem_post(&sem_endRTAcquisition); - // wait for next scan/measurement, else join thread - sem_wait(&sem_newRTAcquisition); - // done with complete acquisition - if (getJoinThreadFlag()) { - running = false; - } else { - // starting a new scan/measurement (got dummy data) - for (size_t i = 0; i < zmqSocket.size(); ++i) { - runningList[i] = connectList[i]; - } - numRunning = numConnected; - } - } } // Disconnect resources @@ -1030,17 +1001,11 @@ int DetectorImpl::acquire() { struct timespec begin, end; clock_gettime(CLOCK_REALTIME, &begin); - // in the real time acquisition loop, processing thread will wait for a - // post each time - sem_init(&sem_newRTAcquisition, 1, 0); - // in the real time acquistion loop, main thread will wait for - // processing thread to be done each time (which in turn waits for - // receiver/ext process) - sem_init(&sem_endRTAcquisition, 1, 0); - bool receiver = Parallel(&Module::getUseReceiverFlag, {}).squash(false); - setJoinThreadFlag(false); + if (dataReady == nullptr) { + setJoinThreadFlag(false); + } // verify receiver is idle if (receiver) { @@ -1050,13 +1015,11 @@ int DetectorImpl::acquire() { } } - startProcessingThread(); + startProcessingThread(receiver); // start receiver if (receiver) { Parallel(&Module::startReceiver, {}); - // let processing thread listen to these packets - sem_post(&sem_newRTAcquisition); } // start and read all @@ -1071,18 +1034,13 @@ int DetectorImpl::acquire() { // stop receiver if (receiver) { Parallel(&Module::stopReceiver, {}); - if (dataReady != nullptr) { - sem_wait(&sem_endRTAcquisition); // waits for receiver's - } - // external process to be - // done sending data to gui - Parallel(&Module::incrementFileIndex, {}); } - // waiting for the data processing thread to finish! - setJoinThreadFlag(true); - sem_post(&sem_newRTAcquisition); + // let the progress thread (no callback) know acquisition is done + if (dataReady == nullptr) { + setJoinThreadFlag(true); + } dataProcessingThread.join(); if (acquisition_finished != nullptr) { @@ -1092,9 +1050,6 @@ int DetectorImpl::acquire() { acquisition_finished(progress, status, acqFinished_p); } - sem_destroy(&sem_newRTAcquisition); - sem_destroy(&sem_endRTAcquisition); - clock_gettime(CLOCK_REALTIME, &end); LOG(logDEBUG1) << "Elapsed time for acquisition:" << ((end.tv_sec - begin.tv_sec) + @@ -1115,12 +1070,13 @@ void DetectorImpl::printProgress(double progress) { std::cout << '\r' << std::flush; } -void DetectorImpl::startProcessingThread() { - dataProcessingThread = std::thread(&DetectorImpl::processData, this); +void DetectorImpl::startProcessingThread(bool receiver) { + dataProcessingThread = + std::thread(&DetectorImpl::processData, this, receiver); } -void DetectorImpl::processData() { - if (Parallel(&Module::getUseReceiverFlag, {}).squash(false)) { +void DetectorImpl::processData(bool receiver) { + if (receiver) { if (dataReady != nullptr) { readFrameFromReceiver(); } diff --git a/slsDetectorSoftware/src/DetectorImpl.h b/slsDetectorSoftware/src/DetectorImpl.h index 7e0613cee..a0f7144af 100644 --- a/slsDetectorSoftware/src/DetectorImpl.h +++ b/slsDetectorSoftware/src/DetectorImpl.h @@ -276,7 +276,7 @@ class DetectorImpl : public virtual slsDetectorDefs { * Combines data from all readouts and gives it to the gui * or just gives progress of acquisition by polling receivers */ - void processData(); + void processData(bool receiver); /** * Convert raw file @@ -352,7 +352,7 @@ class DetectorImpl : public virtual slsDetectorDefs { void printProgress(double progress); - void startProcessingThread(); + void startProcessingThread(bool receiver); /** * Check if processing thread is ready to join main thread @@ -387,14 +387,6 @@ class DetectorImpl : public virtual slsDetectorDefs { /** ZMQ Socket - Receiver to Client */ std::vector> zmqSocket; - /** semaphore to let postprocessing thread continue for next - * scan/measurement */ - sem_t sem_newRTAcquisition; - - /** semaphore to let main thread know it got all the dummy packets (also - * from ext. process) */ - sem_t sem_endRTAcquisition; - /** mutex to synchronize main and data processing threads */ mutable std::mutex mp;