ae_epoll.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Linux epoll(2) based ae.c module
  2. *
  3. * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "../../inc/ae.h"
  31. #include <sys/epoll.h>
  32. typedef struct aeApiState {
  33. int epfd;
  34. struct epoll_event *events;
  35. } aeApiState;
  36. static int aeApiCreate(aeEventLoop *eventLoop) {
  37. aeApiState *state = zmalloc(sizeof(aeApiState));
  38. if (!state) return -1;
  39. state->events = zmalloc(sizeof(struct epoll_event)*eventLoop->setsize);
  40. if (!state->events) {
  41. zfree(state);
  42. return -1;
  43. }
  44. state->epfd = epoll_create(1024); /* 1024 is just a hint for the kernel */
  45. if (state->epfd == -1) {
  46. zfree(state->events);
  47. zfree(state);
  48. return -1;
  49. }
  50. eventLoop->apidata = state;
  51. return 0;
  52. }
  53. static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
  54. aeApiState *state = eventLoop->apidata;
  55. state->events = zrealloc(state->events, sizeof(struct epoll_event)*setsize);
  56. return 0;
  57. }
  58. static void aeApiFree(aeEventLoop *eventLoop) {
  59. aeApiState *state = eventLoop->apidata;
  60. close(state->epfd);
  61. zfree(state->events);
  62. zfree(state);
  63. }
  64. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
  65. aeApiState *state = eventLoop->apidata;
  66. struct epoll_event ee = {0}; /* avoid valgrind warning */
  67. /* If the fd was already monitored for some event, we need a MOD
  68. * operation. Otherwise we need an ADD operation. */
  69. int op = eventLoop->events[fd].mask == AE_NONE ?
  70. EPOLL_CTL_ADD : EPOLL_CTL_MOD;
  71. ee.events = 0;
  72. mask |= eventLoop->events[fd].mask; /* Merge old events */
  73. if (mask & AE_READABLE) ee.events |= EPOLLIN;
  74. if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
  75. ee.data.fd = fd;
  76. if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;
  77. return 0;
  78. }
  79. static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int delmask) {
  80. aeApiState *state = eventLoop->apidata;
  81. struct epoll_event ee = {0}; /* avoid valgrind warning */
  82. int mask = eventLoop->events[fd].mask & (~delmask);
  83. ee.events = 0;
  84. if (mask & AE_READABLE) ee.events |= EPOLLIN;
  85. if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
  86. ee.data.fd = fd;
  87. if (mask != AE_NONE) {
  88. epoll_ctl(state->epfd,EPOLL_CTL_MOD,fd,&ee);
  89. } else {
  90. /* Note, Kernel < 2.6.9 requires a non null event pointer even for
  91. * EPOLL_CTL_DEL. */
  92. epoll_ctl(state->epfd,EPOLL_CTL_DEL,fd,&ee);
  93. }
  94. }
  95. static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
  96. aeApiState *state = eventLoop->apidata;
  97. int retval, numevents = 0;
  98. retval = epoll_wait(state->epfd,state->events,eventLoop->setsize,
  99. tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
  100. if (retval > 0) {
  101. int j;
  102. numevents = retval;
  103. for (j = 0; j < numevents; j++) {
  104. int mask = 0;
  105. struct epoll_event *e = state->events+j;
  106. if (e->events & EPOLLIN) mask |= AE_READABLE;
  107. if (e->events & EPOLLOUT) mask |= AE_WRITABLE;
  108. if (e->events & EPOLLERR) mask |= AE_WRITABLE;
  109. if (e->events & EPOLLHUP) mask |= AE_WRITABLE;
  110. eventLoop->fired[j].fd = e->data.fd;
  111. eventLoop->fired[j].mask = mask;
  112. }
  113. }
  114. return numevents;
  115. }
  116. static char *aeApiName(void) {
  117. return "epoll";
  118. }