1 #include "smud_threads.h"
3 ThreadPool::ThreadPool()
7 ThreadPool::~ThreadPool()
11 bool ThreadPool::Start()
13 m_pWorker = new ThreadWorker();
15 if (!m_pWorker->Start())
24 void ThreadPool::Stop()
26 m_pWorker->CancelAndWait();
30 void ThreadPool::AddConnection(int fd)
32 m_pWorker->AddConnection(fd);
35 ThreadWorker::ThreadWorker() : m_bShouldCancel(false)
39 ThreadWorker::~ThreadWorker()
43 bool ThreadWorker::Start()
45 m_pPool = new ConnectionPool();
46 pthread_mutex_init(&m_NotifyLock, NULL);
47 pthread_cond_init(&m_Notify, NULL);
49 if (pthread_create(&m_Thread, NULL, ThreadCallback, this) != 0)
57 void ThreadWorker::CancelAndWait()
59 m_bShouldCancel = true;
61 pthread_join(m_Thread, NULL);
63 pthread_cond_destroy(&m_Notify);
64 pthread_mutex_destroy(&m_NotifyLock);
69 void ThreadWorker::AddConnection( int fd )
71 m_pPool->AddConnection(fd);
74 void ThreadWorker::Process()
76 m_pPool->Process(&m_bShouldCancel);
79 void *ThreadCallback(void *data)
81 ((ThreadWorker *)data)->Process();