diff --git a/src/iomux_epoll.c b/src/iomux_epoll.c index 7dfc343..586b5f0 100644 --- a/src/iomux_epoll.c +++ b/src/iomux_epoll.c @@ -213,6 +213,14 @@ static void iomux_epoll_delete(struct IOMux *mux) mux->instance = NULL; } } + +static const struct IOMuxOps epoll_ops = { + .run_impl = iomux_epoll_run, + .delete_impl = iomux_epoll_delete, + .register_fd_impl = iomux_epoll_register_fd, + .unregister_fd_impl = iomux_epoll_unregister_fd, + .update_events_impl = iomux_epoll_update_events, +}; int iomux_epoll_init(struct IOMux *mux) { @@ -226,12 +234,8 @@ int iomux_epoll_init(struct IOMux *mux) return errno; } - mux->instance = mux_epoll; - mux->run_impl = iomux_epoll_run; - mux->delete_impl = iomux_epoll_delete; - mux->register_fd_impl = iomux_epoll_register_fd; - mux->unregister_fd_impl = iomux_epoll_unregister_fd; - mux->update_events_impl = iomux_epoll_update_events; + mux->instance = mux_epoll; + mux->ops = epoll_ops; return 0; } diff --git a/src/iomux_impl.c b/src/iomux_impl.c index d4b14ed..a1e52c2 100644 --- a/src/iomux_impl.c +++ b/src/iomux_impl.c @@ -48,7 +48,7 @@ struct IOMux *iomux_create_ex(enum IOMUX_TYPE type, struct UCoreClock *uclock) void iomux_delete(struct IOMux *mux) { - mux->delete_impl(mux); + mux->ops.delete_impl(mux); memset(mux, 0xfa, sizeof *mux); free(mux); } @@ -74,17 +74,18 @@ static int iomux_run_timers(struct IOMux *mux, struct timeval *timeout) struct timeval now; mux->uclock->now(mux->uclock, &now); future_to_interval(&first_timer, &now, timeout); - /* TRACEF("now %d %d future %d %d interval %d %d\n", mux->now.tv_sec, mux->now.tv_usec, + /*TRACEF("now %d %d future %d %d interval %d %d\n", now.tv_sec, now.tv_usec, first_timer.tv_sec, first_timer.tv_usec, - timeout.tv_sec, timeout.tv_usec); - */ + timeout->tv_sec, timeout->tv_usec); + */ + assert(timeout->tv_sec >= 0); assert(timeout->tv_usec >= 0); return 1; } - return 9; + return 0; } int iomux_run(struct IOMux *mux) @@ -95,12 +96,13 @@ int iomux_run(struct IOMux *mux) struct timeval *timeoutp; if (iomux_run_timers(mux, &timeout)) { + TRACEF("NULL\n"); timeoutp = &timeout; } else { timeoutp = NULL; } - rc = mux->run_impl(mux, timeoutp); + rc = mux->ops.run_impl(mux, timeoutp); if (rc < 0) return rc; @@ -128,7 +130,7 @@ int iomux_register_fd(struct IOMux *mux, struct IOMuxFD *fd) assert(fd != NULL); assert(fd->callback != NULL); assert(fd->fd >= 0); - return mux->register_fd_impl(mux, fd); + return mux->ops.register_fd_impl(mux, fd); } int iomux_unregister_fd(struct IOMux *mux, struct IOMuxFD *fd) @@ -136,7 +138,7 @@ int iomux_unregister_fd(struct IOMux *mux, struct IOMuxFD *fd) assert(mux != NULL); assert(fd != NULL); assert(fd->fd >= 0); - return mux->unregister_fd_impl(mux, fd); + return mux->ops.unregister_fd_impl(mux, fd); } int iomux_update_events(struct IOMux *mux, struct IOMuxFD *fd, unsigned int what) @@ -148,7 +150,7 @@ int iomux_update_events(struct IOMux *mux, struct IOMuxFD *fd, unsigned int what assert(fd->fd >= 0); if (fd->what != what) { - mux->update_events_impl(mux, fd); + mux->ops.update_events_impl(mux, fd); } else { rc = 0; } diff --git a/src/iomux_impl.h b/src/iomux_impl.h index 6c5484a..31944cb 100644 --- a/src/iomux_impl.h +++ b/src/iomux_impl.h @@ -4,21 +4,7 @@ #include "ucore/iomux.h" #include "ucore/timers.h" /** struct for IOMux implementations */ -struct IOMux { - - /* TImer instance */ - struct UCTimers timers; - - /** - * Clock used for driving timers, and - * providing timeouts for the mux - */ - struct UCoreClock *uclock; - - - /* Used by mux implementations to hold their own data/instance */ - void *instance; - +struct IOMuxOps { /** Run one iteration of the mux. * The mux implementation must honor the timeout, if given. * The mux implementation must call iomux_timers_run after the @@ -39,6 +25,26 @@ struct IOMux { int (*unregister_fd_impl)(struct IOMux *mux, struct IOMuxFD *fd); /** Update an IOMuxFD (due to its events ('what') has changed*/ int (*update_events_impl)(struct IOMux *mux, struct IOMuxFD *fd); + +}; +struct IOMux { + + /* TImer instance */ + struct UCTimers timers; + + /** + * Clock used for driving timers, and + * providing timeouts for the mux + */ + struct UCoreClock *uclock; + + struct IOMuxOps ops; + + + /* Used by mux implementations to hold their own data/instance */ + void *instance; + + }; int iomux_select_init(struct IOMux *mux); diff --git a/src/iomux_select.c b/src/iomux_select.c index 40156de..f680d60 100644 --- a/src/iomux_select.c +++ b/src/iomux_select.c @@ -199,6 +199,14 @@ static void iomux_select_delete(struct IOMux *mux) mux->instance = NULL; } } + +static const struct IOMuxOps select_ops = { + .run_impl = iomux_select_run, + .delete_impl = iomux_select_delete, + .register_fd_impl = iomux_select_register_fd, + .unregister_fd_impl = iomux_select_unregister_fd, + .update_events_impl = iomux_select_update_events, +}; int iomux_select_init(struct IOMux *mux) { @@ -208,12 +216,8 @@ int iomux_select_init(struct IOMux *mux) mux_select->max_fd = -1; - mux->instance = mux_select; - mux->run_impl = iomux_select_run; - mux->delete_impl = iomux_select_delete; - mux->register_fd_impl = iomux_select_register_fd; - mux->unregister_fd_impl = iomux_select_unregister_fd; - mux->update_events_impl = iomux_select_update_events; + mux->instance = mux_select; + mux->ops = select_ops; return 0; }