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