might actually work now

master
leitner 4 years ago
parent 75fe9a620e
commit d4ac8fe906

@ -9,39 +9,62 @@
#endif #endif
#include <errno.h> #include <errno.h>
// return the next event, waiting of none are queued
// wait at most timeout milliseconds
// on success, return 1 and return the fd in *s and the events on it in *revents
// if we waited but ran into a timeout, return 0
// if we run into a system error, return -1
// if another thread aborted this iomux, return -2
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) {
for (;;) { for (;;) {
/* If we have an event in the queue, use that one */ /* If we have an event in the queue, use that one */
int r; int r;
if (c->working==-2) return -2; /* iomux was aborted */ if (c->working==-2) return -2; /* iomux was aborted */
for (;;) {
for (;;) { // CAS-loop get the first element from the queue
unsigned int f=c->l; unsigned int f=c->l;
if (f == c->h) if (f == c->h)
break; /* no elements in queue */ break; /* no elements in queue */
int n=(f+1)%SLOTS; int n=(f+1)%SLOTS;
if (__sync_bool_compare_and_swap(&c->l,f,n)) {
/* we got one, and its index is in f */
*s=c->q[f].fd; *s=c->q[f].fd;
*revents=c->q[f].events; *revents=c->q[f].events;
if (__sync_bool_compare_and_swap(&c->l,f,n)) {
/* we got one */
return 1;
} }
/* collided with another thread, try again */ /* collided with another thread, try again */
} }
/* The queue was empty. If someone else is already calling /* The queue was empty. If someone else is already calling
* epoll_wait/kevent, then use the semaphore */ * epoll_wait/kevent, then use the semaphore */
if (__sync_bool_compare_and_swap(&c->working,0,1)) { if (__sync_bool_compare_and_swap(&c->working,0,1)) {
/* race avoidance: another thread could have come in between and
* refilled the job queue */
if (c->h != c->l) {
/* set working back to 0 unless someone set it to -2 in the mean time (iom_abort) */
if (__sync_val_compare_and_swap(&c->working,1,0)==-2) return -2;
continue;
}
/* we have the job to fill the struct. */ /* we have the job to fill the struct. */
int freeslots = (c->h - c->l);
if (!freeslots) freeslots=SLOTS;
#ifdef HAVE_EPOLL #ifdef HAVE_EPOLL
struct epoll_event ee[SLOTS]; struct epoll_event ee[SLOTS];
int i; int i;
r=epoll_wait(c->ctx, ee, freeslots, timeout); r=epoll_wait(c->ctx, ee, SLOTS, timeout);
if (r <= 0) { if (r <= 0) {
/* we ran into a timeout, so let someone else take over */ /* epoll_wait returned a timeout or an error! */
/* relinquish the lock and return 0 / -1 */
if (__sync_val_compare_and_swap(&c->working,1,0)==-2) return -2; if (__sync_val_compare_and_swap(&c->working,1,0)==-2) return -2;
#ifdef __dietlibc__ #ifdef __dietlibc__
cnd_broadcast(&c->sem); // for timeout we want to hand off to one other thread, no need
// to wake them all up. Error might be transient (EINTR) and the
// next guy might succeed, so only wake one up. If the error was
// not transient, then they will also get an error and wake the
// next up
cnd_signal(&c->sem);
#else #else
sem_post(&c->sem); sem_post(&c->sem);
#endif #endif
@ -56,7 +79,10 @@ int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout) {
/* return last event instead of enqueueing it */ /* return last event instead of enqueueing it */
*s=ee[i].data.fd; *s=ee[i].data.fd;
*revents=e; *revents=e;
/* loop terminates here, but no return statement because we
* still need to signal the semaphore below */
} else { } else {
/* The CAS loop on c->working above ensures we are the only one writing to c->h */
c->q[c->h].fd=ee[i].data.fd; c->q[c->h].fd=ee[i].data.fd;
c->q[c->h].events=e; c->q[c->h].events=e;
c->h = (c->h + 1) % SLOTS; c->h = (c->h + 1) % SLOTS;
@ -65,12 +91,14 @@ int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout) {
#elif defined(HAVE_KQUEUE) #elif defined(HAVE_KQUEUE)
struct kevent kev[SLOTS]; struct kevent kev[SLOTS];
struct timespec ts = { .tv_sec=timeout/1000, .tv_nsec=(timeout%1000)*1000000 }; struct timespec ts = { .tv_sec=timeout/1000, .tv_nsec=(timeout%1000)*1000000 };
int r=kevent(c->ctx, 0, 0, kev, freeslots, &ts); int r=kevent(c->ctx, 0, 0, kev, SLOTS, &ts);
int i; int i;
if (r<=0) { if (r<=0) {
/* we ran into a timeout, so let someone else take over */ /* kevent returned a timeout or an error! */
/* relinquish the lock and return 0 / -1 */
if (__sync_val_compare_and_swap(&c->working,1,0)==-2) return -2; if (__sync_val_compare_and_swap(&c->working,1,0)==-2) return -2;
#ifdef __dietlibc__ #ifdef __dietlibc__
// no dietlibc for kqueue based systems yet
cnd_broadcast(&c->sem); cnd_broadcast(&c->sem);
#else #else
sem_post(&c->sem); sem_post(&c->sem);
@ -85,7 +113,10 @@ int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout) {
/* return last event instead of enqueueing it */ /* return last event instead of enqueueing it */
*s=kev[i].ident; *s=kev[i].ident;
*revents=e; *revents=e;
/* loop terminates here, but no return statement because we
* still need to signal the semaphore below */
} else { } else {
/* The CAS loop on c->working above ensures we are the only one writing to c->h */
c->q[c->h].fd=kev[i].ident; c->q[c->h].fd=kev[i].ident;
c->q[c->h].events=e; c->q[c->h].events=e;
c->h = (c->h + 1) % SLOTS; c->h = (c->h + 1) % SLOTS;
@ -100,7 +131,10 @@ int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout) {
doing it anymore */ doing it anymore */
if (__sync_val_compare_and_swap(&c->working,1,0)==-2) return -2; if (__sync_val_compare_and_swap(&c->working,1,0)==-2) return -2;
#ifdef __dietlibc__ #ifdef __dietlibc__
if (c->h == (c->l + 1) % SLOTS)
cnd_signal(&c->sem); cnd_signal(&c->sem);
else
cnd_broadcast(&c->sem);
#else #else
sem_post(&c->sem); sem_post(&c->sem);
#endif #endif

Loading…
Cancel
Save