From: mdw Date: Thu, 19 Aug 1999 18:30:26 +0000 (+0000) Subject: Implement hooks for foreign select-using systems (currently not well X-Git-Tag: 2.0.4~245 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/commitdiff_plain/1226c514d496f4552b43682c8ca3dde7d87f952a Implement hooks for foreign select-using systems (currently not well tested). --- diff --git a/sel.c b/sel.c index 43f95dc..de8cdef 100644 --- a/sel.c +++ b/sel.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sel.c,v 1.3 1999/05/21 22:13:59 mdw Exp $ + * $Id: sel.c,v 1.4 1999/08/19 18:30:26 mdw Exp $ * * I/O multiplexing support * @@ -30,6 +30,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: sel.c,v $ + * Revision 1.4 1999/08/19 18:30:26 mdw + * Implement hooks for foreign select-using systems (currently not well + * tested). + * * Revision 1.3 1999/05/21 22:13:59 mdw * Use new `tv' macros. Fix ordering bug for timeout selectors. * @@ -69,6 +73,7 @@ void sel_init(sel_state *s) { s->files = 0; s->timers = 0; + s->hooks = 0; FD_ZERO(&s->fd[SEL_READ]); FD_ZERO(&s->fd[SEL_WRITE]); FD_ZERO(&s->fd[SEL_EXC]); @@ -197,6 +202,74 @@ void sel_rmtimer(sel_timer *t) t->next->prev = t->prev; } +/* --- @sel_addhook@ --- * + * + * Arguments: @sel_state *s@ = pointer to state block + * @sel_hook *h@ = pointer to hook block + * @sel_hookfn before, after@ = hook functions + * @void *p@ = pointer argument to pass to hook functions + * + * Returns: --- + * + * Use: Registers hook functions to be called on each select call. + */ + +void sel_addhook(sel_state *s, sel_hook *h, + sel_hookfn before, sel_hookfn after, + void *p) +{ + h->before = before; + h->after = after; + h->p = p; + h->next = s->hooks; + h->prev = (sel_hook *)&s->hooks; + if (s->hooks) + s->hooks->prev = h; + s->hooks = h; +} + +/* --- @sel_rmhook@ --- * + * + * Arguments: @sel_hook *h@ = pointer to hook block + * + * Returns: --- + * + * Use: Removes hook functions. + */ + +void sel_rmhook(sel_hook *h) +{ + if (h->next) + h->next->prev = h->prev; + h->prev->next = h->next; +} + +/* --- @sel_fdmerge@ --- * + * + * Arguments: @fd_set *dest@ = destination FD set + * @fd_set *fd@ = pointer to set to merge + * @int maxfd@ = highest numbered descriptor in @fd@ + 1 + * + * Returns: Actual highest numbered descriptor. + * + * Use: Merges file descriptor sets, and returns an accurate @maxfd@ + * value. + */ + +int sel_fdmerge(fd_set *dest, fd_set *fd, int maxfd) +{ + int i, m = -1; + + for (i = 0; i < maxfd; i++) { + if (FD_ISSET(i, fd)) { + FD_SET(i, dest); + m = i; + } + } + + return (m + 1); +} + /* --- @sel_select@ --- * * * Arguments: @sel_state *s@ = pointer to state block @@ -208,44 +281,77 @@ void sel_rmtimer(sel_timer *t) int sel_select(sel_state *s) { - fd_set fd[SEL_MODES]; - struct timeval tv; + sel_args a; int err; - memcpy(fd, s->fd, sizeof(s->fd)); - if (s->timers) { - struct timeval now; - gettimeofday(&now, 0); - TV_SUB(&tv, &s->timers->tv, &now); - err = select(s->files ? s->files->fd + 1 : 0, - fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC, - &tv); - gettimeofday(&tv, 0); - } else - err = select(s->files ? s->files->fd + 1 : 0, - fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC, - 0); - - if (err < 0) + /* --- Initialize the argument block --- */ + + a.maxfd = s->files ? s->files->fd + 1 : 0; + memcpy(a.fd, s->fd, sizeof(a.fd)); + if (s->timers || s->hooks) + gettimeofday(&a.now, 0); + if (!s->timers) + a.tvp = 0; + else { + TV_SUB(&a.tv, &s->timers->tv, &a.now); + a.tvp = &a.tv; + } + + /* --- Grind through the pre hooks --- */ + + { + sel_hook *h = s->hooks; + while (h) { + sel_hook *hh = h; + h = h->next; + if (hh->before) + hh->before(s, &a, hh->p); + } + } + + /* --- Run the @select@ call --- */ + + if ((err = select(a.maxfd, + &a.fd[SEL_READ], &a.fd[SEL_WRITE], &a.fd[SEL_EXC], + a.tvp)) < 0) return (err); + if (a.tvp) + gettimeofday(&a.now, 0); + + /* --- Run through the hooks again --- */ + + { + sel_hook *h = s->hooks; + while (h) { + sel_hook *hh = h; + h = h->next; + if (hh->after) + hh->after(s, &a, hh->p); + } + } + + /* --- Run through the timers --- */ + { sel_timer *t, *tt; - for (t = s->timers; t && TV_CMP(&t->tv, <=, &tv); t = tt) { + for (t = s->timers; t && TV_CMP(&t->tv, <=, &a.now); t = tt) { tt = t->next; t->next = t->prev = t; - t->func(&tv, t->p); + t->func(&a.now, t->p); } s->timers = t; if (t) t->prev = (sel_timer *)&s->timers; } + /* --- And finally run through the files --- */ + { sel_file *f, *ff; for (f = s->files; f; f = ff) { ff = f->next; - if (FD_ISSET(f->fd, fd + f->mode)) + if (FD_ISSET(f->fd, a.fd + f->mode)) f->func(f->fd, f->mode, f->p); } } diff --git a/sel.h b/sel.h index 8d121ef..c2aa57f 100644 --- a/sel.h +++ b/sel.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: sel.h,v 1.4 1999/05/22 13:39:15 mdw Exp $ + * $Id: sel.h,v 1.5 1999/08/19 18:30:26 mdw Exp $ * * I/O multiplexing support * @@ -30,6 +30,10 @@ /*----- Revision history --------------------------------------------------* * * $Log: sel.h,v $ + * Revision 1.5 1999/08/19 18:30:26 mdw + * Implement hooks for foreign select-using systems (currently not well + * tested). + * * Revision 1.4 1999/05/22 13:39:15 mdw * Change spelling of `multiplexor'. ;-) * @@ -95,6 +99,28 @@ /*----- Data structures ---------------------------------------------------*/ +/* --- A multiplexor --- * + * + * The files are sorted in reverse order of file descriptor number; the + * timers are in normal order of occurrence. Thus, the interesting one + * is always at the front of the list. + */ + +enum { + SEL_READ, + SEL_WRITE, + SEL_EXC, + SEL_MODES +}; + +typedef struct sel_state { + struct sel_file *files; + struct sel_timer *timers; + struct sel_hook *hooks; + fd_set fd[SEL_MODES]; + struct timeval tv; +} sel_state; + /* --- Listening for a file --- */ typedef struct sel_file { @@ -107,36 +133,40 @@ typedef struct sel_file { void *p; } sel_file; -enum { - SEL_READ, - SEL_WRITE, - SEL_EXC, - SEL_MODES -}; - /* --- Waiting for a timeout --- */ typedef struct sel_timer { struct sel_timer *next; struct sel_timer *prev; struct timeval tv; - void (*func)(struct timeval *tv, void *p); + void (*func)(struct timeval */*tv*/, void */*p*/); void *p; } sel_timer; -/* --- A multiplexor --- * +/* --- A select argument block --- */ + +typedef struct sel_args { + int maxfd; + fd_set fd[SEL_MODES]; + struct timeval tv, *tvp; + struct timeval now; +} sel_args; + +/* --- A selector hook --- * * - * The files are sorted in reverse order of file descriptor number; the - * timers are in normal order of occurrence. Thus, the interesting one - * is always at the front of the list. + * The hooks are called (in arbitrary order) on each select. */ -typedef struct sel_state { - struct sel_file *files; - struct sel_timer *timers; - fd_set fd[SEL_MODES]; - struct timeval tv; -} sel_state; +typedef void (*sel_hookfn)(sel_state */*s*/, + sel_args */*a*/, + void */*p*/); + +typedef struct sel_hook { + struct sel_hook *next; + struct sel_hook *prev; + sel_hookfn before, after; + void *p; +} sel_hook; /*----- Functions provided ------------------------------------------------*/ @@ -201,7 +231,6 @@ extern void sel_rmfile(sel_file */*f*/); * Arguments: @sel_state *s@ = pointer to a state block * @sel_timer *t@ = pointer to a timer block * @struct timeval *tv@ = pointer to time to activate - * @void (*func)(struct timeval *tv, void *p)@ = handler * @void *p@ = argument for handler function * * Returns: --- @@ -226,6 +255,47 @@ extern void sel_addtimer(sel_state */*s*/, sel_timer */*t*/, extern void sel_rmtimer(sel_timer */*t*/); +/* --- @sel_addhook@ --- * + * + * Arguments: @sel_state *s@ = pointer to state block + * @sel_hook *h@ = pointer to hook block + * @sel_hookfn before, after@ = hook functions + * @void *p@ = pointer argument to pass to hook functions + * + * Returns: --- + * + * Use: Registers hook functions to be called on each select call. + */ + +extern void sel_addhook(sel_state */*s*/, sel_hook */*h*/, + sel_hookfn /*before*/, sel_hookfn /*after*/, + void */*p*/); + +/* --- @sel_rmhook@ --- * + * + * Arguments: @sel_hook *h@ = pointer to hook block + * + * Returns: --- + * + * Use: Removes hook functions. + */ + +extern void sel_rmhook(sel_hook */*h*/); + +/* --- @sel_fdmerge@ --- * + * + * Arguments: @fd_set *dest@ = destination FD set + * @fd_set *fd@ = pointer to set to merge + * @int maxfd@ = highest numbered descriptor in @fd@ + 1 + * + * Returns: Actual highest numbered descriptor. + * + * Use: Merges file descriptor sets, and returns an accurate @maxfd@ + * value. + */ + +extern int sel_fdmerge(fd_set */*dest*/, fd_set */*fd*/, int /*maxfd*/); + /* --- @sel_select@ --- * * * Arguments: @sel_state *s@ = pointer to state block