ae_kqueue.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Kqueue(2)-based ae.c module
  2. *
  3. * Copyright (C) 2009 Harish Mallipeddi - harish.mallipeddi@gmail.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 <sys/types.h>
  31. #include <sys/event.h>
  32. #include <sys/time.h>
  33. typedef struct aeApiState {
  34. int kqfd;
  35. struct kevent *events;
  36. } aeApiState;
  37. static int aeApiCreate(aeEventLoop *eventLoop) {
  38. aeApiState *state = zmalloc(sizeof(aeApiState));
  39. if (!state) return -1;
  40. state->events = zmalloc(sizeof(struct kevent)*eventLoop->setsize);
  41. if (!state->events) {
  42. zfree(state);
  43. return -1;
  44. }
  45. state->kqfd = kqueue();
  46. if (state->kqfd == -1) {
  47. zfree(state->events);
  48. zfree(state);
  49. return -1;
  50. }
  51. eventLoop->apidata = state;
  52. return 0;
  53. }
  54. static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
  55. aeApiState *state = eventLoop->apidata;
  56. state->events = zrealloc(state->events, sizeof(struct kevent)*setsize);
  57. return 0;
  58. }
  59. static void aeApiFree(aeEventLoop *eventLoop) {
  60. aeApiState *state = eventLoop->apidata;
  61. close(state->kqfd);
  62. zfree(state->events);
  63. zfree(state);
  64. }
  65. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
  66. aeApiState *state = eventLoop->apidata;
  67. struct kevent ke;
  68. if (mask & AE_READABLE) {
  69. EV_SET(&ke, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
  70. if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;
  71. }
  72. if (mask & AE_WRITABLE) {
  73. EV_SET(&ke, fd, EVFILT_WRITE, EV_ADD, 0, 0, NULL);
  74. if (kevent(state->kqfd, &ke, 1, NULL, 0, NULL) == -1) return -1;
  75. }
  76. return 0;
  77. }
  78. static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
  79. aeApiState *state = eventLoop->apidata;
  80. struct kevent ke;
  81. if (mask & AE_READABLE) {
  82. EV_SET(&ke, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
  83. kevent(state->kqfd, &ke, 1, NULL, 0, NULL);
  84. }
  85. if (mask & AE_WRITABLE) {
  86. EV_SET(&ke, fd, EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
  87. kevent(state->kqfd, &ke, 1, NULL, 0, NULL);
  88. }
  89. }
  90. static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
  91. aeApiState *state = eventLoop->apidata;
  92. int retval, numevents = 0;
  93. if (tvp != NULL) {
  94. struct timespec timeout;
  95. timeout.tv_sec = tvp->tv_sec;
  96. timeout.tv_nsec = tvp->tv_usec * 1000;
  97. retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
  98. &timeout);
  99. } else {
  100. retval = kevent(state->kqfd, NULL, 0, state->events, eventLoop->setsize,
  101. NULL);
  102. }
  103. if (retval > 0) {
  104. int j;
  105. numevents = retval;
  106. for(j = 0; j < numevents; j++) {
  107. int mask = 0;
  108. struct kevent *e = state->events+j;
  109. if (e->filter == EVFILT_READ) mask |= AE_READABLE;
  110. if (e->filter == EVFILT_WRITE) mask |= AE_WRITABLE;
  111. eventLoop->fired[j].fd = e->ident;
  112. eventLoop->fired[j].mask = mask;
  113. }
  114. }
  115. return numevents;
  116. }
  117. static char *aeApiName(void) {
  118. return "kqueue";
  119. }