Exemple #1
0
// CONSTRUCTOR
//------------------------------------------------------------------------------
JobQueue::JobQueue( uint32_t numWorkerThreads ) :
	m_NumLocalJobsActive( 0 ),
	m_DistributableAvailableJobs( 1024, true ),
	m_DistributableJobsMemoryUsage( 0 ),
	m_DistributedJobsRemote( 1204, true ),
	m_DistributedJobsLocal( 128, true ),
	m_DistributedJobsCancelled( 128, true ),
	m_CompletedJobs( 1024, true ),
	m_CompletedJobsFailed( 1024, true ),
	m_CompletedJobs2( 1024, true ),
	m_CompletedJobsFailed2( 1024, true ),
	m_Workers( numWorkerThreads, false )
{
	WorkerThread::InitTmpDir();

	for ( uint32_t i=0; i<numWorkerThreads; ++i )
	{
		// identify each worker with an id starting from 1
		// (the "main" thread is considered 0)
		uint32_t threadIndex = ( i + 1 );
		WorkerThread * wt = FNEW( WorkerThread( threadIndex ) );
		wt->Init();
		m_Workers.Append( wt );
	}
}
Exemple #2
0
ThreadPool::~ThreadPool( )
{
    // --------------------------------------------------------------
    // Request Termination of all worker threads.
    // --------------------------------------------------------------

    m_mList.lock();
    WorkerThreadList list = m_lstThreads;
    m_mList.unlock();

    WorkerThreadList::iterator it = list.begin(); 
    for (; it != list.end(); ++it)
    {
        if (*it != NULL) 
            (*it)->RequestTerminate();
    }

    while (!list.empty())
    {
        WorkerThread *pThread = list.front();
        list.pop_front();
        if (pThread != NULL) 
        {
            pThread->wait();
            delete pThread;
        }
    }
}
Exemple #3
0
void Controller::startWorkInAThread()
{
    WorkerThread *workerThread = new WorkerThread(this);
    connect(workerThread, &WorkerThread::resultReady, this, &Controller::handleResults);
    connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
    workerThread->start();
}
/*!
 * \brief WorkerThreadPool::findResponsiveThread
 * \return
 */
WorkerThread *WorkerThreadPool::findResponsiveThread()
{
    QMutexLocker l(&threadListMutex);

    if(idleThreads.size()) {
        WorkerThread* wt = idleThreads.takeFirst();
        qDebug()<<"[Main] Using Thread "<<wt->threadId()<<" from idle List";
        return wt;
    } else {
        qDebug()<<"[Main] No Idle Threads trying to find a responsive one";

        int myThread = -1;
        for(int i = 0; i < threads.size(); i++) {
            WorkerRunnable::Load load = threads.at(i)->workLoad();
            if(load == WorkerRunnable::Low) {
                myThread = i;
                break;
            } else if(load == WorkerRunnable::Medium && myThread == -1) {
                myThread = i;
                continue;
            }
        }

        if(myThread >= 0) {
            //move the thread to the back, so it will be picked later the next time
            WorkerThread* t = threads.takeAt(myThread);
            threads.append(t);

            return t;
        }

        //we did not find a thread , enqueue it
    }
    return 0;
}
/** \brief The normal thread run function.
 *
 * The function to be started by normal workers.
 */
static void run_normal()
{
  boost::mutex::scoped_lock scope(mut);
  boost::thread::id tid = boost::this_thread::get_id();
  WorkerThread* thr = (*threads.find(tid)).second.get();

  while(!quitting)
  {
    if(inner_run_important(scope))
    {
      continue;
    }
    if(inner_run_normal(scope))
    {
      continue;
    }
    wake_normal();
    workers_active.remove(thr);
    workers_sleeping.addLast(thr);
    thr->suspend(scope);
  }

  // Remove from active workers before exiting.
  workers_active.remove(thr);
}
Exemple #6
0
int main(int argc, char **args)
{
	WorkerThread wt;
	wt.start();
	wt.wait();
	return 0;
}
Exemple #7
0
unsigned WINAPI WorkerThread::ThreadMain(LPVOID thread){

	WorkerThread* workerThread = (WorkerThread*)thread;
	workerThread->run();
	return 0;

}
/**
 * Called by the monitor thread to harvest idle threads.
 */
void 
ThreadPool::harvestSpareWorkers() {

	PoolObject *po;
	int toFree = 0;

	cond.lock();
	if (stopThePool) {
		cond.unlock();
		return;
	}
	if ((currentThreadCount - currentThreadsBusy) > maxSpareThreads) {
		toFree = currentThreadCount -
			currentThreadsBusy -
			maxSpareThreads;

		if (debug) 
			fprintf(stdout, "ThreadPool.harvestSpareWorkers: Freeing %d threads because they are idle\n",
				toFree);
		for (int i = 0 ; i < toFree ; i++) {
			po = (PoolObject *) idleThreads.pop();
			WorkerThread *c = po->getThread();
			c->terminate();
			currentThreadCount --;
		}
		if (debug)
			fprintf(stdout, "ThreadPool.harvestSpareWorkers: Current Thread Count = %d\n",
				currentThreadCount);
	}
	cond.unlock();
}
QDebug tDebug()
{
    WorkerThread* t = qobject_cast<WorkerThread*>(QThread::currentThread());
    if(t)
        return qDebug()<<"["<<t->threadId()<<"]";

    return qDebug();
}
Exemple #10
0
// Called by a worker thread on return from a job.
void WorkerThread::JobCleanup(void *param)
{
    WorkerThread *worker = (WorkerThread *)param;

    (void) pthread_mutex_lock(&(worker->m_worker_mutex));
    if (worker->m_worker_flags & WORKER_WAIT)
        worker->NotifyWaiters();
}
Exemple #11
0
void Task::startWorkerThread(const QString &id)
{
  L_FUNC(QString("id='%1'").arg(id));

  WorkerThread *workerThread = new WorkerThread(id, this);
  connect(workerThread, SIGNAL(resultReady(const QString&)), this, SLOT(slotResultReady(const QString&)));
  connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
  workerThread->start();
}
Exemple #12
0
void TaskScheduler::wakeUpOneThread()
{
	WorkerThread* thread = mIdleThreads.pop();
	if(thread)
	{
		SCHEDULER_DEBUG("wakeUpOneThread -- thread #" << thread->getThreadId() << " -- #" << thread->getDoWorkEvent()->getHandle());
		thread->getDoWorkEvent()->fire();
	}
}
Exemple #13
0
void *Thread_Func(void *arg)
{
    WorkerThread *worker = new WorkerThread((RequestQueue *)arg);
    signal(SIGTERM, cleanExit);
    signal(SIGINT, cleanExit);
    signal(SIGPIPE, SIG_IGN);
    worker->mainloop();
    
    pthread_exit(NULL);
}
Exemple #14
0
void MainWindow::on_learnDicOkBtn_clicked()
{
	ui->learnDicOkBtn->setEnabled(false);
	ui->saveCfBtn->setEnabled(false);
	WorkerThread *workerThread = new WorkerThread();
	connect(workerThread, SIGNAL(resultReady()), this, SLOT(updateSisrButtons()));
	connect(workerThread, SIGNAL(exceptionOccurs()), this, SLOT(sisrExceptionOccurs()));

	workerThread->start();
}
Exemple #15
0
// MainWrapper
//------------------------------------------------------------------------------
/*static*/ uint32_t WorkerThread::ThreadWrapperFunc( void * param )
{
	WorkerThread * wt = reinterpret_cast< WorkerThread * >( param );
	s_WorkerThreadThreadIndex = wt->m_ThreadIndex;

	CreateThreadLocalTmpDir();

	wt->Main();
	return 0;
}
Exemple #16
0
static void InitiateReconnect()
{
    WorkerThread *thr = WorkerThread::This();
    if (thr)
    {
        // Defer sending the completion of exposure message until after
        // the camera re-connecttion attempt
        thr->SetSkipExposeComplete();
    }
    pFrame->TryReconnect();
}
Exemple #17
0
// worker thread is a thread that wait work in the work queue
WorkerThread * WorkerThread::Create(pthread_attr_t *attr)
{
    WorkerThread *worker;
    if((worker = new (std::nothrow)WorkerThread(attr)) == NULL)
    {
        errno = ENOMEM;
        return (NULL);
    }
    worker->CreateWorker();
    return (worker);
}
Exemple #18
0
	void WorkerThread::ConnEventCB(bufferevent *bev, short int events,
	        void * ctx)
	{
		ConnItem * pitem = static_cast<ConnItem *>(ctx);
		WorkerThread * pwt = static_cast<WorkerThread*>(pitem->pthis);
		if (pitem->log_fd != -1)	//连接意外关闭,关闭写日志文件描述符
			close(pitem->log_fd);
		pwt->DeleteConnItem(*pitem);	//从线程队列中删除连接对象
		bufferevent_free(bev);
		msghandler->sessionClosed(pitem);
	}
Exemple #19
0
bool TaskScheduler::allThreadsIdle()
{
	// ugly, TODO
	uint16 idleCount = 0;
	for(uint16 i = 0; i < mThreadsCount; i++)
	{
		WorkerThread* thread = mAllThreads[i];
		if(thread->isIdle()) idleCount++;
	}

	return idleCount == mThreadsCount;
}
TEST(WorkerThread, FutureVoid) {
  int callbacks = 0;
  WorkerThread<void(int)> worker;
  future<void> f = worker.QueueWork(
      [&](int v) {
        ++callbacks;
        ASSERT_EQ(v, 3);
      },
      3);
  f.get();
  ASSERT_EQ(callbacks, 1);
}
void thr::thr_init()
{
  if(!privileged_thread.isUninitialized())
  {
    std::stringstream sstr;
    sstr << "thread system already initialized";
    BOOST_THROW_EXCEPTION(std::runtime_error(sstr.str()));
  }

  privileged_thread.acquire();
  workers_active.addLast(&privileged_thread);
}
Exemple #22
0
	unsigned int __stdcall ThreadPool::Execute(void* _args)
	{
		// TODO - implement your own version
		//return ExecuteSolution(_args);
		ThreadPool* self = (ThreadPool*)_args;

		bool run = true;					// local run bool for completeing last bit of work
		WorkerThread* workerPtr = nullptr;	//worker thread
		int work = 0;						// amount of work left in the queue

		while( run )
		{
			workerPtr = nullptr;
			self->GetWork( &workerPtr );
			if( workerPtr != nullptr )
			{
				workerPtr->DoWork();
				workerPtr->workDone.Post();
			}
			self->runMutex.Lock();
			run = self->run;
			self->runMutex.Unlock();

			self->queueMutex.Lock();
			work = self->workers.size();
			self->queueMutex.Unlock();

			if( work > 0 )
				run = true;
		}
		
		/*self->runMutex.Lock();
		if(self->run == false)
		{
			
			while( self->workers.size() > 0 )
			{
				WorkerThread* workerPtr = nullptr;
				self->GetWork( &workerPtr );
				if( workerPtr != nullptr )
				{
					workerPtr->DoWork();
					workerPtr->workDone.Post();
				}
				else
					break;
			}
		}
		self->runMutex.Unlock();*/

		return self->shutdownType;
	}
Exemple #23
0
WorkerThread *ThreadPool::AddWorkerThread( bool bMakeAvailable, long nTimeout )
{
    QString sName = QString(m_sName + "_WorkerThread"); 
    long nThreadCount;

    LOG(VB_UPNP, LOG_INFO, QString( "ThreadPool:AddWorkerThread - %1" )
                               .arg(sName));

    WorkerThread *pThread = CreateWorkerThread( this, sName );

    if (pThread != NULL)
    {
        pThread->SetTimeout( nTimeout );
        pThread->start();

        if (pThread->WaitForInitialized( 5000 ))
        {
            // ------------------------------------------------------
            // Add new worker thread to list.
            // ------------------------------------------------------

            m_mList.lock();
            m_lstThreads.push_back( pThread );
            nThreadCount = m_lstThreads.size();

            LOG(VB_GENERAL, LOG_DEBUG,
                QString("ThreadPool:%1: thread pool size %2")
                    .arg(m_sName) .arg(nThreadCount));
                
            if (bMakeAvailable)
            {
                m_lstAvailableThreads.push_back( pThread );
                m_threadAvail.wakeAll();
            }
            m_mList.unlock();

        }
        else
        {
            // ------------------------------------------------------
            // It's taking longer than 5 seconds to initialize this thread.... 
            // give up on it.
            // (This should never happen)
            // ------------------------------------------------------

            delete pThread;
            pThread = NULL;
        }
    }

    return pThread;
}
	static void *_exec(void *instance) {

		WorkerThread *pThis = reinterpret_cast<WorkerThread *>(instance);
		bool exitRet=true;
		while( (exitRet = pThis->run()) && pThis->isRunning() ) {
			;
		}

		void *ret;
		ret = (void *)(exitRet?0:-1);
		pthread_exit(ret);

	}
/* See description in header file. */
void*  WorkerThread::StaticThreadFunction(void* arg)
{
    WorkerThread* pThis = NULL;
    if (arg)
    {
        pThis = static_cast<WorkerThread*>(arg);
        if (pThis)
        {
            pThis->m_tid = pthread_self();
            pThis->DoRealWorks();
        }
    }
}
/** \brief Inner common implementation of dispatch_privileged.
 *
 * \param pfunctor Task to execute.
 * \return Task id created.
 */
static void inner_dispatch_privileged(const Task &pfunctor)
{
  tasks_privileged.put(pfunctor);

  if(privileged_thread.isSleeping())
  {
    ThreadStorage *storage = privileged_thread.getStorage();

    storage->remove(&privileged_thread);
    workers_active.addLast(&privileged_thread);
    privileged_thread.notify(true);
  }
}
Exemple #27
0
int WorkQueue::Initialize() {
    // init thread index for main thread
    tls = new ThreadLocal();
    ThreadLocal::SetThreadLocal(0);
    // start threads
    int cores = 4;
    for (int i = 0; i < cores; i++) {
        WorkerThread * Thread = new WorkerThread(this, i + 1);
        Workers.PushBack(Thread);
        Thread->Start();
    }
    return 0;
}
Exemple #28
0
void WorkerThread::WorkerCleanup(void *param)
{
    WorkerThread *worker = (WorkerThread *)param;
    if (worker->m_worker_flags & WORKER_DESTROY) 
    {
        pthread_cond_broadcast(&worker->m_worker_workcv);
    } 
    else
    {
        worker->CreateWorker();
    }
    pthread_mutex_unlock(&worker->m_worker_mutex);
}
Exemple #29
0
void DynamicTask::waitUntilComplete()
{
  WorkerThread* self = WorkerThread::self();
  JASSERT(self!=NULL);
  _lock.lock();
  while(_state != S_COMPLETE && _state!= S_CONTINUED) {
    _lock.unlock();
    self->popAndRunOneTask(STEAL_ATTEMPTS_WAITING);
    _lock.lock();
  }
  _lock.unlock();
  if(_state == S_CONTINUED)
    _continuation->waitUntilComplete();
}
Exemple #30
0
sint32 WorkerThreadPool::init(sint32 number, timeval &tv)
{
    ret_val_if_fail(number > 0, -1);

    for (sint32 i = 0; i < number; ++i)
    {
        WorkerThread* wt = new WorkerThread(this, i, tv);
        if (-1 == wt->init())
        {
            LOGGER_ERROR(g_framework_logger, "init worker thread failed");
            return -1;
        }
        m_worker_threads.push_back(wt);
    }

    for (sint32 i = 0; i < number; ++i)
    {
        if (-1 == m_worker_threads[i]->start_thread())
        {
            LOGGER_ERROR(g_framework_logger, "start worker thread failed");
            return -1;
        }
    }

    // wait the worker_thread all start work
    pthread_mutex_lock(&m_init_lock);
    while (m_num_active_thread < number)
    {
        pthread_cond_wait(&m_init_cond, &m_init_lock);
    }
    pthread_mutex_unlock(&m_init_lock);

    for (sint32 i = 0; i < number; i++)
    {
        pthread_t id = m_worker_threads[i]->get_thread_id();
        m_thread_ids.push_back(id);
        std::pair<map<pthread_t, sint32>::iterator, bool> res = m_threadid_index_map.insert(std::make_pair<pthread_t, sint32>(id, i));
        if (false == res.second)
        {
            LOGGER_ERROR(g_framework_logger, "insert <threadid, index> fail!");
            return -1;
        }
    }

    LOGGER_INFO(g_framework_logger, "need worker thread number=" << number
            << ", active thread number=" << m_num_active_thread);

    return 0;
}