3 * $Id: sel.c,v 1.2 1999/05/15 10:33:32 mdw Exp $
5 * I/O multiplexing support
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 1999/05/15 10:33:32 mdw
34 * Fix copyright notices.
36 * Revision 1.1 1999/05/14 21:01:14 mdw
37 * Integrated `select' handling bits from the background resolver project.
41 /*----- Header files ------------------------------------------------------*/
47 #include <sys/types.h>
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @sel_init@ --- *
58 * Arguments: @sel_state *s@ = pointer to a state block to initialize
62 * Use: Initializes a select state block.
65 void sel_init(sel_state *s)
69 FD_ZERO(&s->fd[SEL_READ]);
70 FD_ZERO(&s->fd[SEL_WRITE]);
71 FD_ZERO(&s->fd[SEL_EXC]);
74 /* --- @sel_initfile@ --- *
76 * Arguments: @sel_state *s@ = select state to attach to
77 * @sel_file *f@ = pointer to a file block to initialize
78 * @int fd@ = the file descriptor to listen to
79 * @unsigned mode@ = what to listen for
80 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
81 * @void *p@ = argument to pass to handler
85 * Use: Initializes a file block ready for use. The file block
86 * isn't added to the list of things to do until a call to
90 void sel_initfile(sel_state *s, sel_file *f,
91 int fd, unsigned mode,
92 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
102 /* --- @sel_addfile@ --- *
104 * Arguments: @sel_file *f@ = pointer to a file block
108 * Use: Adds a file block into the list of things to listen to.
111 void sel_addfile(sel_file *f)
113 sel_file **ff = &f->s->files;
115 /* --- This little dance looks like line-noise, but it does the job --- */
117 while (*ff && (*ff)->fd > f->fd)
120 f->prev = (sel_file *)ff;
124 FD_SET(f->fd, f->s->fd + f->mode);
127 /* --- @sel_rmfile@ --- *
129 * Arguments: @sel_file *f@ = pointer to a file block
133 * Use: Removes a file block from the list of things to listen to.
136 void sel_rmfile(sel_file *f)
138 f->prev->next = f->next;
140 f->next->prev = f->prev;
141 FD_CLR(f->fd, f->s->fd + f->mode);
144 /* --- @sel_addtimer@ --- *
146 * Arguments: @sel_state *s@ = pointer to a state block
147 * @sel_timer *t@ = pointer to a timer block
148 * @struct timeval *tv@ = pointer to time to activate
149 * @void (*func)(struct timeval *tv, void *p)@ = handler
150 * @void *p@ = argument for handler function
154 * Use: Registers and sets up a timer.
157 void sel_addtimer(sel_state *s, sel_timer *t,
159 void (*func)(struct timeval */*tv*/, void */*p*/),
162 sel_timer **tt = &s->timers;
164 /* --- Set up the timer block --- */
170 /* --- More line noise --- */
172 while (*tt && tv_cmp(&(*tt)->tv, tv) > 0)
175 t->prev = (sel_timer *)tt;
181 /* --- @sel_rmtimer@ --- *
183 * Arguments: @sel_timer *t@ = pointer to timer block
187 * Use: Removes a timer from the list of timers.
190 void sel_rmtimer(sel_timer *t)
192 t->prev->next = t->next;
194 t->next->prev = t->prev;
197 /* --- @sel_select@ --- *
199 * Arguments: @sel_state *s@ = pointer to state block
201 * Returns: Zero if all OK, -1 on error.
203 * Use: Does a @select@ call (or equivalent @poll@).
206 int sel_select(sel_state *s)
208 fd_set fd[SEL_MODES];
212 memcpy(fd, s->fd, sizeof(s->fd));
215 gettimeofday(&now, 0);
216 tv_sub(&tv, &now, &s->timers->tv);
217 err = select(s->files ? s->files->fd + 1 : 0,
218 fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
220 gettimeofday(&tv, 0);
222 err = select(s->files ? s->files->fd + 1 : 0,
223 fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
231 for (t = s->timers; t && tv_cmp(&t->tv, &tv) <= 0; t = tt) {
233 t->next = t->prev = t;
238 t->prev = (sel_timer *)&s->timers;
243 for (f = s->files; f; f = ff) {
245 if (FD_ISSET(f->fd, fd + f->mode))
246 f->func(f->fd, f->mode, f->p);
253 /*----- That's all, folks -------------------------------------------------*/