1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002 Cray Inc.
5 * Copyright (c) 2002 Eric Hoffman
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Provides a general mechanism for registering and dispatching
25 * io events through the select system call.
29 #include <sys/filio.h>
31 #include <sys/ioctl.h>
35 #include <sys/types.h>
42 #include <procbridge.h>
45 static struct timeval beginning_of_epoch;
46 static io_handler io_handlers;
50 * Return: the current time in canonical units: a 64 bit number
51 * where the most significant 32 bits contains the number
52 * of seconds, and the least signficant a count of (1/(2^32))ths
57 struct timeval result;
59 gettimeofday(&result,0);
60 return((((unsigned long long)result.tv_sec)<<32)|
61 (((unsigned long long)result.tv_usec)<<32)/1000000);
65 /* Function: register_io_handler
66 * Arguments: fd: the file descriptor of interest
67 * type: a mask of READ_HANDLER, WRITE_HANDLER, EXCEPTION_HANDLER
68 * function: a function to call when io is available on fd
69 * arg: an opaque correlator to return to the handler
70 * Returns: a pointer to the io_handler structure
72 io_handler register_io_handler(int fd,
74 int (*function)(void *),
77 io_handler i=(io_handler)malloc(sizeof(struct io_handler));
84 if ((i->next=io_handlers)) i->next->last=&i->next;
90 /* Function: remove_io_handler
91 * Arguments: i: a pointer to the handler to stop servicing
93 * remove_io_handler() doesn't actually free the handler, due
94 * to reentrancy problems. it just marks the handler for
95 * later cleanup by the blocking function.
97 void remove_io_handler (io_handler i)
102 static void set_flag(io_handler n,fd_set *r, fd_set *w, fd_set *e)
104 if (n->type & READ_HANDLER) FD_SET(n->fd, r);
105 if (n->type & WRITE_HANDLER) FD_SET(n->fd, w);
106 if (n->type & EXCEPTION_HANDLER) FD_SET(n->fd, e);
109 static int prepare_fd_sets(fd_set *r, fd_set *w, fd_set *e)
118 for (k=&io_handlers;*k;){
134 static int execute_callbacks(fd_set *r, fd_set *w, fd_set *e)
139 for (j = io_handlers; j; j = j->next) {
144 if (FD_ISSET(j->fd, r) && (j->type & READ_HANDLER)) {
148 if (FD_ISSET(j->fd, w) && (j->type & WRITE_HANDLER)) {
152 if (FD_ISSET(j->fd, e) && (j->type & EXCEPTION_HANDLER)) {
159 if (!(*j->function)(j->argument))
168 #ifdef ENABLE_SELECT_DISPATCH
171 pthread_mutex_t mutex;
179 struct timeval *timeout;
180 struct timeval submit_time;
182 PTHREAD_MUTEX_INITIALIZER,
183 PTHREAD_COND_INITIALIZER,
185 NULL, NULL, NULL, NULL,
188 extern int liblustre_wait_event(int timeout);
189 extern procbridge __global_procbridge;
192 * this will intercept syscall select() of user apps
195 int select(int n, fd_set *rset, fd_set *wset, fd_set *eset,
196 struct timeval *timeout)
198 LASSERT(fd_extra.submitted == 0);
202 fd_extra.rset = rset;
203 fd_extra.wset = wset;
204 fd_extra.eset = eset;
205 fd_extra.timeout = timeout;
207 liblustre_wait_event(0);
208 pthread_mutex_lock(&fd_extra.mutex);
209 gettimeofday(&fd_extra.submit_time, NULL);
210 fd_extra.submitted = 1;
211 LASSERT(__global_procbridge);
212 procbridge_wakeup_nal(__global_procbridge);
215 if (fd_extra.submitted)
216 pthread_cond_wait(&fd_extra.cond, &fd_extra.mutex);
217 pthread_mutex_unlock(&fd_extra.mutex);
219 liblustre_wait_event(0);
221 pthread_mutex_lock(&fd_extra.mutex);
222 if (fd_extra.submitted)
224 pthread_mutex_unlock(&fd_extra.mutex);
226 LASSERT(fd_extra.nready >= 0);
227 LASSERT(fd_extra.submitted == 0);
228 return fd_extra.nready;
231 static int merge_fds(int max, fd_set *rset, fd_set *wset, fd_set *eset)
239 for (i = 0; i < __FD_SETSIZE/__NFDBITS; i++) {
240 LASSERT(!fd_extra.rset ||
241 !(__FDS_BITS(rset)[i] & __FDS_BITS(fd_extra.rset)[i]));
242 LASSERT(!fd_extra.wset ||
243 !(__FDS_BITS(wset)[i] & __FDS_BITS(fd_extra.wset)[i]));
244 LASSERT(!fd_extra.eset ||
245 !(__FDS_BITS(eset)[i] & __FDS_BITS(fd_extra.eset)[i]));
247 if (fd_extra.rset && __FDS_BITS(fd_extra.rset)[i])
248 __FDS_BITS(rset)[i] |= __FDS_BITS(fd_extra.rset)[i];
249 if (fd_extra.wset && __FDS_BITS(fd_extra.wset)[i])
250 __FDS_BITS(wset)[i] |= __FDS_BITS(fd_extra.wset)[i];
251 if (fd_extra.eset && __FDS_BITS(fd_extra.eset)[i])
252 __FDS_BITS(eset)[i] |= __FDS_BITS(fd_extra.eset)[i];
255 return (fd_extra.maxfd > max ? fd_extra.maxfd : max);
259 int timeval_ge(struct timeval *tv1, struct timeval *tv2)
262 return ((tv1->tv_sec - tv2->tv_sec) * 1000000 +
263 (tv1->tv_usec - tv2->tv_usec) >= 0);
267 * choose the most recent timeout value
269 static struct timeval *choose_timeout(struct timeval *tv1,
277 if (timeval_ge(tv1, tv2))
283 /* Function: select_timer_block
284 * Arguments: until: an absolute time when the select should return
286 * This function dispatches the various file descriptors' handler
287 * functions, if the kernel indicates there is io available.
289 void select_timer_block(when until)
292 struct timeval timeout;
293 struct timeval *timeout_pointer, *select_timeout;
294 int max, nready, nexec;
301 interval = until - now();
302 timeout.tv_sec = (interval >> 32);
303 timeout.tv_usec = ((interval << 32) / 1000000) >> 32;
304 timeout_pointer = &timeout;
306 timeout_pointer = NULL;
309 max = prepare_fd_sets(&fds[0], &fds[1], &fds[2]);
310 select_timeout = timeout_pointer;
312 pthread_mutex_lock(&fd_extra.mutex);
313 fd_handling = fd_extra.submitted;
314 pthread_mutex_unlock(&fd_extra.mutex);
316 max = merge_fds(max, &fds[0], &fds[1], &fds[2]);
317 select_timeout = choose_timeout(timeout_pointer, fd_extra.timeout);
320 /* XXX only compile for linux */
322 nready = syscall(SYS_select, max, &fds[0], &fds[1], &fds[2],
325 nready = syscall(SYS__newselect, max, &fds[0], &fds[1], &fds[2],
329 CERROR("select return err %d, errno %d\n", nready, errno);
334 nexec = execute_callbacks(&fds[0], &fds[1], &fds[2]);
339 /* even both nready & nexec are 0, we still need try to wakeup
340 * upper thread since it may have timed out
343 LASSERT(nready >= 0);
345 pthread_mutex_lock(&fd_extra.mutex);
348 *fd_extra.rset = fds[0];
350 *fd_extra.wset = fds[1];
352 *fd_extra.eset = fds[2];
353 fd_extra.nready = nready;
354 fd_extra.submitted = 0;
359 if (fd_extra.timeout) {
360 gettimeofday(&t, NULL);
361 if (timeval_ge(&t, &fd_extra.submit_time))
362 fd_extra.submitted = 0;
366 pthread_cond_signal(&fd_extra.cond);
367 pthread_mutex_unlock(&fd_extra.mutex);
370 /* haven't found portals event, go back to loop if time
373 if (timeout_pointer == NULL || now() >= until)
378 #else /* !ENABLE_SELECT_DISPATCH */
380 /* Function: select_timer_block
381 * Arguments: until: an absolute time when the select should return
383 * This function dispatches the various file descriptors' handler
384 * functions, if the kernel indicates there is io available.
386 void select_timer_block(when until)
389 struct timeval timeout;
390 struct timeval *timeout_pointer;
396 interval = until - now();
397 timeout.tv_sec = (interval >> 32);
398 timeout.tv_usec = ((interval << 32) / 1000000) >> 32;
399 timeout_pointer = &timeout;
401 timeout_pointer = NULL;
403 max = prepare_fd_sets(&fds[0], &fds[1], &fds[2]);
405 nready = select(max, &fds[0], &fds[1], &fds[2], timeout_pointer);
407 execute_callbacks(&fds[0], &fds[1], &fds[2]);
409 #endif /* ENABLE_SELECT_DISPATCH */
411 /* Function: init_unix_timer()
412 * is called to initialize the library
414 void init_unix_timer()
417 gettimeofday(&beginning_of_epoch, 0);
418 initialize_timer(select_timer_block);