init: move global initialization to init.c

This commit is contained in:
Bert Belder 2017-09-11 15:18:12 +02:00
parent c26cb3e2dd
commit 184ba5b0e0
5 changed files with 56 additions and 27 deletions

View File

@ -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) {

34
src/init.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdbool.h>
#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();
}

8
src/init.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef EPOLL_INIT_H_
#define EPOLL_INIT_H_
#include "internal.h"
EPOLL_INTERNAL int init(void);
#endif /* EPOLL_INIT_H_ */

View File

@ -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");

View File

@ -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 {