ipc/chromium/src/third_party/libevent/iocp-internal.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/third_party/libevent/iocp-internal.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     1.4 +/*
     1.5 + * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
     1.6 + *
     1.7 + * Redistribution and use in source and binary forms, with or without
     1.8 + * modification, are permitted provided that the following conditions
     1.9 + * are met:
    1.10 + * 1. Redistributions of source code must retain the above copyright
    1.11 + *    notice, this list of conditions and the following disclaimer.
    1.12 + * 2. Redistributions in binary form must reproduce the above copyright
    1.13 + *    notice, this list of conditions and the following disclaimer in the
    1.14 + *    documentation and/or other materials provided with the distribution.
    1.15 + * 3. The name of the author may not be used to endorse or promote products
    1.16 + *    derived from this software without specific prior written permission.
    1.17 + *
    1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.28 + */
    1.29 +
    1.30 +#ifndef _EVENT_IOCP_INTERNAL_H
    1.31 +#define _EVENT_IOCP_INTERNAL_H
    1.32 +
    1.33 +#ifdef __cplusplus
    1.34 +extern "C" {
    1.35 +#endif
    1.36 +
    1.37 +struct event_overlapped;
    1.38 +struct event_iocp_port;
    1.39 +struct evbuffer;
    1.40 +typedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_t, int success);
    1.41 +
    1.42 +/* This whole file is actually win32 only. We wrap the structures in a win32
    1.43 + * ifdef so that we can test-compile code that uses these interfaces on
    1.44 + * non-win32 platforms. */
    1.45 +#ifdef WIN32
    1.46 +
    1.47 +/**
    1.48 +   Internal use only.  Wraps an OVERLAPPED that we're using for libevent
    1.49 +   functionality.  Whenever an event_iocp_port gets an event for a given
    1.50 +   OVERLAPPED*, it upcasts the pointer to an event_overlapped, and calls the
    1.51 +   iocp_callback function with the event_overlapped, the iocp key, and the
    1.52 +   number of bytes transferred as arguments.
    1.53 + */
    1.54 +struct event_overlapped {
    1.55 +	OVERLAPPED overlapped;
    1.56 +	iocp_callback cb;
    1.57 +};
    1.58 +
    1.59 +/* Mingw's headers don't define LPFN_ACCEPTEX. */
    1.60 +
    1.61 +typedef BOOL (WINAPI *AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED);
    1.62 +typedef BOOL (WINAPI *ConnectExPtr)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED);
    1.63 +typedef void (WINAPI *GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR *, LPINT, LPSOCKADDR *, LPINT);
    1.64 +
    1.65 +/** Internal use only. Holds pointers to functions that only some versions of
    1.66 +    Windows provide.
    1.67 + */
    1.68 +struct win32_extension_fns {
    1.69 +	AcceptExPtr AcceptEx;
    1.70 +	ConnectExPtr ConnectEx;
    1.71 +	GetAcceptExSockaddrsPtr GetAcceptExSockaddrs;
    1.72 +};
    1.73 +
    1.74 +/**
    1.75 +    Internal use only. Stores a Windows IO Completion port, along with
    1.76 +    related data.
    1.77 + */
    1.78 +struct event_iocp_port {
    1.79 +	/** The port itself */
    1.80 +	HANDLE port;
    1.81 +	/* A lock to cover internal structures. */
    1.82 +	CRITICAL_SECTION lock;
    1.83 +	/** Number of threads ever open on the port. */
    1.84 +	short n_threads;
    1.85 +	/** True iff we're shutting down all the threads on this port */
    1.86 +	short shutdown;
    1.87 +	/** How often the threads on this port check for shutdown and other
    1.88 +	 * conditions */
    1.89 +	long ms;
    1.90 +	/* The threads that are waiting for events. */
    1.91 +	HANDLE *threads;
    1.92 +	/** Number of threads currently open on this port. */
    1.93 +	short n_live_threads;
    1.94 +	/** A semaphore to signal when we are done shutting down. */
    1.95 +	HANDLE *shutdownSemaphore;
    1.96 +};
    1.97 +
    1.98 +const struct win32_extension_fns *event_get_win32_extension_fns(void);
    1.99 +#else
   1.100 +/* Dummy definition so we can test-compile more things on unix. */
   1.101 +struct event_overlapped {
   1.102 +	iocp_callback cb;
   1.103 +};
   1.104 +#endif
   1.105 +
   1.106 +/** Initialize the fields in an event_overlapped.
   1.107 +
   1.108 +    @param overlapped The struct event_overlapped to initialize
   1.109 +    @param cb The callback that should be invoked once the IO operation has
   1.110 +	finished.
   1.111 + */
   1.112 +void event_overlapped_init(struct event_overlapped *, iocp_callback cb);
   1.113 +
   1.114 +/** Allocate and return a new evbuffer that supports overlapped IO on a given
   1.115 +    socket.  The socket must be associated with an IO completion port using
   1.116 +    event_iocp_port_associate.
   1.117 +*/
   1.118 +struct evbuffer *evbuffer_overlapped_new(evutil_socket_t fd);
   1.119 +
   1.120 +/** XXXX Document (nickm) */
   1.121 +evutil_socket_t _evbuffer_overlapped_get_fd(struct evbuffer *buf);
   1.122 +
   1.123 +void _evbuffer_overlapped_set_fd(struct evbuffer *buf, evutil_socket_t fd);
   1.124 +
   1.125 +/** Start reading data onto the end of an overlapped evbuffer.
   1.126 +
   1.127 +    An evbuffer can only have one read pending at a time.  While the read
   1.128 +    is in progress, no other data may be added to the end of the buffer.
   1.129 +    The buffer must be created with event_overlapped_init().
   1.130 +    evbuffer_commit_read() must be called in the completion callback.
   1.131 +
   1.132 +    @param buf The buffer to read onto
   1.133 +    @param n The number of bytes to try to read.
   1.134 +    @param ol Overlapped object with associated completion callback.
   1.135 +    @return 0 on success, -1 on error.
   1.136 + */
   1.137 +int evbuffer_launch_read(struct evbuffer *buf, size_t n, struct event_overlapped *ol);
   1.138 +
   1.139 +/** Start writing data from the start of an evbuffer.
   1.140 +
   1.141 +    An evbuffer can only have one write pending at a time.  While the write is
   1.142 +    in progress, no other data may be removed from the front of the buffer.
   1.143 +    The buffer must be created with event_overlapped_init().
   1.144 +    evbuffer_commit_write() must be called in the completion callback.
   1.145 +
   1.146 +    @param buf The buffer to read onto
   1.147 +    @param n The number of bytes to try to read.
   1.148 +    @param ol Overlapped object with associated completion callback.
   1.149 +    @return 0 on success, -1 on error.
   1.150 + */
   1.151 +int evbuffer_launch_write(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol);
   1.152 +
   1.153 +/** XXX document */
   1.154 +void evbuffer_commit_read(struct evbuffer *, ev_ssize_t);
   1.155 +void evbuffer_commit_write(struct evbuffer *, ev_ssize_t);
   1.156 +
   1.157 +/** Create an IOCP, and launch its worker threads.  Internal use only.
   1.158 +
   1.159 +    This interface is unstable, and will change.
   1.160 + */
   1.161 +struct event_iocp_port *event_iocp_port_launch(int n_cpus);
   1.162 +
   1.163 +/** Associate a file descriptor with an iocp, such that overlapped IO on the
   1.164 +    fd will happen on one of the iocp's worker threads.
   1.165 +*/
   1.166 +int event_iocp_port_associate(struct event_iocp_port *port, evutil_socket_t fd,
   1.167 +    ev_uintptr_t key);
   1.168 +
   1.169 +/** Tell all threads serving an iocp to stop.  Wait for up to waitMsec for all
   1.170 +    the threads to finish whatever they're doing.  If waitMsec is -1, wait
   1.171 +    as long as required.  If all the threads are done, free the port and return
   1.172 +    0. Otherwise, return -1.  If you get a -1 return value, it is safe to call
   1.173 +    this function again.
   1.174 +*/
   1.175 +int event_iocp_shutdown(struct event_iocp_port *port, long waitMsec);
   1.176 +
   1.177 +/* FIXME document. */
   1.178 +int event_iocp_activate_overlapped(struct event_iocp_port *port,
   1.179 +    struct event_overlapped *o,
   1.180 +    ev_uintptr_t key, ev_uint32_t n_bytes);
   1.181 +
   1.182 +struct event_base;
   1.183 +/* FIXME document. */
   1.184 +struct event_iocp_port *event_base_get_iocp(struct event_base *base);
   1.185 +
   1.186 +/* FIXME document. */
   1.187 +int event_base_start_iocp(struct event_base *base, int n_cpus);
   1.188 +void event_base_stop_iocp(struct event_base *base);
   1.189 +
   1.190 +/* FIXME document. */
   1.191 +struct bufferevent *bufferevent_async_new(struct event_base *base,
   1.192 +    evutil_socket_t fd, int options);
   1.193 +
   1.194 +/* FIXME document. */
   1.195 +void bufferevent_async_set_connected(struct bufferevent *bev);
   1.196 +int bufferevent_async_can_connect(struct bufferevent *bev);
   1.197 +int bufferevent_async_connect(struct bufferevent *bev, evutil_socket_t fd,
   1.198 +	const struct sockaddr *sa, int socklen);
   1.199 +
   1.200 +#ifdef __cplusplus
   1.201 +}
   1.202 +#endif
   1.203 +
   1.204 +#endif

mercurial