iocp-internal.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef IOCP_INTERNAL_H_INCLUDED_
  27. #define IOCP_INTERNAL_H_INCLUDED_
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. struct event_overlapped;
  32. struct event_iocp_port;
  33. struct evbuffer;
  34. typedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_t, int success);
  35. /* This whole file is actually win32 only. We wrap the structures in a win32
  36. * ifdef so that we can test-compile code that uses these interfaces on
  37. * non-win32 platforms. */
  38. #ifdef _WIN32
  39. /**
  40. Internal use only. Wraps an OVERLAPPED that we're using for libevent
  41. functionality. Whenever an event_iocp_port gets an event for a given
  42. OVERLAPPED*, it upcasts the pointer to an event_overlapped, and calls the
  43. iocp_callback function with the event_overlapped, the iocp key, and the
  44. number of bytes transferred as arguments.
  45. */
  46. struct event_overlapped {
  47. OVERLAPPED overlapped;
  48. iocp_callback cb;
  49. };
  50. /* Mingw's headers don't define LPFN_ACCEPTEX. */
  51. typedef BOOL (WINAPI *AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED);
  52. typedef BOOL (WINAPI *ConnectExPtr)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED);
  53. typedef void (WINAPI *GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR *, LPINT, LPSOCKADDR *, LPINT);
  54. /** Internal use only. Holds pointers to functions that only some versions of
  55. Windows provide.
  56. */
  57. struct win32_extension_fns {
  58. AcceptExPtr AcceptEx;
  59. ConnectExPtr ConnectEx;
  60. GetAcceptExSockaddrsPtr GetAcceptExSockaddrs;
  61. };
  62. /**
  63. Internal use only. Stores a Windows IO Completion port, along with
  64. related data.
  65. */
  66. struct event_iocp_port {
  67. /** The port itself */
  68. HANDLE port;
  69. /* A lock to cover internal structures. */
  70. CRITICAL_SECTION lock;
  71. /** Number of threads ever open on the port. */
  72. short n_threads;
  73. /** True iff we're shutting down all the threads on this port */
  74. short shutdown;
  75. /** How often the threads on this port check for shutdown and other
  76. * conditions */
  77. long ms;
  78. /* The threads that are waiting for events. */
  79. HANDLE *threads;
  80. /** Number of threads currently open on this port. */
  81. short n_live_threads;
  82. /** A semaphore to signal when we are done shutting down. */
  83. HANDLE *shutdownSemaphore;
  84. };
  85. EVENT2_EXPORT_SYMBOL
  86. const struct win32_extension_fns *event_get_win32_extension_fns_(void);
  87. #else
  88. /* Dummy definition so we can test-compile more things on unix. */
  89. struct event_overlapped {
  90. iocp_callback cb;
  91. };
  92. #endif
  93. /** Initialize the fields in an event_overlapped.
  94. @param overlapped The struct event_overlapped to initialize
  95. @param cb The callback that should be invoked once the IO operation has
  96. finished.
  97. */
  98. EVENT2_EXPORT_SYMBOL
  99. void event_overlapped_init_(struct event_overlapped *, iocp_callback cb);
  100. /** Allocate and return a new evbuffer that supports overlapped IO on a given
  101. socket. The socket must be associated with an IO completion port using
  102. event_iocp_port_associate_.
  103. */
  104. EVENT2_EXPORT_SYMBOL
  105. struct evbuffer *evbuffer_overlapped_new_(evutil_socket_t fd);
  106. /** XXXX Document (nickm) */
  107. evutil_socket_t evbuffer_overlapped_get_fd_(struct evbuffer *buf);
  108. void evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd);
  109. /** Start reading data onto the end of an overlapped evbuffer.
  110. An evbuffer can only have one read pending at a time. While the read
  111. is in progress, no other data may be added to the end of the buffer.
  112. The buffer must be created with event_overlapped_init_().
  113. evbuffer_commit_read_() must be called in the completion callback.
  114. @param buf The buffer to read onto
  115. @param n The number of bytes to try to read.
  116. @param ol Overlapped object with associated completion callback.
  117. @return 0 on success, -1 on error.
  118. */
  119. EVENT2_EXPORT_SYMBOL
  120. int evbuffer_launch_read_(struct evbuffer *buf, size_t n, struct event_overlapped *ol);
  121. /** Start writing data from the start of an evbuffer.
  122. An evbuffer can only have one write pending at a time. While the write is
  123. in progress, no other data may be removed from the front of the buffer.
  124. The buffer must be created with event_overlapped_init_().
  125. evbuffer_commit_write_() must be called in the completion callback.
  126. @param buf The buffer to read onto
  127. @param n The number of bytes to try to read.
  128. @param ol Overlapped object with associated completion callback.
  129. @return 0 on success, -1 on error.
  130. */
  131. EVENT2_EXPORT_SYMBOL
  132. int evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol);
  133. /** XXX document */
  134. EVENT2_EXPORT_SYMBOL
  135. void evbuffer_commit_read_(struct evbuffer *, ev_ssize_t);
  136. EVENT2_EXPORT_SYMBOL
  137. void evbuffer_commit_write_(struct evbuffer *, ev_ssize_t);
  138. /** Create an IOCP, and launch its worker threads. Internal use only.
  139. This interface is unstable, and will change.
  140. */
  141. EVENT2_EXPORT_SYMBOL
  142. struct event_iocp_port *event_iocp_port_launch_(int n_cpus);
  143. /** Associate a file descriptor with an iocp, such that overlapped IO on the
  144. fd will happen on one of the iocp's worker threads.
  145. */
  146. EVENT2_EXPORT_SYMBOL
  147. int event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
  148. ev_uintptr_t key);
  149. /** Tell all threads serving an iocp to stop. Wait for up to waitMsec for all
  150. the threads to finish whatever they're doing. If waitMsec is -1, wait
  151. as long as required. If all the threads are done, free the port and return
  152. 0. Otherwise, return -1. If you get a -1 return value, it is safe to call
  153. this function again.
  154. */
  155. EVENT2_EXPORT_SYMBOL
  156. int event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec);
  157. /* FIXME document. */
  158. EVENT2_EXPORT_SYMBOL
  159. int event_iocp_activate_overlapped_(struct event_iocp_port *port,
  160. struct event_overlapped *o,
  161. ev_uintptr_t key, ev_uint32_t n_bytes);
  162. struct event_base;
  163. /* FIXME document. */
  164. EVENT2_EXPORT_SYMBOL
  165. struct event_iocp_port *event_base_get_iocp_(struct event_base *base);
  166. /* FIXME document. */
  167. EVENT2_EXPORT_SYMBOL
  168. int event_base_start_iocp_(struct event_base *base, int n_cpus);
  169. void event_base_stop_iocp_(struct event_base *base);
  170. /* FIXME document. */
  171. EVENT2_EXPORT_SYMBOL
  172. struct bufferevent *bufferevent_async_new_(struct event_base *base,
  173. evutil_socket_t fd, int options);
  174. /* FIXME document. */
  175. void bufferevent_async_set_connected_(struct bufferevent *bev);
  176. int bufferevent_async_can_connect_(struct bufferevent *bev);
  177. int bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
  178. const struct sockaddr *sa, int socklen);
  179. #ifdef __cplusplus
  180. }
  181. #endif
  182. #endif