2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: eventl.c,v 1.31 2001-10-05 14:43:22 adam Exp $
20 #include <yaz/yconfig.h>
22 #include <yaz/comstack.h>
23 #include <yaz/xmalloc.h>
26 #include <yaz/statserv.h>
30 #define YAZ_EV_SELECT pth_select
34 #define YAZ_EV_SELECT select
37 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
41 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
43 new_iochan->destroyed = 0;
45 new_iochan->flags = flags;
47 new_iochan->force_event = 0;
48 new_iochan->last_event = new_iochan->max_idle = 0;
49 new_iochan->next = NULL;
53 int event_loop(IOCHAN *iochans)
55 do /* loop as long as there are active associations to process */
58 fd_set in, out, except;
60 static struct timeval nullto = {0, 0}, to;
61 struct timeval *timeout;
66 timeout = &to; /* hang on select */
70 for (p = *iochans; p; p = p->next)
73 timeout = &nullto; /* polling select */
74 if (p->flags & EVENT_INPUT)
76 if (p->flags & EVENT_OUTPUT)
78 if (p->flags & EVENT_EXCEPT)
79 FD_SET(p->fd, &except);
83 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
90 /* Destroy the first member in the chain, and try again */
91 association *assoc = (association *)iochan_getdata(*iochans);
92 COMSTACK conn = assoc->client_link;
95 destroy_association(assoc);
96 iochan_destroy(*iochans);
97 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
101 for (p = *iochans; p; p = p->next)
103 int force_event = p->force_event;
104 time_t now = time(0);
107 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
108 force_event == EVENT_INPUT))
111 (*p->fun)(p, EVENT_INPUT);
113 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
114 force_event == EVENT_OUTPUT))
117 (*p->fun)(p, EVENT_OUTPUT);
119 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
120 force_event == EVENT_EXCEPT))
123 (*p->fun)(p, EVENT_EXCEPT);
125 if (!p->destroyed && ((p->max_idle && now - p->last_event >
126 p->max_idle) || force_event == EVENT_TIMEOUT))
129 (*p->fun)(p, EVENT_TIMEOUT);
132 for (p = *iochans; p; p = nextp)
140 /* We need to inform the threadlist that this channel has been destroyed */
143 /* Now reset the pointers */
148 for (pr = *iochans; pr; pr = pr->next)
151 assert(pr); /* grave error if it weren't there */