epoll.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "io_watcher.h"
  2. #ifdef EVENT_EPOLL
  3. #include "hio.h"
  4. #include "hplatform.h"
  5. #include "hdef.h"
  6. #define INIT_EVENTS_NUM 64
  7. typedef struct epoll_ctx_s {
  8. int epfd;
  9. int capacity;
  10. int nevents;
  11. struct epoll_event* events;
  12. } epoll_ctx_t;
  13. static void epoll_ctx_resize(epoll_ctx_t* epoll_ctx, int size) {
  14. int bytes = sizeof(struct epoll_event) * size;
  15. epoll_ctx->events = (struct epoll_event*)realloc(epoll_ctx->events, bytes);
  16. epoll_ctx->capacity = size;
  17. }
  18. int iowatcher_init(hloop_t* loop) {
  19. if (loop->iowatcher) return 0;
  20. epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)malloc(sizeof(epoll_ctx_t));
  21. epoll_ctx->epfd = epoll_create(INIT_EVENTS_NUM);
  22. epoll_ctx->capacity = INIT_EVENTS_NUM;
  23. epoll_ctx->nevents = 0;
  24. int bytes = sizeof(struct epoll_event) * epoll_ctx->capacity;
  25. epoll_ctx->events = (struct epoll_event*)malloc(bytes);
  26. memset(epoll_ctx->events, 0, bytes);
  27. loop->iowatcher = epoll_ctx;
  28. return 0;
  29. }
  30. int iowatcher_cleanup(hloop_t* loop) {
  31. if (loop->iowatcher == NULL) return 0;
  32. epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->iowatcher;
  33. close(epoll_ctx->epfd);
  34. SAFE_FREE(epoll_ctx->events);
  35. SAFE_FREE(loop->iowatcher);
  36. return 0;
  37. }
  38. int iowatcher_add_event(hio_t* io, int events) {
  39. hloop_t* loop = io->loop;
  40. if (loop->iowatcher == NULL) {
  41. hloop_iowatcher_init(loop);
  42. }
  43. epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->iowatcher;
  44. struct epoll_event ee;
  45. ee.events = 0;
  46. ee.data.fd = io->fd;
  47. if (events & READ_EVENT) {
  48. ee.events |= EPOLLIN;
  49. }
  50. if (events & WRITE_EVENT) {
  51. ee.events |= EPOLLOUT;
  52. }
  53. int op = io->events == 0 ? EPOLL_CTL_ADD : EPOLL_CTL_MOD;
  54. epoll_ctl(epoll_ctx->epfd, op, io->fd, &ee);
  55. if (op == EPOLL_CTL_ADD) {
  56. if (epoll_ctx->nevents == epoll_ctx->capacity) {
  57. epoll_ctx_resize(epoll_ctx, epoll_ctx->capacity*2);
  58. }
  59. epoll_ctx->nevents++;
  60. }
  61. return 0;
  62. }
  63. int iowatcher_del_event(hio_t* io, int events) {
  64. hloop_t* loop = io->loop;
  65. epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->iowatcher;
  66. if (epoll_ctx == NULL) return 0;
  67. struct epoll_event ee;
  68. ee.events = io->events;
  69. ee.data.fd = io->fd;
  70. if (events & READ_EVENT) {
  71. ee.events &= ~EPOLLIN;
  72. }
  73. if (events & WRITE_EVENT) {
  74. ee.events &= ~EPOLLOUT;
  75. }
  76. int op = ee.events == 0 ? EPOLL_CTL_DEL : EPOLL_CTL_MOD;
  77. epoll_ctl(epoll_ctx->epfd, op, io->fd, &ee);
  78. if (op == EPOLL_CTL_DEL) {
  79. epoll_ctx->nevents--;
  80. }
  81. return 0;
  82. }
  83. int iowatcher_poll_events(hloop_t* loop, int timeout) {
  84. epoll_ctx_t* epoll_ctx = (epoll_ctx_t*)loop->iowatcher;
  85. if (epoll_ctx == NULL) return 0;
  86. if (epoll_ctx->nevents == 0) return 0;
  87. int nepoll = epoll_wait(epoll_ctx->epfd, epoll_ctx->events, epoll_ctx->nevents, timeout);
  88. if (nepoll < 0) {
  89. perror("epoll");
  90. return nepoll;
  91. }
  92. if (nepoll == 0) return 0;
  93. int nevent = 0;
  94. for (int i = 0; i < epoll_ctx->nevents; ++i) {
  95. if (nevent == nepoll) break;
  96. int fd = epoll_ctx->events[i].data.fd;
  97. uint32_t revents = epoll_ctx->events[i].events;
  98. if (revents) {
  99. ++nevent;
  100. hio_t* io = hio_get(loop, fd);
  101. if (io == NULL) continue;
  102. io->revents = revents;
  103. hio_handle_events(io);
  104. }
  105. }
  106. return nevent;
  107. }
  108. #endif