关键词搜索

源码搜索 ×
×

漫话Redis源码之八十四

发布2022-02-20浏览540次

详情内容

这里主要是epoll相关的代码,涉及状态位控制:

  1. #include <sys/epoll.h>
  2. typedef struct aeApiState {
  3. int epfd;
  4. struct epoll_event *events;
  5. } aeApiState;
  6. static int aeApiCreate(aeEventLoop *eventLoop) {
  7. aeApiState *state = zmalloc(sizeof(aeApiState));
  8. if (!state) return -1;
  9. state->events = zmalloc(sizeof(struct epoll_event)*eventLoop->setsize);
  10. if (!state->events) {
  11. zfree(state);
  12. return -1;
  13. }
  14. state->epfd = epoll_create(1024); /* 1024 is just a hint for the kernel */
  15. if (state->epfd == -1) {
  16. zfree(state->events);
  17. zfree(state);
  18. return -1;
  19. }
  20. anetCloexec(state->epfd);
  21. eventLoop->apidata = state;
  22. return 0;
  23. }
  24. static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
  25. aeApiState *state = eventLoop->apidata;
  26. state->events = zrealloc(state->events, sizeof(struct epoll_event)*setsize);
  27. return 0;
  28. }
  29. static void aeApiFree(aeEventLoop *eventLoop) {
  30. aeApiState *state = eventLoop->apidata;
  31. close(state->epfd);
  32. zfree(state->events);
  33. zfree(state);
  34. }
  35. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
  36. aeApiState *state = eventLoop->apidata;
  37. struct epoll_event ee = {0}; /* avoid valgrind warning */
  38. /* If the fd was already monitored for some event, we need a MOD
  39. * operation. Otherwise we need an ADD operation. */
  40. int op = eventLoop->events[fd].mask == AE_NONE ?
  41. EPOLL_CTL_ADD : EPOLL_CTL_MOD;
  42. ee.events = 0;
  43. mask |= eventLoop->events[fd].mask; /* Merge old events */
  44. if (mask & AE_READABLE) ee.events |= EPOLLIN;
  45. if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
  46. ee.data.fd = fd;
  47. if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;
  48. return 0;
  49. }
  50. static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) {
  51. aeApiState *state = eventLoop->apidata;
  52. struct epoll_event ee = {0}; /* avoid valgrind warning */
  53. int mask = eventLoop->events[fd].mask & (~delmask);
  54. ee.events = 0;
  55. if (mask & AE_READABLE) ee.events |= EPOLLIN;
  56. if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
  57. ee.data.fd = fd;
  58. if (mask != AE_NONE) {
  59. epoll_ctl(state->epfd,EPOLL_CTL_MOD,fd,&ee);
  60. } else {
  61. /* Note, Kernel < 2.6.9 requires a non null event pointer even for
  62. * EPOLL_CTL_DEL. */
  63. epoll_ctl(state->epfd,EPOLL_CTL_DEL,fd,&ee);
  64. }
  65. }
  66. static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
  67. aeApiState *state = eventLoop->apidata;
  68. int retval, numevents = 0;
  69. retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
  70. tvp ? (tvp->tv_sec*1000 + (tvp->tv_usec + 999)/1000) : -1);
  71. if (retval > 0) {
  72. int j;
  73. numevents = retval;
  74. for (j = 0; j < numevents; j++) {
  75. int mask = 0;
  76. struct epoll_event *e = state->events+j;
  77. if (e->events & EPOLLIN) mask |= AE_READABLE;
  78. if (e->events & EPOLLOUT) mask |= AE_WRITABLE;
  79. if (e->events & EPOLLERR) mask |= AE_WRITABLE|AE_READABLE;
  80. if (e->events & EPOLLHUP) mask |= AE_WRITABLE|AE_READABLE;
  81. eventLoop->fired[j].fd = e->data.fd;
  82. eventLoop->fired[j].mask = mask;
  83. }
  84. }
  85. return numevents;
  86. }
  87. static char *aeApiName(void) {
  88. return "epoll";
  89. }

相关技术文章

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

提示信息

×

选择支付方式

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