summaryrefslogblamecommitdiffstats
path: root/epoll.c
blob: 576fb0e46f07d74a3296545c892bad8b45d093e8 (plain) (tree)










































                                                                                                                                                   
#include "epoll.h"
#include <errno.h>

static int efd = -1;
#define MAXEVENTS 10
static struct epoll_event events[MAXEVENTS];

int ePoll_init()
{
	if (efd != -1) return 0;
	efd = epoll_create1(0);
	if (efd == -1) return -1;
	return 0;
}

int ePoll_wait(const int timeoutMs)
{
		const int num = epoll_wait(efd, events, MAXEVENTS, timeoutMs);
		if (num < 0 && (errno == EINTR || errno == EAGAIN)) return 0;
		if (num < 0) return -1;
		for (int i = 0; i < num; ++i) {
			epoll_item_t *data = (epoll_item_t *)events[i].data.ptr;
			(*data->callback)(data, events[i].events & EPOLLIN, events[i].events & EPOLLOUT, events[i].events & (EPOLLERR | EPOLLHUP));
		}
		return 0;
}

/**
 * Returns -1 on error, just like epoll_ctl
 */
int ePoll_add(uint32_t flags, epoll_item_t* callback)
{
	struct epoll_event event;
	event.data.ptr = (void*)callback;
	event.events = flags;
	return epoll_ctl(efd, EPOLL_CTL_ADD, callback->fd, &event);
}

int ePoll_remove(int fd)
{
	return epoll_ctl(efd, EPOLL_CTL_DEL, fd, (struct epoll_event*)67775);
}