tools/daemon/smud_threads.cpp
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 19:22:36 2009 -0500 (2009-02-25)
changeset 2603 fa286305bd3f
parent 2129 11d73bf97119
permissions -rw-r--r--
Allow for second private chat trigger (bug 3667, r,a11=pred).
     1 #include "smud_threads.h"
     2 
     3 ThreadPool::ThreadPool() 
     4 {
     5 }
     6 
     7 ThreadPool::~ThreadPool()
     8 {
     9 }
    10 
    11 bool ThreadPool::Start()
    12 {
    13 	m_pWorker = new ThreadWorker();
    14 	
    15 	if (!m_pWorker->Start())
    16 	{
    17 		delete m_pWorker;
    18 		return false;
    19 	}
    20 
    21 	return true;
    22 }
    23 
    24 void ThreadPool::Stop()
    25 {
    26 	m_pWorker->CancelAndWait();
    27 	delete m_pWorker;
    28 }
    29 
    30 void ThreadPool::AddConnection(int fd)
    31 {
    32 	m_pWorker->AddConnection(fd);
    33 }
    34 
    35 ThreadWorker::ThreadWorker() : m_bShouldCancel(false)
    36 {
    37 }
    38 
    39 ThreadWorker::~ThreadWorker()
    40 {
    41 }
    42 
    43 bool ThreadWorker::Start()
    44 {
    45 	m_pPool = new ConnectionPool();
    46 	pthread_mutex_init(&m_NotifyLock, NULL);
    47 	pthread_cond_init(&m_Notify, NULL);
    48 
    49 	if (pthread_create(&m_Thread, NULL, ThreadCallback, this) != 0)
    50 	{
    51 		return false;
    52 	}
    53 
    54 	return true;
    55 }
    56 
    57 void ThreadWorker::CancelAndWait()
    58 {
    59 	m_bShouldCancel = true;
    60 
    61 	pthread_join(m_Thread, NULL);
    62 
    63 	pthread_cond_destroy(&m_Notify);
    64 	pthread_mutex_destroy(&m_NotifyLock);
    65 
    66 	delete m_pPool;
    67 }
    68 
    69 void ThreadWorker::AddConnection( int fd )
    70 {
    71 	m_pPool->AddConnection(fd);
    72 }
    73 
    74 void ThreadWorker::Process()
    75 {
    76 	m_pPool->Process(&m_bShouldCancel);
    77 }
    78 
    79 void *ThreadCallback(void *data)
    80 {
    81 	((ThreadWorker *)data)->Process();
    82 
    83 	pthread_exit(NULL);
    84 }
    85 
    86