tools/daemon/smud_threads.cpp
changeset 2129 11d73bf97119
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/daemon/smud_threads.cpp	Mon Jun 30 02:34:21 2008 +0000
     1.3 @@ -0,0 +1,86 @@
     1.4 +#include "smud_threads.h"
     1.5 +
     1.6 +ThreadPool::ThreadPool() 
     1.7 +{
     1.8 +}
     1.9 +
    1.10 +ThreadPool::~ThreadPool()
    1.11 +{
    1.12 +}
    1.13 +
    1.14 +bool ThreadPool::Start()
    1.15 +{
    1.16 +	m_pWorker = new ThreadWorker();
    1.17 +	
    1.18 +	if (!m_pWorker->Start())
    1.19 +	{
    1.20 +		delete m_pWorker;
    1.21 +		return false;
    1.22 +	}
    1.23 +
    1.24 +	return true;
    1.25 +}
    1.26 +
    1.27 +void ThreadPool::Stop()
    1.28 +{
    1.29 +	m_pWorker->CancelAndWait();
    1.30 +	delete m_pWorker;
    1.31 +}
    1.32 +
    1.33 +void ThreadPool::AddConnection(int fd)
    1.34 +{
    1.35 +	m_pWorker->AddConnection(fd);
    1.36 +}
    1.37 +
    1.38 +ThreadWorker::ThreadWorker() : m_bShouldCancel(false)
    1.39 +{
    1.40 +}
    1.41 +
    1.42 +ThreadWorker::~ThreadWorker()
    1.43 +{
    1.44 +}
    1.45 +
    1.46 +bool ThreadWorker::Start()
    1.47 +{
    1.48 +	m_pPool = new ConnectionPool();
    1.49 +	pthread_mutex_init(&m_NotifyLock, NULL);
    1.50 +	pthread_cond_init(&m_Notify, NULL);
    1.51 +
    1.52 +	if (pthread_create(&m_Thread, NULL, ThreadCallback, this) != 0)
    1.53 +	{
    1.54 +		return false;
    1.55 +	}
    1.56 +
    1.57 +	return true;
    1.58 +}
    1.59 +
    1.60 +void ThreadWorker::CancelAndWait()
    1.61 +{
    1.62 +	m_bShouldCancel = true;
    1.63 +
    1.64 +	pthread_join(m_Thread, NULL);
    1.65 +
    1.66 +	pthread_cond_destroy(&m_Notify);
    1.67 +	pthread_mutex_destroy(&m_NotifyLock);
    1.68 +
    1.69 +	delete m_pPool;
    1.70 +}
    1.71 +
    1.72 +void ThreadWorker::AddConnection( int fd )
    1.73 +{
    1.74 +	m_pPool->AddConnection(fd);
    1.75 +}
    1.76 +
    1.77 +void ThreadWorker::Process()
    1.78 +{
    1.79 +	m_pPool->Process(&m_bShouldCancel);
    1.80 +}
    1.81 +
    1.82 +void *ThreadCallback(void *data)
    1.83 +{
    1.84 +	((ThreadWorker *)data)->Process();
    1.85 +
    1.86 +	pthread_exit(NULL);
    1.87 +}
    1.88 +
    1.89 +