Implement generalied IOMux implementation operations

Fix serious bug  in iomix_timers_run
This commit is contained in:
Nils O. Selåsdal
2014-03-02 06:00:05 +01:00
parent 857c2018b9
commit a112b44c6e
4 changed files with 52 additions and 36 deletions
+9 -5
View File
@@ -214,6 +214,14 @@ static void iomux_epoll_delete(struct IOMux *mux)
} }
} }
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) 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);
@@ -227,11 +235,7 @@ int iomux_epoll_init(struct IOMux *mux)
} }
mux->instance = mux_epoll; mux->instance = mux_epoll;
mux->run_impl = iomux_epoll_run; mux->ops = epoll_ops;
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;
return 0; return 0;
} }
+10 -8
View File
@@ -48,7 +48,7 @@ struct IOMux *iomux_create_ex(enum IOMUX_TYPE type, struct UCoreClock *uclock)
void iomux_delete(struct IOMux *mux) void iomux_delete(struct IOMux *mux)
{ {
mux->delete_impl(mux); mux->ops.delete_impl(mux);
memset(mux, 0xfa, sizeof *mux); memset(mux, 0xfa, sizeof *mux);
free(mux); free(mux);
} }
@@ -74,17 +74,18 @@ static int iomux_run_timers(struct IOMux *mux, struct timeval *timeout)
struct timeval now; struct timeval now;
mux->uclock->now(mux->uclock, &now); mux->uclock->now(mux->uclock, &now);
future_to_interval(&first_timer, &now, timeout); 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, 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_sec >= 0);
assert(timeout->tv_usec >= 0); assert(timeout->tv_usec >= 0);
return 1; return 1;
} }
return 9; return 0;
} }
int iomux_run(struct IOMux *mux) int iomux_run(struct IOMux *mux)
@@ -95,12 +96,13 @@ int iomux_run(struct IOMux *mux)
struct timeval *timeoutp; struct timeval *timeoutp;
if (iomux_run_timers(mux, &timeout)) { if (iomux_run_timers(mux, &timeout)) {
TRACEF("NULL\n");
timeoutp = &timeout; timeoutp = &timeout;
} else { } else {
timeoutp = NULL; timeoutp = NULL;
} }
rc = mux->run_impl(mux, timeoutp); rc = mux->ops.run_impl(mux, timeoutp);
if (rc < 0) if (rc < 0)
return rc; return rc;
@@ -128,7 +130,7 @@ int iomux_register_fd(struct IOMux *mux, struct IOMuxFD *fd)
assert(fd != NULL); assert(fd != NULL);
assert(fd->callback != NULL); assert(fd->callback != NULL);
assert(fd->fd >= 0); 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) 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(mux != NULL);
assert(fd != NULL); assert(fd != NULL);
assert(fd->fd >= 0); 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) 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); assert(fd->fd >= 0);
if (fd->what != what) { if (fd->what != what) {
mux->update_events_impl(mux, fd); mux->ops.update_events_impl(mux, fd);
} else { } else {
rc = 0; rc = 0;
} }
+21 -15
View File
@@ -4,21 +4,7 @@
#include "ucore/iomux.h" #include "ucore/iomux.h"
#include "ucore/timers.h" #include "ucore/timers.h"
/** struct for IOMux implementations */ /** struct for IOMux implementations */
struct IOMux { struct IOMuxOps {
/* 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;
/** Run one iteration of the mux. /** Run one iteration of the mux.
* The mux implementation must honor the timeout, if given. * The mux implementation must honor the timeout, if given.
* The mux implementation must call iomux_timers_run after the * 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); int (*unregister_fd_impl)(struct IOMux *mux, struct IOMuxFD *fd);
/** Update an IOMuxFD (due to its events ('what') has changed*/ /** Update an IOMuxFD (due to its events ('what') has changed*/
int (*update_events_impl)(struct IOMux *mux, struct IOMuxFD *fd); 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); int iomux_select_init(struct IOMux *mux);
+9 -5
View File
@@ -200,6 +200,14 @@ static void iomux_select_delete(struct IOMux *mux)
} }
} }
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) int iomux_select_init(struct IOMux *mux)
{ {
struct IOMuxSelect *mux_select = calloc(1, sizeof *mux_select); struct IOMuxSelect *mux_select = calloc(1, sizeof *mux_select);
@@ -209,11 +217,7 @@ int iomux_select_init(struct IOMux *mux)
mux_select->max_fd = -1; mux_select->max_fd = -1;
mux->instance = mux_select; mux->instance = mux_select;
mux->run_impl = iomux_select_run; mux->ops = select_ops;
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;
return 0; return 0;
} }