build on osx

This commit is contained in:
Nils O. Selåsdal
2025-06-27 23:29:09 +02:00
parent f0685abf62
commit a6a7b67d72
+23 -13
View File
@@ -1,12 +1,13 @@
#ifdef __linux__
#include <sys/epoll.h> #include <sys/epoll.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <errno.h>
#include "iomux_impl.h" #include "iomux_impl.h"
//epoll (linux specific) IO mux. //epoll (linux specific) IO mux.
//We stuff the struct IOMuxFD pointer in the event.data.ptr slot //We stuff the struct IOMuxFD pointer in the event.data.ptr slot
//for the kernel to keep track of, so we don't have to. //for the kernel to keep track of, so we don't have to.
///we do not use edge triggered mode ///we do not use edge triggered mode
@@ -29,10 +30,10 @@ static inline uint32_t mux_2_epoll_events(unsigned int events)
{ {
uint32_t epoll_events = 0; uint32_t epoll_events = 0;
if (events & MUX_EV_READ) if (events & MUX_EV_READ)
epoll_events |= EPOLLIN; epoll_events |= EPOLLIN;
if (events & MUX_EV_WRITE) if (events & MUX_EV_WRITE)
epoll_events |= EPOLLOUT; epoll_events |= EPOLLOUT;
return epoll_events; return epoll_events;
@@ -47,7 +48,7 @@ static inline unsigned int epoll_2_mux_events(uint32_t events)
if (events & (EPOLLIN | EPOLLHUP | EPOLLERR)) if (events & (EPOLLIN | EPOLLHUP | EPOLLERR))
mux_events |= MUX_EV_READ; mux_events |= MUX_EV_READ;
if (events & EPOLLOUT) if (events & EPOLLOUT)
mux_events |= MUX_EV_WRITE; mux_events |= MUX_EV_WRITE;
return mux_events; return mux_events;
@@ -58,10 +59,10 @@ static int iomux_epoll_update_events(struct IOMux *mux_, struct IOMuxFD *fd)
struct IOMuxEpoll *mux = mux_->instance; struct IOMuxEpoll *mux = mux_->instance;
struct epoll_event e_event = {0, {0}}; struct epoll_event e_event = {0, {0}};
int rc; int rc;
e_event.events = mux_2_epoll_events(fd->what); e_event.events = mux_2_epoll_events(fd->what);
e_event.data.ptr = fd; e_event.data.ptr = fd;
rc = epoll_ctl(mux->epoll_fd, EPOLL_CTL_MOD, fd->fd, &e_event); rc = epoll_ctl(mux->epoll_fd, EPOLL_CTL_MOD, fd->fd, &e_event);
assert(rc == 0); assert(rc == 0);
if (rc != 0) if (rc != 0)
@@ -117,7 +118,7 @@ static int iomux_epoll_unregister_fd(struct IOMux *mux_, struct IOMuxFD *fd)
break; break;
} }
} }
mux->num_descriptors--; mux->num_descriptors--;
return rc; return rc;
@@ -145,10 +146,10 @@ static int iomux_epoll_run(struct IOMux *mux_, struct timeval *timeout)
//epoll takes the timeout in miliseconds //epoll takes the timeout in miliseconds
if (timeout) if (timeout)
timeout_ms = timeval_to_ms(timeout); timeout_ms = timeval_to_ms(timeout);
else else
timeout_ms = -1; timeout_ms = -1;
do { do {
rc = epoll_wait(mux->epoll_fd, mux->pending_events, EPOLL_WAIT_EVENTS, timeout_ms); rc = epoll_wait(mux->epoll_fd, mux->pending_events, EPOLL_WAIT_EVENTS, timeout_ms);
@@ -173,14 +174,14 @@ static int iomux_epoll_run(struct IOMux *mux_, struct timeval *timeout)
//we want to deliver only one event at a time to simplify //we want to deliver only one event at a time to simplify
//application programming. //application programming.
if (events & MUX_EV_READ && fd != NULL) { if (events & MUX_EV_READ && fd != NULL) {
assert(fd->callback != NULL); assert(fd->callback != NULL);
fd->callback(mux_, fd, MUX_EV_READ); fd->callback(mux_, fd, MUX_EV_READ);
} }
//now re-check, in case the IOMuxFD was removed //now re-check, in case the IOMuxFD was removed
fd = mux->pending_events[i].data.ptr; fd = mux->pending_events[i].data.ptr;
if (events & MUX_EV_WRITE && fd != NULL) { if (events & MUX_EV_WRITE && fd != NULL) {
assert(fd->callback != NULL); assert(fd->callback != NULL);
fd->callback(mux_, fd, MUX_EV_WRITE); fd->callback(mux_, fd, MUX_EV_WRITE);
} }
@@ -211,7 +212,7 @@ static const struct IOMuxOps epoll_ops = {
.unregister_fd_impl = iomux_epoll_unregister_fd, .unregister_fd_impl = iomux_epoll_unregister_fd,
.update_events_impl = iomux_epoll_update_events, .update_events_impl = iomux_epoll_update_events,
}; };
int iomux_epoll_init(struct IOMux *mux) int iomux_epoll_init(struct IOMux *mux)
{ {
struct IOMuxEpoll *mux_epoll = calloc(1, sizeof *mux_epoll); struct IOMuxEpoll *mux_epoll = calloc(1, sizeof *mux_epoll);
@@ -230,3 +231,12 @@ int iomux_epoll_init(struct IOMux *mux)
return 0; return 0;
} }
#else
#include <errno.h>
#include "iomux_impl.h"
int iomux_epoll_init(struct IOMux *mux)
{
return ENOSYS;
}
#endif