Beispiel #1
0
void ChannelService::OnSoftSignal(uint32 soft_signo, uint32 appendinfo)
{
    switch (soft_signo)
    {
        case CHANNEL_REMOVE:
        {
            //uint32 chanel_id = appendinfo;
            VerifyRemoveQueue();
            break;
        }
        case WAKEUP:
        {
            Runnable* task = NULL;
            while (m_pending_tasks.Pop(task))
            {
                if (NULL != task)
                {
                    task->Run();
                }
            }
            break;
        }
        case USER_DEFINED:
        {
            if (NULL != m_user_cb)
            {
                m_user_cb(this, appendinfo, m_user_cb_data);
            }
            break;
        }
        case CHANNEL_ASNC_IO:
        {
            ChannelAsyncIOContext ctx;
            while (m_async_io_queue.Pop(ctx))
            {
                if (NULL != ctx.cb)
                {
                    Channel* ch = GetChannel(ctx.channel_id);
                    ctx.cb(ch, ctx.data);
                }
            }
            break;
        }
        default:
        {
            break;
        }
    }
}
/**
执行任务的工作线程。
当前没有任务时,
如果当前线程数量大于最小线程数量,减少线程,
否则,执行清理程序,将线程类给释放掉
**/
void CThreadPoolExecutor::CWorker::Run()
{
	Runnable * pTask = NULL;
	while (m_bRun)
	{
		if (NULL == m_pFirstTask)
		{
			pTask = m_pThreadPool->GetTask();
		}
		else
		{
			pTask = m_pFirstTask;
			m_pFirstTask = NULL;
		}

		if (NULL == pTask)
		{
			EnterCriticalSection(&(m_pThreadPool->m_csThreadPoolLock));
			if (m_pThreadPool->GetThreadPoolSize() > m_pThreadPool->m_minThreads)
			{
				ThreadPoolItr itr = m_pThreadPool->m_ThreadPool.find(this);
				if (itr != m_pThreadPool->m_ThreadPool.end())
				{
					m_pThreadPool->m_ThreadPool.erase(itr);
					m_pThreadPool->m_TrashThread.insert(this);
				}
				m_bRun = false;
			}
			else
			{
				ThreadPoolItr itr = m_pThreadPool->m_TrashThread.begin();
				while (itr != m_pThreadPool->m_TrashThread.end())
				{
					(*itr)->Join();
					delete (*itr);
					m_pThreadPool->m_TrashThread.erase(itr);
					itr = m_pThreadPool->m_TrashThread.begin();
				}
			}
			LeaveCriticalSection(&(m_pThreadPool->m_csThreadPoolLock));
			continue;
		}
		else
		{
			pTask->Run();
			pTask = NULL;
		}
	}
}
            LRESULT OnTaskHandleMsg(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
            {
                Runnable* pRunnable = (Runnable *)wParam;

                if (pRunnable)
                {
                    pRunnable->Run();

                    if (pRunnable->IsBeHosted())
                    {
                        pRunnable->Release();
                    }
                }

                return 0;
            }
Beispiel #4
0
void LinkageWorker::OnCommand()
{
    std::list<Runnable *> q;
    MutexLocker locker(_mutex);
    q.swap(_commands);
    locker.Unlock();

    for (std::list<Runnable *>::iterator p = q.begin(); p != q.end(); ++p) {
        Runnable *command = *p;
        if (!command) {
            ev_break(_loop, EVBREAK_ALL);
            _quit = true;
            return;
        }

        if (!command->Run()) {
            break;
        }
    }

    for (std::list<Runnable *>::iterator p = q.begin(); p != q.end(); ++p) {
        delete *p;
    }
}
Beispiel #5
0
void ThreadPoolImpl::Worker::Run()
{
	Runnable *pTask = 0;

	while (m_bRun.Read())
	{
		if (0 == m_pRunnable) {
			TAutoLock lock(m_pPoolImpl->m_mtxTasks);
			if (!m_pPoolImpl->m_tasks.empty()) {
				pTask = m_pPoolImpl->m_tasks.front();
				m_pPoolImpl->m_tasks.pop_front();
			}
		} else {
			pTask = m_pRunnable;
			m_pRunnable = 0;
		}

		if (0 == pTask)
		{
			if (m_pPoolImpl->m_threads.size() > m_pPoolImpl->m_nMinThread)
			{
				this->Stop();

				m_pPoolImpl->m_mtxThread.Lock();

				ThreadIterator itr = m_pPoolImpl->m_threads.begin();
				while (itr != m_pPoolImpl->m_threads.end()) {
					if (this == (*itr)->GetRunnable()) {
						break;
					}
					++itr;
				}
				if (itr != m_pPoolImpl->m_threads.end()) {
					Thread *pThread = (*itr);
					m_pPoolImpl->m_threads.erase(itr);
					TAutoLock lock(m_pPoolImpl->m_mtxThreadDead);
					m_pPoolImpl->m_threadDead.push_back(pThread);
				}

				m_pPoolImpl->m_mtxThread.Unlock();
			}
			else
			{
				m_pPoolImpl->m_mtxThreadDead.Lock();
				if (!m_pPoolImpl->m_threadDead.empty())
				{
					ThreadIterator itr = m_pPoolImpl->m_threadDead.begin();
					while (itr != m_pPoolImpl->m_threadDead.end())
					{
						(*itr)->Join();
						delete (*itr);
						(*itr) = 0;
						m_pPoolImpl->m_threadDead.erase(itr);
						itr = m_pPoolImpl->m_threadDead.begin();
					}
					m_pPoolImpl->m_mtxThreadDead.Unlock();
				}
				else 
				{
					m_pPoolImpl->m_mtxThreadDead.Unlock();
					Thread::Sleep(50); // to be continue...
				}
			}
		}
		else
		{
			pTask->Run();
			delete pTask;
			pTask = 0;
		}

	} //~while
}
Beispiel #6
0
static void* runnable_thread_body(void* arg) {
  Runnable* r = static_cast<Runnable*>(arg);
  r->Run();
  return NULL;
}