From 879420a711453a67c0421968e9592b6cc9f0aae2 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 23 Oct 2009 22:00:29 +0000 Subject: [PATCH] Expose a narrow window to the IOCP code. svn:r1459 --- bufferevent_async.c | 7 +------ bufferevent_sock.c | 8 ++++++++ event.c | 23 +++++++++++++++++++++++ include/event2/event.h | 4 +++- iocp-internal.h | 9 +++++++++ test/regress_iocp.c | 12 +++--------- 6 files changed, 47 insertions(+), 16 deletions(-) diff --git a/bufferevent_async.c b/bufferevent_async.c index e8851234..0399fc88 100644 --- a/bufferevent_async.c +++ b/bufferevent_async.c @@ -245,14 +245,9 @@ be_async_flush(struct bufferevent *bev, short what, return 0; } -/*FIXME export this */ struct bufferevent * bufferevent_async_new(struct event_base *base, - evutil_socket_t fd, enum bufferevent_options options); - -struct bufferevent * -bufferevent_async_new(struct event_base *base, - evutil_socket_t fd, enum bufferevent_options options) + evutil_socket_t fd, int options) { struct bufferevent_async *bev_a; struct bufferevent *bev; diff --git a/bufferevent_sock.c b/bufferevent_sock.c index 2f591bd2..5dec03f8 100644 --- a/bufferevent_sock.c +++ b/bufferevent_sock.c @@ -66,6 +66,9 @@ #include "mm-internal.h" #include "bufferevent-internal.h" #include "util-internal.h" +#ifdef WIN32 +#include "iocp-internal.h" +#endif /* prototypes */ static int be_socket_enable(struct bufferevent *, short); @@ -266,6 +269,11 @@ bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, struct bufferevent_private *bufev_p; struct bufferevent *bufev; +#ifdef WIN32 + if (base && base->iocp) + return bufferevent_async_new(base, fd, options); +#endif + if ((bufev_p = mm_calloc(1, sizeof(struct bufferevent_private)))== NULL) return NULL; diff --git a/event.c b/event.c index a6128121..7078a0a5 100644 --- a/event.c +++ b/event.c @@ -71,6 +71,7 @@ #include "event2/util.h" #include "log-internal.h" #include "evmap-internal.h" +#include "iocp-internal.h" #ifdef _EVENT_HAVE_EVENT_PORTS extern const struct eventop evportops; @@ -340,9 +341,31 @@ event_base_new_with_config(struct event_config *cfg) } } +#ifdef WIN32 + if ((cfg->flags & EVENT_BASE_FLAG_STARTUP_IOCP) != 0) + event_base_start_iocp(base); +#endif + return (base); } +int +event_base_start_iocp(struct event_base *base) +{ +#ifdef WIN32 + if (base->iocp) + return 0; + base->iocp = event_iocp_port_launch(); + if (!base->iocp) { + event_warnx("%s: Couldn't launch IOCP", __func__); + return -1; + } + return 0; +#else + return -1; +#endif +} + void event_base_free(struct event_base *base) { diff --git a/include/event2/event.h b/include/event2/event.h index 1a96260d..1e7026d2 100644 --- a/include/event2/event.h +++ b/include/event2/event.h @@ -165,7 +165,9 @@ enum event_base_config_flag { EVENT_BASE_FLAG_NOLOCK = 0x01, /** Do not check the EVENT_NO* environment variables when picking an event_base. */ - EVENT_BASE_FLAG_IGNORE_ENV = 0x02 + EVENT_BASE_FLAG_IGNORE_ENV = 0x02, + /** Windows only: enable the IOCP dispatcher at startup */ + EVENT_BASE_FLAG_STARTUP_IOCP = 0x04 }; /** diff --git a/iocp-internal.h b/iocp-internal.h index f03409f1..d743bd54 100644 --- a/iocp-internal.h +++ b/iocp-internal.h @@ -149,8 +149,17 @@ int event_iocp_activate_overlapped(struct event_iocp_port *port, uintptr_t key, ev_uint32_t n_bytes); struct event_base; +/* FIXME document. */ struct event_iocp_port *event_base_get_iocp(struct event_base *base); +/* FIXME document. */ +int event_base_start_iocp(struct event_base *base); + +/* FIXME document. */ +struct bufferevent *bufferevent_async_new(struct event_base *base, + evutil_socket_t fd, int options); + + #ifdef __cplusplus } #endif diff --git a/test/regress_iocp.c b/test/regress_iocp.c index d103c710..51953cbe 100644 --- a/test/regress_iocp.c +++ b/test/regress_iocp.c @@ -186,8 +186,7 @@ test_iocp_evbuffer(void *ptr) #endif tt_int_op(evbuffer_get_length(rbuf),==,512); - - + /* FIXME Actually test some stuff here. */ tt_want(!event_iocp_shutdown(port, 2000)); @@ -216,15 +215,10 @@ test_iocp_bufferevent_async(void *ptr) evthread_use_windows_threads(); #endif - port = event_iocp_port_launch(); + event_base_start_iocp(data->base); + port = event_base_get_iocp(data->base); tt_assert(port); - -#ifdef WIN32 - /* FIXME set this indirectly once there is an interface to do that. */ - data->base->iocp = port; -#endif - bea1 = bufferevent_async_new(data->base, data->pair[0], BEV_OPT_DEFER_CALLBACKS); bea2 = bufferevent_async_new(data->base, data->pair[1],