add iom_requeue

master
leitner 9 months ago
parent 7100af72ca
commit 0756884d41

@ -249,12 +249,16 @@ enum {
IOM_WRITE=2, IOM_WRITE=2,
IOM_ERROR=4 IOM_ERROR=4
}; };
/* signal interest in events from a socket */
/* return -1 if error, events can be IOM_READ or IOM_WRITE */ /* return -1 if error, events can be IOM_READ or IOM_WRITE */
int iom_add(iomux_t* c,int64 s,unsigned int events); int iom_add(iomux_t* c,int64 s,unsigned int events);
/* signal interest in more events from a socket */
int iom_requeue(iomux_t* c,int64 s,unsigned int events);
/* Blocking wait for single event, timeout in milliseconds */ /* Blocking wait for single event, timeout in milliseconds */
/* return -1 if error, 0 if ok; s set to fd, revents set to known events on that fd */ /* return -1 if error, 0 if ok; s set to fd, revents set to known events on that fd */
/* when done with the fd, call iom_add on it again! */ /* when done with the fd, call iom_requeue on it to receive more events! */
/* This can be called by multiple threads in parallel */ /* This can be called by multiple threads in parallel */
int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout); int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout);

@ -0,0 +1,28 @@
#include "io_internal.h"
#ifdef HAVE_EPOLL
#include <sys/epoll.h>
#endif
#ifdef HAVE_KQUEUE
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#endif
int iom_requeue(iomux_t* c,int64 s,unsigned int events) {
#ifdef HAVE_EPOLL
struct epoll_event e = { .events=EPOLLONESHOT, .data.fd=s };
if (events & IOM_READ) e.events|=EPOLLIN;
if (events & IOM_WRITE) e.events|=EPOLLOUT;
return epoll_ctl(c->ctx, EPOLL_CTL_MOD, s, &e);
#elif defined(HAVE_KQUEUE)
struct kevent kev;
struct timespec ts = { 0 };
EV_SET(&kev, s,
(events & IOM_READ ? EVFILT_READ : 0) +
(events & IOM_WRITE ? EVFILT_WRITE : 0),
EV_ADD | EV_ENABLE | EV_ONESHOT, 0, 0, (void*)s);
return kevent(c->ctx, &kev, 1, 0, 0, &ts);
#else
#warning "only epoll and kqueue supported for now"
#endif
}
Loading…
Cancel
Save