Mixe for Privacy and Anonymity in the Internet
CAThreadPool.hpp
Go to the documentation of this file.
00001 /********************************************************
00002  * A thread pool class inspired by:
00003  * "Using POSIX Threads: Programming with Pthreads"
00004  *     by Brad nichols, Dick Buttlar, Jackie Farrell
00005  *     O'Reilly & Associates, Inc.
00006  *
00007  ********************************************************/
00008 
00009 #ifndef __CATHREADPOOL__
00010 #define __CATHREADPOOL__
00011 
00012 #include "CAMutex.hpp"
00013 #ifndef ONLY_LOCAL_PROXY
00014 #include "CAThread.hpp"
00015 #include "CAConditionVariable.hpp"
00016 
00017 struct tpool_work 
00018   {
00019     THREAD_MAIN_TYP     routine;
00020     void                *arg;
00021     struct tpool_work   *next;
00022   };
00023 
00024 typedef struct tpool_work tpool_work_t;
00025 
00026 THREAD_RETURN worker_thread_main_loop(void *args);
00027 
00031 class CAThreadPool 
00032   {
00033     public:
00034       CAThreadPool( UINT32 num_worker_threads, 
00035                                   UINT32 max_queue_size,
00036                                   bool   b_do_not_block_when_full);
00037       ~CAThreadPool()
00038         {
00039           destroy(true);
00040         }
00044       SINT32 destroy(bool bWaitForFinish);
00045       SINT32 addRequest(THREAD_MAIN_TYP, void *args);
00046       UINT32 countRequests()
00047       {
00048         return m_CurQueueSize;
00049       }
00050       friend THREAD_RETURN worker_thread_main_loop(void *args);
00051     private:
00052       /* pool characteristics */
00053       UINT32        m_NumThreads;
00054       UINT32        m_MaxQueueSize;
00055       bool          m_bDoNotBlockWhenFull;
00056       /* pool state */
00057       CAThread**      m_parThreads;
00058       volatile UINT32 m_CurQueueSize;
00059       tpool_work_t*   m_pQueueHead;
00060       tpool_work_t*   m_pQueueTail;
00061       volatile bool   m_bQueueClosed;
00062       volatile bool   m_bShutdown;
00063       /* pool synchronization */
00064       CAMutex*              m_pmutexQueue;
00065       CAConditionVariable*  m_pcondNotEmpty;
00066       CAConditionVariable*  m_pcondNotFull;
00067       CAConditionVariable*  m_pcondEmpty;
00068   };
00069 #endif //ONLY_LOCAL_PROXY
00070 
00071 #endif