diff --git a/include/ucore/iomux.h b/include/ucore/iomux.h index f5604fb..414ce21 100644 --- a/include/ucore/iomux.h +++ b/include/ucore/iomux.h @@ -132,6 +132,11 @@ int iomux_update_events(struct IOMux *mux, struct IOMuxFD *fd, unsigned int what */ struct UCTimers *iomux_get_timers(struct IOMux *mux); +/** Given an UCTimers associated with an IOMux, return the associated IOMux. + * (the UCTimers must previously been obtained from iomux_get_timers directly, + * or indirectly by the UCTimers provieded in a timer callback handler. + */ +struct IOMux *iomux_from_timers(struct UCTimers *timers); #ifdef __cplusplus } #endif diff --git a/src/iomux_impl.c b/src/iomux_impl.c index a1e52c2..bbb16bc 100644 --- a/src/iomux_impl.c +++ b/src/iomux_impl.c @@ -168,3 +168,8 @@ struct UCTimers *iomux_get_timers(struct IOMux *mux) return &mux->timers; } +struct IOMux *iomux_from_timers(struct UCTimers *timers) +{ + return (struct IOMux*)timers; //timers is the 1. member of IOMux +} + diff --git a/src/iomux_impl.h b/src/iomux_impl.h index 794c738..ea51873 100644 --- a/src/iomux_impl.h +++ b/src/iomux_impl.h @@ -29,7 +29,7 @@ struct IOMuxOps { }; struct IOMux { - /* TImer instance */ + /* TImer instance - must be the 1. member*/ struct UCTimers timers; /** diff --git a/test/udp_heartbeat.c b/test/udp_heartbeat.c index f33255f..3e73385 100644 --- a/test/udp_heartbeat.c +++ b/test/udp_heartbeat.c @@ -1,5 +1,6 @@ #define DEBUG #include +#include #include #include #include @@ -76,6 +77,7 @@ void peer_timer_cb(struct UCTimers *timers, struct UCTimer *timer) struct HBPeer *peer = UC_CONTAINER_OF(timer, struct HBPeer, timer); struct HBContext *ctx = timer->cookie_ptr; time_t now; + struct IOMux *mux; uc_timers_add(timers, timer, PEER_HB_MISS , 0); now = time(NULL); @@ -85,6 +87,9 @@ void peer_timer_cb(struct UCTimers *timers, struct UCTimer *timer) } peer_send(ctx, peer); + //jsut a small test of iomux_from_timers + mux = iomux_from_timers(timers); + assert(mux == ctx->mux); } void peer_add_addr(struct HBContext *ctx, const struct sockaddr_in *addr)