3 * $Id: sel.c,v 1.1 1999/05/14 21:01:14 mdw Exp $
5 * I/O multiplexing support
7 * (c) 1999 Mark Wooding
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.1 1999/05/14 21:01:14 mdw
34 * Integrated `select' handling bits from the background resolver project.
38 /*----- Header files ------------------------------------------------------*/
44 #include <sys/types.h>
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @sel_init@ --- *
55 * Arguments: @sel_state *s@ = pointer to a state block to initialize
59 * Use: Initializes a select state block.
62 void sel_init(sel_state *s)
66 FD_ZERO(&s->fd[SEL_READ]);
67 FD_ZERO(&s->fd[SEL_WRITE]);
68 FD_ZERO(&s->fd[SEL_EXC]);
71 /* --- @sel_initfile@ --- *
73 * Arguments: @sel_state *s@ = select state to attach to
74 * @sel_file *f@ = pointer to a file block to initialize
75 * @int fd@ = the file descriptor to listen to
76 * @unsigned mode@ = what to listen for
77 * @void (*func)(int fd, unsigned mode, void *p)@ = handler
78 * @void *p@ = argument to pass to handler
82 * Use: Initializes a file block ready for use. The file block
83 * isn't added to the list of things to do until a call to
87 void sel_initfile(sel_state *s, sel_file *f,
88 int fd, unsigned mode,
89 void (*func)(int /*fd*/, unsigned /*mode*/, void */*p*/),
99 /* --- @sel_addfile@ --- *
101 * Arguments: @sel_file *f@ = pointer to a file block
105 * Use: Adds a file block into the list of things to listen to.
108 void sel_addfile(sel_file *f)
110 sel_file **ff = &f->s->files;
112 /* --- This little dance looks like line-noise, but it does the job --- */
114 while (*ff && (*ff)->fd > f->fd)
117 f->prev = (sel_file *)ff;
121 FD_SET(f->fd, f->s->fd + f->mode);
124 /* --- @sel_rmfile@ --- *
126 * Arguments: @sel_file *f@ = pointer to a file block
130 * Use: Removes a file block from the list of things to listen to.
133 void sel_rmfile(sel_file *f)
135 f->prev->next = f->next;
137 f->next->prev = f->prev;
138 FD_CLR(f->fd, f->s->fd + f->mode);
141 /* --- @sel_addtimer@ --- *
143 * Arguments: @sel_state *s@ = pointer to a state block
144 * @sel_timer *t@ = pointer to a timer block
145 * @struct timeval *tv@ = pointer to time to activate
146 * @void (*func)(struct timeval *tv, void *p)@ = handler
147 * @void *p@ = argument for handler function
151 * Use: Registers and sets up a timer.
154 void sel_addtimer(sel_state *s, sel_timer *t,
156 void (*func)(struct timeval */*tv*/, void */*p*/),
159 sel_timer **tt = &s->timers;
161 /* --- Set up the timer block --- */
167 /* --- More line noise --- */
169 while (*tt && tv_cmp(&(*tt)->tv, tv) > 0)
172 t->prev = (sel_timer *)tt;
178 /* --- @sel_rmtimer@ --- *
180 * Arguments: @sel_timer *t@ = pointer to timer block
184 * Use: Removes a timer from the list of timers.
187 void sel_rmtimer(sel_timer *t)
189 t->prev->next = t->next;
191 t->next->prev = t->prev;
194 /* --- @sel_select@ --- *
196 * Arguments: @sel_state *s@ = pointer to state block
198 * Returns: Zero if all OK, -1 on error.
200 * Use: Does a @select@ call (or equivalent @poll@).
203 int sel_select(sel_state *s)
205 fd_set fd[SEL_MODES];
209 memcpy(fd, s->fd, sizeof(s->fd));
212 gettimeofday(&now, 0);
213 tv_sub(&tv, &now, &s->timers->tv);
214 err = select(s->files ? s->files->fd + 1 : 0,
215 fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
217 gettimeofday(&tv, 0);
219 err = select(s->files ? s->files->fd + 1 : 0,
220 fd + SEL_READ, fd + SEL_WRITE, fd + SEL_EXC,
228 for (t = s->timers; t && tv_cmp(&t->tv, &tv) <= 0; t = tt) {
230 t->next = t->prev = t;
235 t->prev = (sel_timer *)&s->timers;
240 for (f = s->files; f; f = ff) {
242 if (FD_ISSET(f->fd, fd + f->mode))
243 f->func(f->fd, f->mode, f->p);
250 /*----- That's all, folks -------------------------------------------------*/