sourcemod/tools/daemon/smud_threads.cpp
Matt Woodrow 57bba6d83f Import of gamedata daemon. I love imported stuff.
--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%402304
2008-06-30 02:34:21 +00:00

87 lines
1.1 KiB
C++

#include "smud_threads.h"
ThreadPool::ThreadPool()
{
}
ThreadPool::~ThreadPool()
{
}
bool ThreadPool::Start()
{
m_pWorker = new ThreadWorker();
if (!m_pWorker->Start())
{
delete m_pWorker;
return false;
}
return true;
}
void ThreadPool::Stop()
{
m_pWorker->CancelAndWait();
delete m_pWorker;
}
void ThreadPool::AddConnection(int fd)
{
m_pWorker->AddConnection(fd);
}
ThreadWorker::ThreadWorker() : m_bShouldCancel(false)
{
}
ThreadWorker::~ThreadWorker()
{
}
bool ThreadWorker::Start()
{
m_pPool = new ConnectionPool();
pthread_mutex_init(&m_NotifyLock, NULL);
pthread_cond_init(&m_Notify, NULL);
if (pthread_create(&m_Thread, NULL, ThreadCallback, this) != 0)
{
return false;
}
return true;
}
void ThreadWorker::CancelAndWait()
{
m_bShouldCancel = true;
pthread_join(m_Thread, NULL);
pthread_cond_destroy(&m_Notify);
pthread_mutex_destroy(&m_NotifyLock);
delete m_pPool;
}
void ThreadWorker::AddConnection( int fd )
{
m_pPool->AddConnection(fd);
}
void ThreadWorker::Process()
{
m_pPool->Process(&m_bShouldCancel);
}
void *ThreadCallback(void *data)
{
((ThreadWorker *)data)->Process();
pthread_exit(NULL);
}