summaryrefslogblamecommitdiffstats
path: root/epoll.c
blob: ce721f86e8e687e02a1afc37ce0220df083865e3 (plain) (tree)
1
2
3

                  
                   



















                                                                                
                                                                                                                                                                









                                                     
                                        









                                                                             
#include "epoll.h"
#include <errno.h>
#include <string.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 | EPOLLRDHUP));
		}
		return 0;
}

/**
 * Returns -1 on error, just like epoll_ctl
 */
int ePoll_add(uint32_t flags, epoll_item_t* callback)
{
	struct epoll_event event;
	memset(&event, 0, sizeof 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);
}