tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

iocp-internal.h (7993B)


      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 
     27 #ifndef IOCP_INTERNAL_H_INCLUDED_
     28 #define IOCP_INTERNAL_H_INCLUDED_
     29 
     30 #ifdef __cplusplus
     31 extern "C" {
     32 #endif
     33 
     34 struct event_overlapped;
     35 struct event_iocp_port;
     36 struct evbuffer;
     37 typedef void (*iocp_callback)(struct event_overlapped *, ev_uintptr_t, ev_ssize_t, int success);
     38 
     39 /* This whole file is actually win32 only. We wrap the structures in a win32
     40 * ifdef so that we can test-compile code that uses these interfaces on
     41 * non-win32 platforms. */
     42 #ifdef _WIN32
     43 
     44 /**
     45   Internal use only.  Wraps an OVERLAPPED that we're using for libevent
     46   functionality.  Whenever an event_iocp_port gets an event for a given
     47   OVERLAPPED*, it upcasts the pointer to an event_overlapped, and calls the
     48   iocp_callback function with the event_overlapped, the iocp key, and the
     49   number of bytes transferred as arguments.
     50 */
     51 struct event_overlapped {
     52 OVERLAPPED overlapped;
     53 iocp_callback cb;
     54 };
     55 
     56 /* Mingw's headers don't define LPFN_ACCEPTEX. */
     57 
     58 typedef BOOL (WINAPI *AcceptExPtr)(SOCKET, SOCKET, PVOID, DWORD, DWORD, DWORD, LPDWORD, LPOVERLAPPED);
     59 typedef BOOL (WINAPI *ConnectExPtr)(SOCKET, const struct sockaddr *, int, PVOID, DWORD, LPDWORD, LPOVERLAPPED);
     60 typedef void (WINAPI *GetAcceptExSockaddrsPtr)(PVOID, DWORD, DWORD, DWORD, LPSOCKADDR *, LPINT, LPSOCKADDR *, LPINT);
     61 
     62 /** Internal use only. Holds pointers to functions that only some versions of
     63    Windows provide.
     64 */
     65 struct win32_extension_fns {
     66 AcceptExPtr AcceptEx;
     67 ConnectExPtr ConnectEx;
     68 GetAcceptExSockaddrsPtr GetAcceptExSockaddrs;
     69 };
     70 
     71 /**
     72    Internal use only. Stores a Windows IO Completion port, along with
     73    related data.
     74 */
     75 struct event_iocp_port {
     76 /** The port itself */
     77 HANDLE port;
     78 /* A lock to cover internal structures. */
     79 CRITICAL_SECTION lock;
     80 /** Number of threads ever open on the port. */
     81 short n_threads;
     82 /** True iff we're shutting down all the threads on this port */
     83 short shutdown;
     84 /** How often the threads on this port check for shutdown and other
     85  * conditions */
     86 long ms;
     87 /* The threads that are waiting for events. */
     88 HANDLE *threads;
     89 /** Number of threads currently open on this port. */
     90 short n_live_threads;
     91 /** A semaphore to signal when we are done shutting down. */
     92 HANDLE *shutdownSemaphore;
     93 };
     94 
     95 EVENT2_EXPORT_SYMBOL
     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 EVENT2_EXPORT_SYMBOL
    111 void event_overlapped_init_(struct event_overlapped *, iocp_callback cb);
    112 
    113 /** Allocate and return a new evbuffer that supports overlapped IO on a given
    114    socket.  The socket must be associated with an IO completion port using
    115    event_iocp_port_associate_.
    116 */
    117 EVENT2_EXPORT_SYMBOL
    118 struct evbuffer *evbuffer_overlapped_new_(evutil_socket_t fd);
    119 
    120 /** XXXX Document (nickm) */
    121 evutil_socket_t evbuffer_overlapped_get_fd_(struct evbuffer *buf);
    122 
    123 void evbuffer_overlapped_set_fd_(struct evbuffer *buf, evutil_socket_t fd);
    124 
    125 /** Start reading data onto the end of an overlapped evbuffer.
    126 
    127    An evbuffer can only have one read pending at a time.  While the read
    128    is in progress, no other data may be added to the end of the buffer.
    129    The buffer must be created with event_overlapped_init_().
    130    evbuffer_commit_read_() must be called in the completion callback.
    131 
    132    @param buf The buffer to read onto
    133    @param n The number of bytes to try to read.
    134    @param ol Overlapped object with associated completion callback.
    135    @return 0 on success, -1 on error.
    136 */
    137 EVENT2_EXPORT_SYMBOL
    138 int evbuffer_launch_read_(struct evbuffer *buf, size_t n, struct event_overlapped *ol);
    139 
    140 /** Start writing data from the start of an evbuffer.
    141 
    142    An evbuffer can only have one write pending at a time.  While the write is
    143    in progress, no other data may be removed from the front of the buffer.
    144    The buffer must be created with event_overlapped_init_().
    145    evbuffer_commit_write_() must be called in the completion callback.
    146 
    147    @param buf The buffer to read onto
    148    @param n The number of bytes to try to read.
    149    @param ol Overlapped object with associated completion callback.
    150    @return 0 on success, -1 on error.
    151 */
    152 EVENT2_EXPORT_SYMBOL
    153 int evbuffer_launch_write_(struct evbuffer *buf, ev_ssize_t n, struct event_overlapped *ol);
    154 
    155 /** XXX document */
    156 EVENT2_EXPORT_SYMBOL
    157 void evbuffer_commit_read_(struct evbuffer *, ev_ssize_t);
    158 EVENT2_EXPORT_SYMBOL
    159 void evbuffer_commit_write_(struct evbuffer *, ev_ssize_t);
    160 
    161 /** Create an IOCP, and launch its worker threads.  Internal use only.
    162 
    163    This interface is unstable, and will change.
    164 */
    165 EVENT2_EXPORT_SYMBOL
    166 struct event_iocp_port *event_iocp_port_launch_(int n_cpus);
    167 
    168 /** Associate a file descriptor with an iocp, such that overlapped IO on the
    169    fd will happen on one of the iocp's worker threads.
    170 */
    171 EVENT2_EXPORT_SYMBOL
    172 int event_iocp_port_associate_(struct event_iocp_port *port, evutil_socket_t fd,
    173    ev_uintptr_t key);
    174 
    175 /** Tell all threads serving an iocp to stop.  Wait for up to waitMsec for all
    176    the threads to finish whatever they're doing.  If waitMsec is -1, wait
    177    as long as required.  If all the threads are done, free the port and return
    178    0. Otherwise, return -1.  If you get a -1 return value, it is safe to call
    179    this function again.
    180 */
    181 EVENT2_EXPORT_SYMBOL
    182 int event_iocp_shutdown_(struct event_iocp_port *port, long waitMsec);
    183 
    184 /* FIXME document. */
    185 EVENT2_EXPORT_SYMBOL
    186 int event_iocp_activate_overlapped_(struct event_iocp_port *port,
    187    struct event_overlapped *o,
    188    ev_uintptr_t key, ev_uint32_t n_bytes);
    189 
    190 struct event_base;
    191 /* FIXME document. */
    192 EVENT2_EXPORT_SYMBOL
    193 struct event_iocp_port *event_base_get_iocp_(struct event_base *base);
    194 
    195 /* FIXME document. */
    196 EVENT2_EXPORT_SYMBOL
    197 int event_base_start_iocp_(struct event_base *base, int n_cpus);
    198 void event_base_stop_iocp_(struct event_base *base);
    199 
    200 /* FIXME document. */
    201 EVENT2_EXPORT_SYMBOL
    202 struct bufferevent *bufferevent_async_new_(struct event_base *base,
    203    evutil_socket_t fd, int options);
    204 
    205 /* FIXME document. */
    206 void bufferevent_async_set_connected_(struct bufferevent *bev);
    207 int bufferevent_async_can_connect_(struct bufferevent *bev);
    208 int bufferevent_async_connect_(struct bufferevent *bev, evutil_socket_t fd,
    209 const struct sockaddr *sa, int socklen);
    210 
    211 #ifdef __cplusplus
    212 }
    213 #endif
    214 
    215 #endif