commit e0d665c8f6b8652b5c621f7a3c4eadf80502d608
parent ab4f32e0695582621f14bb891137246c6ff88f88
Author: Yersa Nordman <yersa@finwo.nl>
Date: Sat, 30 Dec 2023 23:28:54 +0100
fpoll_wait implementation
Diffstat:
2 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/src/fpoll.c b/src/fpoll.c
@@ -2,6 +2,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/poll.h>
#include "fpoll.h"
@@ -23,10 +24,43 @@ FPOLL_STATUS fpoll_close(struct fpoll *descriptor) {
}
int fpoll_wait(struct fpoll *descriptor, struct fpoll_ev *evs, int max_evs, int timeout) {
+ int i, c;
+
+ // Fetch new events
+ if (!descriptor->remaining) {
+ descriptor->remaining = poll(descriptor->fds, descriptor->size, timeout);
+ }
+
+ // Handle remaining events, possibly from previous run
+ if (descriptor->remaining) {
+ c = 0;
+ for( i = 0 ; (i < descriptor->size) && (c < max_evs) ; i++ ) {
+ if (!(descriptor->fds[i].revents)) continue;
+
+ // Convert event to our standard
+ evs[c].fd = descriptor->fds[i].fd;
+ evs[c].ev = descriptor->fds[i].revents;
+
+ // Reset the origin
+ descriptor->fds[i].revents = 0;
+
+ // Iterate to the next
+ descriptor->remaining -= 1;
+ c++;
+ }
+
+ // Prevent deadlocks
+ if (i == descriptor->size) {
+ descriptor->remaining = 0;
+ }
+
+ return c;
+ }
+
return 0;
}
-FPOLL_STATUS fpoll_add(struct fpoll *descriptor, FPOLL_EVENT events, FPOLL_FD filedescriptor) {
+FPOLL_STATUS fpoll_add(struct fpoll *descriptor, FPOLL_EVENT events, FPOLL_FD filedescriptor, void *udata) {
// Initial alloc
if (!descriptor->fds) {
@@ -58,8 +92,7 @@ FPOLL_STATUS fpoll_add(struct fpoll *descriptor, FPOLL_EVENT events, FPOLL_FD fi
}
// Assign the correct events
- if (events & FPOLL_IN) pfd->events |= POLLIN;
- if (events & FPOLL_OUT) pfd->events |= POLLOUT;
+ pfd->events = events;
return FPOLL_STATUS_OK;
}
diff --git a/src/fpoll.h b/src/fpoll.h
@@ -1,6 +1,8 @@
#ifndef __FINWO_POLL_H__
#define __FINWO_POLL_H__
+#include <stdbool.h>
+
#include <poll.h>
#ifndef FPOLL_STATUS
@@ -12,9 +14,9 @@
#ifndef FPOLL_EVENT
#define FPOLL_EVENT int
#endif
-#define FPOLL_IN 1
-#define FPOLL_OUT 2
-#define FPOLL_HUP 4
+#define FPOLL_IN POLLIN
+#define FPOLL_OUT POLLOUT
+#define FPOLL_HUP POLLHUP
#ifndef FPOLL_FD
#define FPOLL_FD int
@@ -24,17 +26,19 @@ struct fpoll {
struct pollfd *fds;
int size;
int limit;
+ int remaining;
};
struct fpoll_ev {
FPOLL_FD fd;
FPOLL_EVENT ev;
+ void *udata;
};
struct fpoll * fpoll_create();
FPOLL_STATUS fpoll_close(struct fpoll *);
int fpoll_wait(struct fpoll *, struct fpoll_ev *, int max_evs, int timeout);
-FPOLL_STATUS fpoll_add(struct fpoll *, FPOLL_EVENT, FPOLL_FD);
+FPOLL_STATUS fpoll_add(struct fpoll *, FPOLL_EVENT, FPOLL_FD, void *udata);
FPOLL_STATUS fpoll_del(struct fpoll *, FPOLL_EVENT, FPOLL_FD);
#endif // __FINWO_POLL_H__