关键词搜索

源码搜索 ×
×

漫话Redis源码之八十五

发布2022-02-20浏览508次

详情内容

学习C语言,一定要了解宏,开头这部分就是好了兼容不同平台,其余的代码主要还是状态位的设置:

  1. /* Include the best multiplexing layer supported by this system.
  2. * The following should be ordered by performances, descending. */
  3. #ifdef HAVE_EVPORT
  4. #include "ae_evport.c"
  5. #else
  6. #ifdef HAVE_EPOLL
  7. #include "ae_epoll.c"
  8. #else
  9. #ifdef HAVE_KQUEUE
  10. #include "ae_kqueue.c"
  11. #else
  12. #include "ae_select.c"
  13. #endif
  14. #endif
  15. #endif
  16. aeEventLoop *aeCreateEventLoop(int setsize) {
  17. aeEventLoop *eventLoop;
  18. int i;
  19. monotonicInit(); /* just in case the calling app didn't initialize */
  20. if ((eventLoop = zmalloc(sizeof(*eventLoop))) == NULL) goto err;
  21. eventLoop->events = zmalloc(sizeof(aeFileEvent)*setsize);
  22. eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
  23. if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
  24. eventLoop->setsize = setsize;
  25. eventLoop->timeEventHead = NULL;
  26. eventLoop->timeEventNextId = 0;
  27. eventLoop->stop = 0;
  28. eventLoop->maxfd = -1;
  29. eventLoop->beforesleep = NULL;
  30. eventLoop->aftersleep = NULL;
  31. eventLoop->flags = 0;
  32. if (aeApiCreate(eventLoop) == -1) goto err;
  33. /* Events with mask == AE_NONE are not set. So let's initialize the
  34. * vector with it. */
  35. for (i = 0; i < setsize; i++)
  36. eventLoop->events[i].mask = AE_NONE;
  37. return eventLoop;
  38. err:
  39. if (eventLoop) {
  40. zfree(eventLoop->events);
  41. zfree(eventLoop->fired);
  42. zfree(eventLoop);
  43. }
  44. return NULL;
  45. }
  46. /* Return the current set size. */
  47. int aeGetSetSize(aeEventLoop *eventLoop) {
  48. return eventLoop->setsize;
  49. }
  50. /* Tells the next iteration/s of the event processing to set timeout of 0. */
  51. void aeSetDontWait(aeEventLoop *eventLoop, int noWait) {
  52. if (noWait)
  53. eventLoop->flags |= AE_DONT_WAIT;
  54. else
  55. eventLoop->flags &= ~AE_DONT_WAIT;
  56. }
  57. /* Resize the maximum set size of the event loop.
  58. * If the requested set size is smaller than the current set size, but
  59. * there is already a file descriptor in use that is >= the requested
  60. * set size minus one, AE_ERR is returned and the operation is not
  61. * performed at all.
  62. *
  63. * Otherwise AE_OK is returned and the operation is successful. */
  64. int aeResizeSetSize(aeEventLoop *eventLoop, int setsize) {
  65. int i;
  66. if (setsize == eventLoop->setsize) return AE_OK;
  67. if (eventLoop->maxfd >= setsize) return AE_ERR;
  68. if (aeApiResize(eventLoop,setsize) == -1) return AE_ERR;
  69. eventLoop->events = zrealloc(eventLoop->events,sizeof(aeFileEvent)*setsize);
  70. eventLoop->fired = zrealloc(eventLoop->fired,sizeof(aeFiredEvent)*setsize);
  71. eventLoop->setsize = setsize;
  72. /* Make sure that if we created new slots, they are initialized with
  73. * an AE_NONE mask. */
  74. for (i = eventLoop->maxfd+1; i < setsize; i++)
  75. eventLoop->events[i].mask = AE_NONE;
  76. return AE_OK;
  77. }
  78. void aeDeleteEventLoop(aeEventLoop *eventLoop) {
  79. aeApiFree(eventLoop);
  80. zfree(eventLoop->events);
  81. zfree(eventLoop->fired);
  82. /* Free the time events list. */
  83. aeTimeEvent *next_te, *te = eventLoop->timeEventHead;
  84. while (te) {
  85. next_te = te->next;
  86. zfree(te);
  87. te = next_te;
  88. }
  89. zfree(eventLoop);
  90. }
  91. void aeStop(aeEventLoop *eventLoop) {
  92. eventLoop->stop = 1;
  93. }
  94. int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
  95. aeFileProc *proc, void *clientData)
  96. {
  97. if (fd >= eventLoop->setsize) {
  98. errno = ERANGE;
  99. return AE_ERR;
  100. }
  101. aeFileEvent *fe = &eventLoop->events[fd];
  102. if (aeApiAddEvent(eventLoop, fd, mask) == -1)
  103. return AE_ERR;
  104. fe->mask |= mask;
  105. if (mask & AE_READABLE) fe->rfileProc = proc;
  106. if (mask & AE_WRITABLE) fe->wfileProc = proc;
  107. fe->clientData = clientData;
  108. if (fd > eventLoop->maxfd)
  109. eventLoop->maxfd = fd;
  110. return AE_OK;
  111. }
  112. void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
  113. {
  114. if (fd >= eventLoop->setsize) return;
  115. aeFileEvent *fe = &eventLoop->events[fd];
  116. if (fe->mask == AE_NONE) return;
  117. /* We want to always remove AE_BARRIER if set when AE_WRITABLE
  118. * is removed. */
  119. if (mask & AE_WRITABLE) mask |= AE_BARRIER;
  120. aeApiDelEvent(eventLoop, fd, mask);
  121. fe->mask = fe->mask & (~mask);
  122. if (fd == eventLoop->maxfd && fe->mask == AE_NONE) {
  123. /* Update the max fd */
  124. int j;
  125. for (j = eventLoop->maxfd-1; j >= 0; j--)
  126. if (eventLoop->events[j].mask != AE_NONE) break;
  127. eventLoop->maxfd = j;
  128. }
  129. }
  130. int aeGetFileEvents(aeEventLoop *eventLoop, int fd) {
  131. if (fd >= eventLoop->setsize) return 0;
  132. aeFileEvent *fe = &eventLoop->events[fd];
  133. return fe->mask;
  134. }
  135. long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
  136. aeTimeProc *proc, void *clientData,
  137. aeEventFinalizerProc *finalizerProc)
  138. {
  139. long long id = eventLoop->timeEventNextId++;
  140. aeTimeEvent *te;
  141. te = zmalloc(sizeof(*te));
  142. if (te == NULL) return AE_ERR;
  143. te->id = id;
  144. te->when = getMonotonicUs() + milliseconds * 1000;
  145. te->timeProc = proc;
  146. te->finalizerProc = finalizerProc;
  147. te->clientData = clientData;
  148. te->prev = NULL;
  149. te->next = eventLoop->timeEventHead;
  150. te->refcount = 0;
  151. if (te->next)
  152. te->next->prev = te;
  153. eventLoop->timeEventHead = te;
  154. return id;
  155. }
  156. int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)
  157. {
  158. aeTimeEvent *te = eventLoop->timeEventHead;
  159. while(te) {
  160. if (te->id == id) {
  161. te->id = AE_DELETED_EVENT_ID;
  162. return AE_OK;
  163. }
  164. te = te->next;
  165. }
  166. return AE_ERR; /* NO event with the specified ID found */
  167. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载