EventPool.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "stdafx.h"
  2. #include "EventPool.h"
  3. #include "msgdef.h"
  4. #include "EventQueue.h"
  5. #include "EventThread.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. CEventPool::CEventPool()
  12. {
  13. m_pThread = new CEventThread;
  14. m_pThread->SetSemaphoreNum(100000);
  15. m_pThread->Start();
  16. m_pEventQueue = new CEventQueue;
  17. }
  18. CEventPool::~CEventPool()
  19. {
  20. SafeRelease(m_pThread);
  21. ClearAllEvent();
  22. }
  23. /************************************************************************/
  24. /*
  25. 函数:AddEvent
  26. 描述:添加任务事件
  27. 参数:
  28. IN LPSEVENT lpEvent 任务事件
  29. 返回:1成功
  30. */
  31. /************************************************************************/
  32. int CEventPool::AddEvent(IN LPSEVENT lpEvent)
  33. {
  34. m_pEventQueue->Push_Back(lpEvent);
  35. m_lock.lock();
  36. ++m_dwEventSize;
  37. m_lock.unlock();
  38. return 1;
  39. }
  40. /************************************************************************/
  41. /*
  42. 函数:ClearAllEvent
  43. 描述:清除所有事件
  44. 参数:
  45. 返回:
  46. */
  47. /************************************************************************/
  48. void CEventPool::ClearAllEvent()
  49. {
  50. if(m_pEventQueue)
  51. m_pEventQueue->Clear();
  52. SafeRelease(m_pEventQueue);
  53. }
  54. //
  55. /************************************************************************/
  56. /*
  57. 函数:TakeOutEvent
  58. 描述:从任务队列取出任务
  59. 参数:
  60. 返回:成功返回任务,失败为NULL
  61. */
  62. /************************************************************************/
  63. LPSEVENT CEventPool::TakeOutEvent()
  64. {
  65. m_lock.lock();
  66. DWORD dwSize = m_dwEventSize;
  67. m_lock.unlock();
  68. if(dwSize == 0)
  69. return NULL;
  70. LPSEVENT lp = m_pEventQueue->Pop_Front();
  71. if(lp == NULL)
  72. return NULL;
  73. m_lock.lock();
  74. --m_dwEventSize;
  75. m_lock.unlock();
  76. return lp;
  77. }
  78. /************************************************************************/
  79. /*
  80. 函数:GetEventNum
  81. 描述:获取事件数量
  82. 参数:
  83. 返回:任务数量
  84. */
  85. /************************************************************************/
  86. int CEventPool::GetEventNum()
  87. {
  88. m_lock.lock();
  89. DWORD dwSize = m_dwEventSize;
  90. m_lock.unlock();
  91. return dwSize;
  92. }
  93. /************************************************************************/
  94. /*
  95. 函数:DeleteEvent
  96. 描述:删除事件
  97. 参数:
  98. 返回:任务数量
  99. */
  100. /************************************************************************/
  101. void CEventPool::DeleteEvent()
  102. {
  103. m_pEventQueue->Pop_Front();
  104. }
  105. /************************************************************************/
  106. /*
  107. 函数:Terminate
  108. 描述:终止线程
  109. 参数:
  110. 返回:
  111. */
  112. /************************************************************************/
  113. void CEventPool::Terminate()
  114. {
  115. m_pThread->Exit();
  116. }