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