Exemple #1
0
void WorkerThread::Run()
{
	BOOL ret = FALSE;
	DWORD bytesTransferred = 0;
	AsyncCompleteOperation* pCompleteOperation = NULL;
	void* pCompletionKey = NULL;

	while( Runnable() )
	{
		ret = GetQueuedCompletionStatus(
			s_Dispatcher.GetIOCPHandle(),
			&bytesTransferred,
			(PULONG_PTR)&pCompletionKey,
			(LPOVERLAPPED*)&pCompleteOperation,
			INFINITE
			);

		if ( FALSE == ret )
		{
			LOG_ERROR( logger, __FUNCTION__ << " failed GetQueuedCompletionStatus(), " << FormatErrorMessage( WSAGetLastError() ) );

			if ( NULL != pCompletionKey )
			{
				pCompleteOperation->Close( static_cast<Session*>( pCompletionKey ) );
			}
			continue;
		}
		
 		if ( 0 == bytesTransferred )
 		{
			//if NULL == pCompletionKey and NULL == pCompleteOperation,
			//	it means worker thread is being stopped.
			if ( !Runnable() && NULL == pCompletionKey && NULL == pCompleteOperation )
			{
				break;				
			}

			//if NULL != pCompletionKey, the pCompletionKey points to a Session
			//if NULL == pCompletionKey, the asynchronous operation is an accept complete operation, 
			//	we should use Complete operation to accept this new session, not Close
			if ( NULL != pCompletionKey )
			{
				pCompleteOperation->Close( static_cast<Session*>( pCompletionKey ) );
				continue;
			}
 		}

		pCompleteOperation->Complete( static_cast<Session*>( pCompletionKey ) );
	}

	LOG_INFO( logger, "exit thread" );
}
Exemple #2
0
void
JobQueue::Worker::Run()
{
    while ( Suspended() )
        WaitForResume();

    while ( Runnable() )
    {
        while ( Suspended() )
            WaitForResume();

        if ( m_pJob )
        {
            Job::Data* pJobData = m_pJob->GetData();
            Job::Processor pfnJobProc = m_pJob->GetProcessor();
            pfnJobProc( pJobData );

            pJobData->SetDoneFlagOn();
            m_pJob->~Job();
            cntlFreeSystem( m_pJob );
            m_pJob = NULL;
        }
        else
        {
            if ( m_pJobQueue->Running() )
            {
                m_pJob = m_pJobQueue->PopJob();
            }
            else
            {
                cntl::Thread::Sleep( 1 );
            }
        }
    }

    if ( m_pJob )
    {
        Job::Data* pJobData = m_pJob->GetData();
        pJobData->SetDoneFlagOff();
        m_pJob->~Job();
        cntlFreeSystem( m_pJob );
        m_pJob = NULL;
    }
}
Exemple #3
0
	virtual bool CanRun(uint32_t now) { return Runnable(now); }