CSCommon: MAsyncProxy.h 소스 파일

MAIET

MAsyncProxy.h

00001 #pragma once
00002 
00004 // Purpose      : Blocking 작업을 여러 쓰레드로 나눠 Async 스럽게 굴린다.
00005 // Last Update  : 2004-02-04 
00006 
00007 #pragma warning(disable:4786)
00008 #include <list>
00009 #include <algorithm>
00010 using namespace std;
00011 
00012 
00013 enum MASYNC_RESULT {
00014     MASYNC_RESULT_SUCCEED,
00015     MASYNC_RESULT_FAILED,
00016     MASYNC_RESULT_TIMEOUT
00017 };
00018 
00019 
00020 class MAsyncJob {
00021 protected:
00022     int             m_nJobID;   // Job Type ID
00023 
00024     unsigned long   m_nPostTime;
00025     unsigned long   m_nFinishTime;
00026 
00027     MASYNC_RESULT   m_nResult;
00028 
00029 public:
00030     MAsyncJob(int nJobID) {
00031         m_nJobID = nJobID;
00032         m_nPostTime = 0;
00033         m_nFinishTime = 0;
00034     }
00035     virtual ~MAsyncJob()    {}
00036 
00037     int GetJobID()                          { return m_nJobID; }
00038     unsigned long GetPostTime()             { return m_nPostTime; }
00039     void SetPostTime(unsigned long nTime)   { m_nPostTime = nTime; }
00040     unsigned long GetFinishTime()           { return m_nFinishTime; }
00041     void SetFinishTime(unsigned long nTime) { m_nFinishTime = nTime; }
00042 
00043     MASYNC_RESULT GetResult()               { return m_nResult; }
00044     void SetResult(MASYNC_RESULT nResult)   { m_nResult = nResult; }
00045 
00046     virtual void Run(void* pContext) = 0;
00047 };
00048 
00049 class MAsyncJobList : protected list<MAsyncJob*> {
00050 protected:
00051     CRITICAL_SECTION    m_csLock;
00052 public:
00053     MAsyncJobList()             { InitializeCriticalSection(&m_csLock); }
00054     virtual ~MAsyncJobList()    { DeleteCriticalSection(&m_csLock); }
00055 
00056     void Lock()     { EnterCriticalSection(&m_csLock); }
00057     void Unlock()   { LeaveCriticalSection(&m_csLock); }
00058 
00059     // Unsafe Methods /////////////////////////////////////////////
00060     // ~Unsafe() 용도 이외엔 사용금지
00061     MAsyncJobList::iterator GetBeginItorUnsafe()    { return begin(); }
00062     MAsyncJobList::iterator GetEndItorUnsafe()  { return end(); }
00063 
00064     void AddUnsafe(MAsyncJob* pJob) {
00065         push_back(pJob);
00066     }
00067     void RemoveUnsafe(MAsyncJob* pJob, MAsyncJobList::iterator* itorOut) {
00068         iterator i = find(begin(), end(), pJob);
00069         if (i != end()) {
00070             iterator itorTmp = erase(i);
00071             if (itorOut)
00072                 *itorOut = itorTmp;
00073         }
00074     }
00075     MAsyncJob* GetJobUnsafe() {
00076         if (begin() == end()) return NULL;
00077         MAsyncJob* pReturn = *begin();
00078         pop_front();
00079         return pReturn;
00080     }
00081     int GetCount() { return (int)size(); }
00082 };
00083 
00084 #define MAX_THREADPOOL_COUNT 10
00085 
00086 class MAsyncProxy {
00087 protected:
00088     HANDLE          m_hEventShutdown;
00089     HANDLE          m_hEventFetchJob;
00090 
00091     int             m_nThreadCount;
00092     HANDLE          m_ThreadPool[MAX_THREADPOOL_COUNT];
00093 
00094     MAsyncJobList   m_WaitQueue;
00095     MAsyncJobList   m_ResultQueue;
00096     
00097 protected:
00098     HANDLE GetEventShutdown()   { return m_hEventShutdown; }
00099     HANDLE GetEventFetchJob()   { return m_hEventFetchJob; }
00100 
00101     static DWORD WINAPI WorkerThread(LPVOID pJobContext);
00102 
00103 public:
00104     MAsyncProxy();
00105     virtual ~MAsyncProxy();
00106     bool Create(int nThreadCount);
00107     void Destroy();
00108     
00109     int GetWaitQueueCount()     { return m_WaitQueue.GetCount(); }
00110     int GetResultQueueCount()   { return m_ResultQueue.GetCount(); }
00111 
00112     void PostJob(MAsyncJob* pJob);
00113     MAsyncJob* GetJobResult()   {
00114         MAsyncJob* pJob = NULL;
00115         m_ResultQueue.Lock();
00116             pJob = m_ResultQueue.GetJobUnsafe();
00117         m_ResultQueue.Unlock();
00118         return pJob;
00119     }
00120 };


MAIET entertainment