2 * ParaZ - a simple tool for harvesting performance data for parallel
3 * operations using Z39.50.
4 * Copyright (c) 2000-2004 Index Data ApS
5 * See LICENSE file for details.
9 * $Id: eventl.c,v 1.1 2006-11-14 20:44:37 quinn Exp $
10 * Based on revision YAZ' server/eventl.c 1.29.
24 #include <yaz/yconfig.h>
26 #include <yaz/comstack.h>
27 #include <yaz/xmalloc.h>
29 #include <yaz/statserv.h>
31 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
35 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
37 new_iochan->destroyed = 0;
39 new_iochan->flags = flags;
41 new_iochan->force_event = 0;
42 new_iochan->last_event = new_iochan->max_idle = 0;
43 new_iochan->next = NULL;
47 int event_loop(IOCHAN *iochans)
49 do /* loop as long as there are active associations to process */
52 fd_set in, out, except;
54 static struct timeval nullto = {0, 0}, to;
55 struct timeval *timeout;
60 timeout = &to; /* hang on select */
64 for (p = *iochans; p; p = p->next)
67 timeout = &nullto; /* polling select */
68 if (p->flags & EVENT_INPUT)
70 if (p->flags & EVENT_OUTPUT)
72 if (p->flags & EVENT_EXCEPT)
73 FD_SET(p->fd, &except);
77 if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
84 for (p = *iochans; p; p = p->next)
86 int force_event = p->force_event;
90 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
91 force_event == EVENT_INPUT))
94 (*p->fun)(p, EVENT_INPUT);
96 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
97 force_event == EVENT_OUTPUT))
100 (*p->fun)(p, EVENT_OUTPUT);
102 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
103 force_event == EVENT_EXCEPT))
106 (*p->fun)(p, EVENT_EXCEPT);
108 if (!p->destroyed && ((p->max_idle && now - p->last_event >
109 p->max_idle) || force_event == EVENT_TIMEOUT))
112 (*p->fun)(p, EVENT_TIMEOUT);
115 for (p = *iochans; p; p = nextp)
123 /* Now reset the pointers */
128 for (pr = *iochans; pr; pr = pr->next)
131 assert(pr); /* grave error if it weren't there */