void Device::initTaskingSystem(size_t numThreads) { Lock<MutexSys> lock(g_mutex); if (numThreads == 0) g_num_threads_map[this] = std::numeric_limits<size_t>::max(); else g_num_threads_map[this] = numThreads; /* create task scheduler */ size_t maxNumThreads = getMaxNumThreads(); TaskScheduler::create(maxNumThreads,State::set_affinity,State::start_threads); #if USE_TASK_ARENA arena = make_unique(new tbb::task_arena((int)min(maxNumThreads,TaskScheduler::threadCount()))); #endif }
void Device::exitTaskingSystem() { Lock<MutexSys> lock(g_mutex); g_num_threads_map.erase(this); /* terminate tasking system */ if (g_num_threads_map.size() == 0) { TaskScheduler::destroy(); } /* or configure new number of threads */ else { size_t maxNumThreads = getMaxNumThreads(); TaskScheduler::create(maxNumThreads,State::set_affinity,State::start_threads); } #if USE_TASK_ARENA arena.reset(); #endif }
void AHFilter::controlEvents(){ int i, createReturn; pthread_mutex_lock(&threadsCreated); // Creating worker threads for (int i =0; i < getMaxNumThreads(); i++){ void *arg = (void*) malloc(sizeof(int)); memcpy(arg, &i, sizeof(int)); createReturn = pthread_create(&(workerThreads[i]), NULL, callThread, arg); if (createReturn){ printf("ERROR: Return code from pthread_create() is %d\n", createReturn); exit(-1); } } int j; // while we do not receive EOW from all channel we // have to still listening them and creating events while(getNumReceivedEOW() < AHGetNumInputPorts()){ int ahReadReturn; int numReceived = 0; int isMsg = 0; int probeRet = 0; // for(j = 0; j < 10; j++){ // verify if we have messages in one of the channels for(i = 0; i < AHGetNumInputPorts(); i++) { if(eventLists[i].getEOW() == true)continue; void *buf = NULL; char *label = NULL; probeRet = ahProbe((InputPortHandler) i); if(probeRet == 0){ continue; } isMsg = 1; ahReadReturn = ahReadBufferMalloc((InputPortHandler) i, &buf, &label); // ahReadReturn = ahReadNonBlockingBufferMalloc((InputPortHandler) i, &buf, &label); switch ( ahReadReturn ){ case 0: continue; break; case EOW: addNumReceivedEOW(); eventLists[i].setEOW(true); printf("Receive EOW = %d and numInputPorts = %d\n", getNumReceivedEOW(), AHGetNumInputPorts()); fflush(stdout); break; default: numReceived++; isMsg = 1; AHEvent *event; assert(buf != NULL); if(label != NULL){ string labelString = label; event = new AHEvent (buf, ahReadReturn, (streamHandlerType) i, &labelString); }else{ // int *mesg = (int*) buf; // cout << "Creating a received event. Mesg = " << mesg[0] <<endl; event = new AHEvent (buf, ahReadReturn, (streamHandlerType) i); } assert(event != NULL); eventLists[i].insert(event); sem_post(&eventsToBeProcessed); break; } } dispachedEvents.sendAllEvents(); if(isMsg){ isMsg = 0; n_noReceive = 0; }else{ if(n_noReceive < 10){ n_noReceive +=1 } usleep(100*n_noReceive); } } pthread_mutex_lock(&threadsCreated); int semValue; // At this point no other event will be created in this filter. // I will give the threads the permission to get another event, // but this event is NULL, meanning that there no more events // to be processed. Bellow I just wait them die for(int i = 0; i < getMaxNumThreads(); i++){ sem_post(&eventsToBeProcessed); } do{ // Wait all threads finishing and send the created events dispachedEvents.sendAllEvents(); sem_getvalue(&aliveThreads, &semValue); }while(semValue != getMaxNumThreads()); // Dar um Join para ter certeza // Now I will send the last created events dispachedEvents.sendAllEvents(); }