Mirror of :pserver:anonymous@cvs.schmorp.de/schmorpforge libev http://software.schmorp.de/pkg/libev.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
5.7 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
  1. /*
  2. * libev select fd activity backend
  3. *
  4. * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer in the documentation and/or other materials provided
  17. * with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #ifndef _WIN32
  32. /* for unix systems */
  33. # include <sys/select.h>
  34. # include <inttypes.h>
  35. #endif
  36. #ifndef EV_SELECT_USE_FD_SET
  37. # ifdef NFDBITS
  38. # define EV_SELECT_USE_FD_SET 0
  39. # else
  40. # define EV_SELECT_USE_FD_SET 1
  41. # endif
  42. #endif
  43. #if EV_SELECT_IS_WINSOCKET
  44. # undef EV_SELECT_USE_FD_SET
  45. # define EV_SELECT_USE_FD_SET 1
  46. # undef EINTR
  47. # define EINTR WSAEINTR
  48. # undef EBADF
  49. # define EBADF WSAENOTSOCK
  50. # undef ENOMEM
  51. # define ENOMEM (errno + 1)
  52. #endif
  53. #if !EV_SELECT_USE_FD_SET
  54. # define NFDBYTES (NFDBITS / 8)
  55. #endif
  56. #include <string.h>
  57. static void
  58. select_modify (EV_P_ int fd, int oev, int nev)
  59. {
  60. if (oev == nev)
  61. return;
  62. {
  63. #if EV_SELECT_USE_FD_SET
  64. #if EV_SELECT_IS_WINSOCKET
  65. SOCKET handle = anfds [fd].handle;
  66. #else
  67. int handle = fd;
  68. #endif
  69. if (nev & EV_READ)
  70. FD_SET (handle, (fd_set *)vec_ri);
  71. else
  72. FD_CLR (handle, (fd_set *)vec_ri);
  73. if (nev & EV_WRITE)
  74. FD_SET (handle, (fd_set *)vec_wi);
  75. else
  76. FD_CLR (handle, (fd_set *)vec_wi);
  77. #else
  78. int word = fd / NFDBITS;
  79. int mask = 1UL << (fd % NFDBITS);
  80. if (vec_max < word + 1)
  81. {
  82. int new_max = word + 1;
  83. vec_ri = ev_realloc (vec_ri, new_max * NFDBYTES);
  84. vec_ro = ev_realloc (vec_ro, new_max * NFDBYTES); /* could free/malloc */
  85. vec_wi = ev_realloc (vec_wi, new_max * NFDBYTES);
  86. vec_wo = ev_realloc (vec_wo, new_max * NFDBYTES); /* could free/malloc */
  87. for (; vec_max < new_max; ++vec_max)
  88. ((fd_mask *)vec_ri)[vec_max] =
  89. ((fd_mask *)vec_wi)[vec_max] = 0;
  90. }
  91. ((fd_mask *)vec_ri) [word] |= mask;
  92. if (!(nev & EV_READ))
  93. ((fd_mask *)vec_ri) [word] &= ~mask;
  94. ((fd_mask *)vec_wi) [word] |= mask;
  95. if (!(nev & EV_WRITE))
  96. ((fd_mask *)vec_wi) [word] &= ~mask;
  97. #endif
  98. }
  99. }
  100. static void
  101. select_poll (EV_P_ ev_tstamp timeout)
  102. {
  103. struct timeval tv;
  104. int res;
  105. #if EV_SELECT_USE_FD_SET
  106. memcpy (vec_ro, vec_ri, sizeof (fd_set));
  107. memcpy (vec_wo, vec_wi, sizeof (fd_set));
  108. #else
  109. memcpy (vec_ro, vec_ri, vec_max * NFDBYTES);
  110. memcpy (vec_wo, vec_wi, vec_max * NFDBYTES);
  111. #endif
  112. tv.tv_sec = (long)timeout;
  113. tv.tv_usec = (long)((timeout - (ev_tstamp)tv.tv_sec) * 1e6);
  114. res = select (vec_max * NFDBITS, (fd_set *)vec_ro, (fd_set *)vec_wo, 0, &tv);
  115. if (res < 0)
  116. {
  117. #if EV_SELECT_IS_WINSOCKET
  118. errno = WSAGetLastError ();
  119. #endif
  120. if (errno == EBADF)
  121. fd_ebadf (EV_A);
  122. else if (errno == ENOMEM && !syserr_cb)
  123. fd_enomem (EV_A);
  124. else if (errno != EINTR)
  125. syserr ("(libev) select");
  126. return;
  127. }
  128. #if EV_SELECT_USE_FD_SET
  129. {
  130. int fd;
  131. for (fd = 0; fd < anfdmax; ++fd)
  132. if (anfds [fd].events)
  133. {
  134. int events = 0;
  135. #if EV_SELECT_IS_WINSOCKET
  136. SOCKET handle = anfds [fd].handle;
  137. #else
  138. int handle = fd;
  139. #endif
  140. if (FD_ISSET (handle, (fd_set *)vec_ro)) events |= EV_READ;
  141. if (FD_ISSET (handle, (fd_set *)vec_wo)) events |= EV_WRITE;
  142. if (events)
  143. fd_event (EV_A_ fd, events);
  144. }
  145. }
  146. #else
  147. {
  148. int word, bit;
  149. for (word = vec_max; word--; )
  150. {
  151. fd_mask word_r = ((fd_mask *)vec_ro) [word];
  152. fd_mask word_w = ((fd_mask *)vec_wo) [word];
  153. if (word_r || word_w)
  154. for (bit = NFDBITS; bit--; )
  155. {
  156. fd_mask mask = 1UL << bit;
  157. int events = 0;
  158. events |= word_r & mask ? EV_READ : 0;
  159. events |= word_w & mask ? EV_WRITE : 0;
  160. if (events)
  161. fd_event (EV_A_ word * NFDBITS + bit, events);
  162. }
  163. }
  164. }
  165. #endif
  166. }
  167. static int
  168. select_init (EV_P_ int flags)
  169. {
  170. backend_fudge = 1e-2; /* needed to compensate for select returning early, very conservative */
  171. backend_modify = select_modify;
  172. backend_poll = select_poll;
  173. #if EV_SELECT_USE_FD_SET
  174. vec_max = FD_SETSIZE / 32;
  175. vec_ri = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_ri);
  176. vec_ro = ev_malloc (sizeof (fd_set));
  177. vec_wi = ev_malloc (sizeof (fd_set)); FD_ZERO ((fd_set *)vec_wi);
  178. vec_wo = ev_malloc (sizeof (fd_set));
  179. #else
  180. vec_max = 0;
  181. vec_ri = 0;
  182. vec_ri = 0;
  183. vec_wo = 0;
  184. vec_wo = 0;
  185. #endif
  186. return EVBACKEND_SELECT;
  187. }
  188. static void
  189. select_destroy (EV_P)
  190. {
  191. ev_free (vec_ri);
  192. ev_free (vec_ro);
  193. ev_free (vec_wi);
  194. ev_free (vec_wo);
  195. }