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.33 2002-11-26 13:15:42 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;
63 if (statserv_must_terminate())
65 fprintf (stderr, "must terminate 1\n");
67 for (p = *iochans; p; p = p->next)
68 p->force_event = EVENT_TIMEOUT;
73 timeout = &to; /* hang on select */
77 for (p = *iochans; p; p = p->next)
80 timeout = &nullto; /* polling select */
81 if (p->flags & EVENT_INPUT)
83 if (p->flags & EVENT_OUTPUT)
85 if (p->flags & EVENT_EXCEPT)
86 FD_SET(p->fd, &except);
90 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
93 if (yaz_errno() == EINTR)
95 if (statserv_must_terminate())
97 for (p = *iochans; p; p = p->next)
98 p->force_event = EVENT_TIMEOUT;
104 /* Destroy the first member in the chain, and try again */
105 association *assoc = (association *)iochan_getdata(*iochans);
106 COMSTACK conn = assoc->client_link;
109 destroy_association(assoc);
110 iochan_destroy(*iochans);
111 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
115 for (p = *iochans; p; p = p->next)
117 int force_event = p->force_event;
118 time_t now = time(0);
121 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
122 force_event == EVENT_INPUT))
125 (*p->fun)(p, EVENT_INPUT);
127 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
128 force_event == EVENT_OUTPUT))
131 (*p->fun)(p, EVENT_OUTPUT);
133 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
134 force_event == EVENT_EXCEPT))
137 (*p->fun)(p, EVENT_EXCEPT);
139 if (!p->destroyed && ((p->max_idle && now - p->last_event >
140 p->max_idle) || force_event == EVENT_TIMEOUT))
143 (*p->fun)(p, EVENT_TIMEOUT);
146 for (p = *iochans; p; p = nextp)
154 /* We need to inform the threadlist that this channel has been destroyed */
157 /* Now reset the pointers */
162 for (pr = *iochans; pr; pr = pr->next)
165 assert(pr); /* grave error if it weren't there */