ae_select.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* Select()-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/select.h>
  32. #include <string.h>
  33. typedef struct aeApiState {
  34. fd_set rfds, wfds;
  35. /* We need to have a copy of the fd sets as it's not safe to reuse
  36. * FD sets after select(). */
  37. fd_set _rfds, _wfds;
  38. } aeApiState;
  39. static int aeApiCreate(aeEventLoop *eventLoop) {
  40. aeApiState *state = zmalloc(sizeof(aeApiState));
  41. if (!state) return -1;
  42. FD_ZERO(&state->rfds);
  43. FD_ZERO(&state->wfds);
  44. eventLoop->apidata = state;
  45. return 0;
  46. }
  47. static int aeApiResize(aeEventLoop *eventLoop, int setsize) {
  48. /* Just ensure we have enough room in the fd_set type. */
  49. if (setsize >= FD_SETSIZE) return -1;
  50. return 0;
  51. }
  52. static void aeApiFree(aeEventLoop *eventLoop) {
  53. zfree(eventLoop->apidata);
  54. }
  55. static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
  56. aeApiState *state = eventLoop->apidata;
  57. if (mask & AE_READABLE) FD_SET(fd,&state->rfds);
  58. if (mask & AE_WRITABLE) FD_SET(fd,&state->wfds);
  59. return 0;
  60. }
  61. static void aeApiDelEvent(aeEventLoop *eventLoop, int fd, int mask) {
  62. aeApiState *state = eventLoop->apidata;
  63. if (mask & AE_READABLE) FD_CLR(fd,&state->rfds);
  64. if (mask & AE_WRITABLE) FD_CLR(fd,&state->wfds);
  65. }
  66. static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
  67. aeApiState *state = eventLoop->apidata;
  68. int retval, j, numevents = 0;
  69. memcpy(&state->_rfds,&state->rfds,sizeof(fd_set));
  70. memcpy(&state->_wfds,&state->wfds,sizeof(fd_set));
  71. retval = select(eventLoop->maxfd+1,
  72. &state->_rfds,&state->_wfds,NULL,tvp);
  73. if (retval > 0) {
  74. for (j = 0; j <= eventLoop->maxfd; j++) {
  75. int mask = 0;
  76. aeFileEvent *fe = &eventLoop->events[j];
  77. if (fe->mask == AE_NONE) continue;
  78. if (fe->mask & AE_READABLE && FD_ISSET(j,&state->_rfds))
  79. mask |= AE_READABLE;
  80. if (fe->mask & AE_WRITABLE && FD_ISSET(j,&state->_wfds))
  81. mask |= AE_WRITABLE;
  82. eventLoop->fired[numevents].fd = j;
  83. eventLoop->fired[numevents].mask = mask;
  84. numevents++;
  85. }
  86. }
  87. return numevents;
  88. }
  89. static char *aeApiName(void) {
  90. return "select";
  91. }