From 184ba5b0e090a3d28964d240e072c2e32ab7176b Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Mon, 11 Sep 2017 15:18:12 +0200 Subject: [PATCH] init: move global initialization to init.c --- src/epoll.c | 37 ++++++++++++------------------------- src/init.c | 34 ++++++++++++++++++++++++++++++++++ src/init.h | 8 ++++++++ src/nt.c | 2 +- src/nt.h | 2 +- 5 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 src/init.c create mode 100644 src/init.h diff --git a/src/epoll.c b/src/epoll.c index d4e962f..574315e 100644 --- a/src/epoll.c +++ b/src/epoll.c @@ -9,6 +9,7 @@ #include "epoll-socket.h" #include "epoll.h" #include "error.h" +#include "init.h" #include "nt.h" #include "poll-request.h" #include "port.h" @@ -23,12 +24,9 @@ typedef struct ep_port ep_port_t; typedef struct poll_req poll_req_t; typedef struct ep_sock ep_sock_t; -static int _ep_initialize(void); static SOCKET _ep_create_driver_socket(HANDLE iocp, WSAPROTOCOL_INFOW* protocol_info); -static int _ep_initialized = 0; - static int _ep_ctl_add(ep_port_t* port_info, uintptr_t socket, struct epoll_event* ev) { @@ -75,6 +73,9 @@ int epoll_ctl(epoll_t port_handle, struct epoll_event* ev) { ep_port_t* port_info = (ep_port_t*) port_handle; + if (init() < 0) + return -1; + switch (op) { case EPOLL_CTL_ADD: return _ep_ctl_add(port_info, socket, ev); @@ -135,6 +136,9 @@ int epoll_wait(epoll_t port_handle, ULONGLONG due = 0; DWORD gqcs_timeout; + if (init() < 0) + return -1; + port_info = (ep_port_t*) port_handle; /* Compute the timeout for GetQueuedCompletionStatus, and the wait end @@ -196,14 +200,8 @@ epoll_t epoll_create(void) { ep_port_t* port_info; HANDLE iocp; - /* If necessary, do global initialization first. This is totally not - * thread-safe at the moment. - */ - if (!_ep_initialized) { - if (_ep_initialize() < 0) - return NULL; - _ep_initialized = 1; - } + if (init() < 0) + return NULL; port_info = malloc(sizeof *port_info); if (port_info == NULL) @@ -230,6 +228,9 @@ int epoll_close(epoll_t port_handle) { ep_port_t* port_info; tree_node_t* tree_node; + if (init() < 0) + return -1; + port_info = (ep_port_t*) port_handle; /* Close all peer sockets. This will make all pending io requests return. */ @@ -285,20 +286,6 @@ int epoll_close(epoll_t port_handle) { return 0; } -static int _ep_initialize(void) { - int r; - WSADATA wsa_data; - - r = WSAStartup(MAKEWORD(2, 2), &wsa_data); - if (r != 0) - return_error(-1); - - if (nt_initialize() < 0) - return -1; - - return 0; -} - int ep_port_add_socket(ep_port_t* port_info, tree_node_t* tree_node, SOCKET socket) { diff --git a/src/init.c b/src/init.c new file mode 100644 index 0000000..0d13743 --- /dev/null +++ b/src/init.c @@ -0,0 +1,34 @@ +#include + +#include "error.h" +#include "init.h" +#include "nt.h" +#include "win.h" + +static bool _initialized = false; + +static int _init_winsock(void) { + int r; + WSADATA wsa_data; + + r = WSAStartup(MAKEWORD(2, 2), &wsa_data); + if (r != 0) + return_error(-1); + + return 0; +} + +static int _init_once(void) { + if (_init_winsock() < 0 || nt_init() < 0) + return -1; + + _initialized = true; + return 0; +} + +int init(void) { + if (_initialized) + return 0; + + return _init_once(); +} diff --git a/src/init.h b/src/init.h new file mode 100644 index 0000000..8ea1a8d --- /dev/null +++ b/src/init.h @@ -0,0 +1,8 @@ +#ifndef EPOLL_INIT_H_ +#define EPOLL_INIT_H_ + +#include "internal.h" + +EPOLL_INTERNAL int init(void); + +#endif /* EPOLL_INIT_H_ */ diff --git a/src/nt.c b/src/nt.c index f2c3f24..cfcca48 100644 --- a/src/nt.c +++ b/src/nt.c @@ -8,7 +8,7 @@ NTDLL_IMPORT_LIST(X) #undef X -int nt_initialize(void) { +int nt_init(void) { HMODULE ntdll; ntdll = GetModuleHandleW(L"ntdll.dll"); diff --git a/src/nt.h b/src/nt.h index 23002f8..98845e1 100644 --- a/src/nt.h +++ b/src/nt.h @@ -5,7 +5,7 @@ #include "ntstatus.h" #include "win.h" -EPOLL_INTERNAL int nt_initialize(void); +EPOLL_INTERNAL int nt_init(void); typedef struct _IO_STATUS_BLOCK { union {