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