port: move epoll_(create|close) public APIs to api.c

This commit is contained in:
Bert Belder 2017-09-11 17:14:43 +02:00
parent 6448bd2203
commit 02dceacbe3
3 changed files with 63 additions and 29 deletions

36
src/api.c Normal file
View File

@ -0,0 +1,36 @@
#include "epoll.h"
#include "error.h"
#include "init.h"
#include "port.h"
#include "win.h"
epoll_t epoll_create(void) {
ep_port_t* port_info;
HANDLE iocp;
if (init() < 0)
return NULL;
iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (iocp == INVALID_HANDLE_VALUE)
return_error(NULL);
port_info = ep_port_new(iocp);
if (port_info == NULL) {
CloseHandle(iocp);
return NULL;
}
return (epoll_t) port_info;
}
int epoll_close(epoll_t port_handle) {
ep_port_t* port_info;
if (init() < 0)
return -1;
port_info = (ep_port_t*) port_handle;
return ep_port_delete(port_info);
}

View File

@ -195,43 +195,38 @@ int epoll_wait(epoll_t port_handle,
return 0;
}
epoll_t epoll_create(void) {
ep_port_t* port_info;
HANDLE iocp;
if (init() < 0)
return NULL;
port_info = malloc(sizeof *port_info);
static ep_port_t* _ep_port_alloc(void) {
ep_port_t* port_info = malloc(sizeof *port_info);
if (port_info == NULL)
return_error(NULL, ERROR_NOT_ENOUGH_MEMORY);
iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (iocp == INVALID_HANDLE_VALUE) {
free(port_info);
return_error(NULL);
}
port_info->iocp = iocp;
port_info->poll_req_count = 0;
queue_init(&port_info->update_queue);
memset(&port_info->driver_sockets, 0, sizeof port_info->driver_sockets);
tree_init(&port_info->sock_tree);
return (epoll_t) port_info;
return port_info;
}
int epoll_close(epoll_t port_handle) {
static void _ep_port_free(ep_port_t* port) {
assert(port != NULL);
free(port);
}
ep_port_t* ep_port_new(HANDLE iocp) {
ep_port_t* port_info;
port_info = _ep_port_alloc();
if (port_info == NULL)
return NULL;
memset(port_info, 0, sizeof *port_info);
port_info->iocp = iocp;
queue_init(&port_info->update_queue);
tree_init(&port_info->sock_tree);
return port_info;
}
int ep_port_delete(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. */
for (size_t i = 0; i < array_count(port_info->driver_sockets); i++) {
SOCKET driver_socket = port_info->driver_sockets[i];

View File

@ -21,6 +21,9 @@ typedef struct ep_port {
size_t poll_req_count;
} ep_port_t;
EPOLL_INTERNAL ep_port_t* ep_port_new(HANDLE iocp);
EPOLL_INTERNAL int ep_port_delete(ep_port_t* port_info);
EPOLL_INTERNAL SOCKET ep_port_get_driver_socket(ep_port_t* port_info,
SOCKET socket);