IODispatcher.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include "IODispatcher.h"
  24. #include "FuncHelper.h"
  25. #include <signal.h>
  26. #include <pthread.h>
  27. volatile UINT CIODispatcher::sm_uiNum = 0;
  28. LPCTSTR CIODispatcher::WORKER_THREAD_PREFIX = _T("io-disp-");
  29. BOOL CIODispatcher::Start(IIOHandler* pHandler, int iWorkerMaxEvents, int iWorkers, LLONG llTimerInterval)
  30. {
  31. ASSERT_CHECK_EINVAL(pHandler && iWorkerMaxEvents >= 0 && iWorkers >= 0);
  32. CHECK_ERROR(!HasStarted(), ERROR_INVALID_STATE);
  33. if(iWorkerMaxEvents == 0) iWorkerMaxEvents = DEF_WORKER_MAX_EVENTS;
  34. if(iWorkers == 0) iWorkers = DEFAULT_WORKER_THREAD_COUNT;
  35. m_iMaxEvents = iWorkerMaxEvents;
  36. m_iWorkers = iWorkers;
  37. m_pHandler = pHandler;
  38. m_epoll = epoll_create1(EPOLL_CLOEXEC);
  39. CHECK_ERROR_FD(m_epoll);
  40. m_evCmd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
  41. if(IS_INVALID_FD(m_evCmd))
  42. goto START_ERROR;
  43. if(!VERIFY(AddFD(m_evCmd, EPOLLIN | EPOLLET, &m_evCmd)))
  44. goto START_ERROR;
  45. m_evExit = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC | EFD_SEMAPHORE);
  46. if(IS_INVALID_FD(m_evExit))
  47. goto START_ERROR;
  48. if(!VERIFY(AddFD(m_evExit, EPOLLIN, &m_evExit)))
  49. goto START_ERROR;
  50. if(llTimerInterval > 0)
  51. {
  52. m_evTimer = AddTimer(llTimerInterval, &m_evTimer);
  53. if(IS_INVALID_FD(m_evTimer))
  54. goto START_ERROR;
  55. }
  56. sigset_t ss;
  57. sigemptyset(&ss);
  58. sigaddset(&ss, SIGPIPE);
  59. VERIFY_IS_NO_ERROR(pthread_sigmask(SIG_BLOCK, &ss, nullptr));
  60. m_pWorkers = make_unique<CWorkerThread[]>(m_iWorkers);
  61. for(int i = 0; i < m_iWorkers; i++)
  62. {
  63. if(!VERIFY(m_pWorkers[i].Start(this, &CIODispatcher::WorkerProc)))
  64. goto START_ERROR;
  65. }
  66. return TRUE;
  67. START_ERROR:
  68. EXECUTE_RESTORE_ERROR(Stop(FALSE));
  69. return FALSE;
  70. }
  71. BOOL CIODispatcher::Stop(BOOL bCheck)
  72. {
  73. if(bCheck) CHECK_ERROR(HasStarted(), ERROR_INVALID_STATE);
  74. BOOL isOK = TRUE;
  75. if(m_pWorkers)
  76. {
  77. isOK &= IS_NO_ERROR(eventfd_write(m_evExit, m_iWorkers));
  78. for(int i = 0; i < m_iWorkers; i++)
  79. isOK &= m_pWorkers[i].Join();
  80. }
  81. if(!m_queue.IsEmpty())
  82. {
  83. TDispCommand* pCmd = nullptr;
  84. while(m_queue.PopFront(&pCmd))
  85. TDispCommand::Destruct(pCmd);
  86. VERIFY(m_queue.IsEmpty());
  87. }
  88. if(IS_VALID_FD(m_evExit))
  89. isOK &= IS_NO_ERROR(close(m_evExit));
  90. if(IS_VALID_FD(m_evCmd))
  91. isOK &= IS_NO_ERROR(close(m_evCmd));
  92. if(IS_VALID_FD(m_evTimer))
  93. isOK &= IS_NO_ERROR(close(m_evTimer));
  94. if(IS_VALID_FD(m_epoll))
  95. isOK &= IS_NO_ERROR(close(m_epoll));
  96. Reset();
  97. return isOK;
  98. }
  99. VOID CIODispatcher::Reset()
  100. {
  101. m_uiSeq = 0;
  102. m_iWorkers = 0;
  103. m_iMaxEvents= 0;
  104. m_pHandler = nullptr;
  105. m_pWorkers = nullptr;
  106. m_epoll = INVALID_FD;
  107. m_evCmd = INVALID_FD;
  108. m_evExit = INVALID_FD;
  109. m_evTimer = INVALID_FD;
  110. }
  111. void CIODispatcher::MakePrefix()
  112. {
  113. UINT uiNumber = ::InterlockedIncrement(&sm_uiNum);
  114. m_strPrefix.Format(_T("%s%u-"), WORKER_THREAD_PREFIX, uiNumber);
  115. }
  116. BOOL CIODispatcher::SendCommand(USHORT t, UINT_PTR wp, UINT_PTR lp)
  117. {
  118. return SendCommand(TDispCommand::Construct(t, wp, lp));
  119. }
  120. BOOL CIODispatcher::SendCommand(TDispCommand* pCmd)
  121. {
  122. m_queue.PushBack(pCmd);
  123. return VERIFY_IS_NO_ERROR(eventfd_write(m_evCmd, 1));
  124. }
  125. BOOL CIODispatcher::CtlFD(FD fd, int op, UINT mask, PVOID pv)
  126. {
  127. epoll_event evt = {mask, pv};
  128. return IS_NO_ERROR(epoll_ctl(m_epoll, op, fd, &evt));
  129. }
  130. int CIODispatcher::WorkerProc(PVOID pv)
  131. {
  132. ::SetSequenceThreadName(SELF_THREAD_ID, m_strPrefix, m_uiSeq);
  133. m_pHandler->OnDispatchThreadStart(SELF_THREAD_ID);
  134. BOOL bRun = TRUE;
  135. unique_ptr<epoll_event[]> pEvents = make_unique<epoll_event[]>(m_iMaxEvents);
  136. while(bRun)
  137. {
  138. int rs = NO_EINTR_INT(epoll_pwait(m_epoll, pEvents.get(), m_iMaxEvents, INFINITE, nullptr));
  139. if(rs <= TIMEOUT)
  140. ERROR_ABORT();
  141. for(int i = 0; i < rs; i++)
  142. {
  143. UINT events = pEvents[i].events;
  144. PVOID ptr = pEvents[i].data.ptr;
  145. if(ptr == &m_evCmd)
  146. ProcessCommand(events);
  147. else if(ptr == &m_evTimer)
  148. ProcessTimer(events);
  149. else if(ptr == &m_evExit)
  150. bRun = ProcessExit(events);
  151. else
  152. ProcessIo(ptr, events);
  153. }
  154. }
  155. m_pHandler->OnDispatchThreadEnd(SELF_THREAD_ID);
  156. return 0;
  157. }
  158. BOOL CIODispatcher::ProcessCommand(UINT events)
  159. {
  160. if(events & _EPOLL_ALL_ERROR_EVENTS)
  161. ERROR_ABORT();
  162. if(!(events & EPOLLIN))
  163. return FALSE;
  164. BOOL isOK = TRUE;
  165. eventfd_t v;
  166. int rs = eventfd_read(m_evCmd, &v);
  167. if(IS_NO_ERROR(rs))
  168. {
  169. ASSERT(v > 0);
  170. TDispCommand* pCmd = nullptr;
  171. while(m_queue.PopFront(&pCmd))
  172. {
  173. m_pHandler->OnCommand(pCmd);
  174. TDispCommand::Destruct(pCmd);
  175. }
  176. }
  177. else if(IS_HAS_ERROR(rs))
  178. {
  179. ASSERT(IS_WOULDBLOCK_ERROR());
  180. isOK = FALSE;
  181. }
  182. return isOK;
  183. }
  184. BOOL CIODispatcher::ProcessTimer(UINT events)
  185. {
  186. if(events & _EPOLL_ALL_ERROR_EVENTS)
  187. ERROR_ABORT();
  188. if(!(events & EPOLLIN))
  189. return TRUE;
  190. BOOL isOK = FALSE;
  191. ULLONG ullExpirations;
  192. if(::ReadTimer(m_evTimer, &ullExpirations, &isOK) && isOK)
  193. m_pHandler->OnTimer(ullExpirations);
  194. else
  195. ASSERT(IS_WOULDBLOCK_ERROR());
  196. return isOK;
  197. }
  198. BOOL CIODispatcher::ProcessExit(UINT events)
  199. {
  200. if(events & _EPOLL_ALL_ERROR_EVENTS)
  201. ERROR_ABORT();
  202. if(!(events & EPOLLIN))
  203. return TRUE;
  204. BOOL bRun = TRUE;
  205. eventfd_t v;
  206. int rs = eventfd_read(m_evExit, &v);
  207. if(IS_HAS_ERROR(rs))
  208. ASSERT(IS_WOULDBLOCK_ERROR());
  209. else
  210. {
  211. ASSERT(v == 1);
  212. bRun = FALSE;
  213. }
  214. return bRun;
  215. }
  216. BOOL CIODispatcher::ProcessIo(PVOID pv, UINT events)
  217. {
  218. if(!m_pHandler->OnBeforeProcessIo(pv, events))
  219. return FALSE;
  220. BOOL rs = DoProcessIo(pv, events);
  221. m_pHandler->OnAfterProcessIo(pv, events, rs);
  222. return rs;
  223. }
  224. BOOL CIODispatcher::DoProcessIo(PVOID pv, UINT events)
  225. {
  226. if(events & EPOLLERR)
  227. return m_pHandler->OnError(pv, events);
  228. if((events & EPOLLPRI) && !m_pHandler->OnReadyPrivilege(pv, events))
  229. return FALSE;
  230. if((events & EPOLLIN) && !m_pHandler->OnReadyRead(pv, events))
  231. return FALSE;
  232. if((events & EPOLLOUT) && !m_pHandler->OnReadyWrite(pv, events))
  233. return FALSE;
  234. if((events & (_EPOLL_HUNGUP_EVENTS)) && !m_pHandler->OnHungUp(pv, events))
  235. return FALSE;
  236. return TRUE;
  237. }
  238. FD CIODispatcher::AddTimer(LLONG llInterval, PVOID pv)
  239. {
  240. FD fdTimer = ::CreateTimer(llInterval);
  241. if(IS_VALID_FD(fdTimer))
  242. {
  243. if(!AddFD(fdTimer, EPOLLIN | EPOLLET, pv))
  244. {
  245. close(fdTimer);
  246. fdTimer = INVALID_FD;
  247. }
  248. }
  249. return fdTimer;
  250. }
  251. BOOL CIODispatcher::DelTimer(FD fdTimer)
  252. {
  253. BOOL isOK = FALSE;
  254. if(IS_VALID_FD(fdTimer))
  255. {
  256. if(DelFD(fdTimer))
  257. isOK = TRUE;
  258. close(fdTimer);
  259. }
  260. return isOK;
  261. }