init: move global initialization to init.c
This commit is contained in:
parent
c26cb3e2dd
commit
184ba5b0e0
37
src/epoll.c
37
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) {
|
||||
|
||||
34
src/init.c
Normal file
34
src/init.c
Normal 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
8
src/init.h
Normal 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_ */
|
||||
2
src/nt.c
2
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");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user