diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index 57a2a31b6..ce4627cb0 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -4958,17 +4958,111 @@ int multiSlsDetector::resetFramesCaught() { } +int multiSlsDetector::createReceivingDataThreads(bool destroy){ + + int numReadouts = thisMultiDetector->numberOfDetectors; + if(getDetectorsType() == EIGER) + numReadouts *= 2; + + //reset masks + killAllReceivingDataThreads = false; + pthread_mutex_lock(&ms); + receivingDataThreadMask = 0x0; + pthread_mutex_unlock(&(ms)); + + //destroy + if(destroy){ +#ifdef DEBUG + cout << "Destroying Receiving Data Thread(s)" << endl; +#endif + killAllReceivingDataThreads = true; + for(int i = 0; i < numReadouts; ++i){ + sem_post(&receivingDataSemaphore[i]); + pthread_join(receivingDataThreads[i],NULL); + sem_destroy(&receivingDataSemaphore[i]); + sem_destroy(&receivingDataSocketsCreatedSemaphore[i]); + sem_destroy(&sem_singlewait[i]); + sem_destroy(&sem_singledone[i]); + delete [] singleframe[i]; +#ifdef DEBUG + cout << "." << flush << endl; +#endif + } + killAllReceivingDataThreads = false; + threadStarted = false; + + cout << "Destroyed Receiving Data Thread(s)" << endl; + } + + //create + else{ +#ifdef DEBUG + cout << "Creating Receiving Data Thread(s)" << endl; +#endif + //reset current index + currentThreadIndex = -1; + + for(int i = 0; i < numReadouts; ++i){ + sem_init(&receivingDataSemaphore[i],1,0); + sem_init(&receivingDataSocketsCreatedSemaphore[i],1,0); + sem_init(&sem_singlewait[i],1,0); + sem_init(&sem_singledone[i],1,0); + threadStarted = false; + currentThreadIndex = i; + if(pthread_create(&receivingDataThreads[i], NULL,startReceivingDataThread, (void*) this)){ + cout << "Could not create receiving data thread with index " << i << endl; + return FAIL; + } + while(!threadStarted); +#ifdef DEBUG + cout << "." << flush << endl; +#endif + } + //cout << "Receiving Data Thread(s) created" << endl; + + for(int i=0;inumberOfDetectors; + if(getDetectorsType() == EIGER) + numReadouts *= 2; + + if(threadStarted){ + for(int i=0;istartReceivingData(); + ((multiSlsDetector*)this_pointer)->startReceivingDataThread(); return this_pointer; } -void multiSlsDetector::startReceivingData(){ +void multiSlsDetector::startReceivingDataThread(){ int ithread = currentThreadIndex; //set current thread value index threadStarted = true; //let calling function know thread started and obtained current - + //cout << ithread << " thread created" << endl; int numReadoutPerDetector = 1; bool jungfrau = false; @@ -4987,111 +5081,129 @@ void multiSlsDetector::startReceivingData(){ //cout << "ZMQ Client of " << ithread << " at " << hostname << endl; singleframe[ithread]=new int[nel]; - - //loop though the half readouts to start sockets - zmq_msg_t message; - int len,idet = 0; - void *context; - void *zmqsocket; - context = zmq_ctx_new(); - zmqsocket = zmq_socket(context, ZMQ_PULL); - zmq_connect(zmqsocket, hostname); // connect to publisher,the publisher server does not have to be started - pthread_mutex_lock(&ms); - receivingDataThreadMask|=(1<<(ithread)); - pthread_mutex_unlock(&ms); - - - //read frame + /* outer loop - loops once for each acquisition */ + //infinite loop, exited only at the end of acquire() while(true){ - sem_wait(&sem_singlewait[ithread]); //wait for it to be copied + zmq_msg_t message; + int len,idet = 0; + void *context; + void *zmqsocket; + context = zmq_ctx_new(); + zmqsocket = zmq_socket(context, ZMQ_PULL); + zmq_connect(zmqsocket, hostname); + //cprintf(BLUE,"%d ZMQ Client Socket at %s\n",ithread, hostname); + sem_post(&receivingDataSocketsCreatedSemaphore[ithread]); - //scan header------------------------------------------------------------------- - zmq_msg_init (&message); - len = zmq_msg_recv(&message, zmqsocket, 0); - if (len == -1) { - zmq_msg_close(&message); - cprintf(RED, "%d message null\n",ithread); - continue; - } - - // error if you print it - // cout << ithread << " header len:"<numberOfDetectors; - - //create threads - /** Data Callback Threads */ - pthread_t receivingDataThreads[numReadouts]; - volatile uint64_t expectedMask = 0x0; - receivingDataThreadMask = 0x0; - currentThreadIndex = -1; + //initializing variables strcpy(currentFileName,""); - for(int i = 0; i < numReadouts; ++i){ - threadStarted = false; - currentThreadIndex = i; - sem_init(&sem_singlewait[i],1,0); - sem_init(&sem_singledone[i],1,0); - if(pthread_create(&receivingDataThreads[i], NULL,startReceivingDataThread, (void*) this)){ - cprintf(RED, "ERROR: Could not create receiving thread with index %d\n",i); - return; - } - while(!threadStarted); - //cout << "Data Thread created successfully for " << i << endl; - expectedMask|=(1<getDataBytes(); @@ -5158,9 +5250,14 @@ void multiSlsDetector::readFrameFromReceiver(){ - sem_post(&dataThreadStartedSemaphore); //let utils:acquire continue to start measurement/acquisition + volatile uint64_t expectedMask = 0x0; + for(int i = 0; i < numReadouts; ++i) + expectedMask|=(1<