HPThreadPool.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "hpsocket/SocketInterface.h"
  25. #include "common/STLHelper.h"
  26. #include "common/Semaphore.h"
  27. #include "common/RingBuffer.h"
  28. #include "InternalDef.h"
  29. LPTSocketTask CreateSocketTaskObj( Fn_SocketTaskProc fnTaskProc,
  30. PVOID pSender, CONNID dwConnID,
  31. LPCBYTE pBuffer, INT iBuffLen, EnTaskBufferType enBuffType = TBT_COPY,
  32. WPARAM wParam = 0, LPARAM lParam = 0);
  33. void DestroySocketTaskObj(LPTSocketTask pTask);
  34. class CHPThreadPool : public IHPThreadPool
  35. {
  36. private:
  37. enum EnSubmitResult{SUBMIT_OK, SUBMIT_FULL, SUBMIT_ERROR};
  38. struct TTask
  39. {
  40. Fn_TaskProc fn;
  41. PVOID arg;
  42. BOOL freeArg;
  43. public:
  44. static TTask* Construct(Fn_TaskProc fnTaskProc, PVOID pvArg, BOOL bFreeArg)
  45. {
  46. return new TTask(fnTaskProc, pvArg, bFreeArg);
  47. }
  48. static void Destruct(TTask* pTask)
  49. {
  50. if(pTask)
  51. {
  52. delete pTask;
  53. }
  54. }
  55. private:
  56. TTask(Fn_TaskProc fnTaskProc, PVOID pvArg, BOOL bFreeArg)
  57. : fn(fnTaskProc), arg(pvArg), freeArg(bFreeArg)
  58. {
  59. ASSERT(fn != nullptr);
  60. }
  61. };
  62. using CTaskQueue = CCASQueue<TTask>;
  63. public:
  64. virtual BOOL Start(DWORD dwThreadCount = 0, DWORD dwMaxQueueSize = 0, EnRejectedPolicy enRejectedPolicy = TRP_CALL_FAIL, DWORD dwStackSize = 0);
  65. virtual BOOL Stop(DWORD dwMaxWait = INFINITE);
  66. virtual BOOL Wait(DWORD dwMilliseconds = INFINITE) {return m_evWait.WaitFor(dwMilliseconds, WAIT_FOR_STOP_PREDICATE);}
  67. virtual BOOL Submit(Fn_TaskProc fnTaskProc, PVOID pvArg, DWORD dwMaxWait = INFINITE);
  68. virtual BOOL Submit(LPTSocketTask pTask, DWORD dwMaxWait = INFINITE);
  69. virtual BOOL AdjustThreadCount(DWORD dwNewThreadCount);
  70. public:
  71. virtual BOOL HasStarted() {return m_enState == SS_STARTED || m_enState == SS_STARTING;}
  72. virtual EnServiceState GetState() {return m_enState;}
  73. virtual DWORD GetQueueSize() {return m_lsTasks.Size();}
  74. virtual DWORD GetTaskCount() {return m_dwTaskCount;}
  75. virtual DWORD GetThreadCount() {return m_dwThreadCount;}
  76. virtual DWORD GetMaxQueueSize() {return m_dwMaxQueueSize;}
  77. virtual EnRejectedPolicy GetRejectedPolicy() {return m_enRejectedPolicy;}
  78. private:
  79. BOOL CheckStarting();
  80. BOOL CheckStarted();
  81. BOOL CheckStoping();
  82. BOOL InternalAdjustThreadCount(DWORD dwNewThreadCount);
  83. BOOL DoAdjustThreadCount(DWORD dwNewThreadCount);
  84. BOOL CreateWorkerThreads(DWORD dwThreadCount);
  85. BOOL Shutdown(DWORD dwMaxWait);
  86. BOOL CheckWorkerThreadExit();
  87. static PVOID ThreadProc(LPVOID pv);
  88. int WorkerProc();
  89. EnSubmitResult DirectSubmit(Fn_TaskProc fnTaskProc, PVOID pvArg, BOOL bFreeArg);
  90. BOOL CycleWaitSubmit(Fn_TaskProc fnTaskProc, PVOID pvArg, DWORD dwMaxWait, BOOL bFreeArg);
  91. BOOL DoSubmit(Fn_TaskProc fnTaskProc, PVOID pvArg, BOOL bFreeArg, DWORD dwMaxWait);
  92. void DoRunTaskProc(Fn_TaskProc fnTaskProc, PVOID pvArg, BOOL bFreeArg);
  93. void FireStartup()
  94. {if(m_pListener != nullptr) m_pListener->OnStartup(this);}
  95. void FireShutdown()
  96. {if(m_pListener != nullptr) m_pListener->OnShutdown(this);}
  97. void FireWorkerThreadStart()
  98. {if(m_pListener != nullptr) m_pListener->OnWorkerThreadStart(this, SELF_THREAD_ID);}
  99. void FireWorkerThreadEnd()
  100. {if(m_pListener != nullptr) m_pListener->OnWorkerThreadEnd(this, SELF_THREAD_ID);}
  101. public:
  102. CHPThreadPool(IHPThreadPoolListener* pListener = nullptr)
  103. : m_pListener(pListener)
  104. {
  105. MakePrefix();
  106. Reset(FALSE);
  107. }
  108. virtual ~CHPThreadPool()
  109. {
  110. ENSURE_STOP();
  111. }
  112. private:
  113. void Reset(BOOL bSetWaitEvent = TRUE);
  114. void MakePrefix();
  115. private:
  116. static LPCTSTR POOLED_THREAD_PREFIX;
  117. static volatile UINT sm_uiNum;
  118. volatile UINT m_uiSeq;
  119. CString m_strPrefix;
  120. private:
  121. IHPThreadPoolListener* m_pListener;
  122. DWORD m_dwStackSize;
  123. DWORD m_dwMaxQueueSize;
  124. EnRejectedPolicy m_enRejectedPolicy;
  125. volatile DWORD m_dwTaskCount;
  126. volatile DWORD m_dwThreadCount;
  127. volatile EnServiceState m_enState;
  128. CSEM m_evWait;
  129. CSEM m_evShutdown;
  130. CSEM m_evTask;
  131. CSEM m_evQueue;
  132. CCriSec m_csThread;
  133. CTaskQueue m_lsTasks;
  134. unordered_set<THR_ID> m_stThreads;
  135. DECLARE_NO_COPY_CLASS(CHPThreadPool)
  136. };