2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: eventl.c,v 1.39 2003-02-21 12:08:59 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 to;
63 if (statserv_must_terminate())
65 for (p = *iochans; p; p = p->next)
66 p->force_event = EVENT_TIMEOUT;
74 for (p = *iochans; p; p = p->next)
77 yaz_log(LOG_DEBUG, "fd=%d flags=%d force_event=%d",
78 p->fd, p->flags, p->force_event);
80 to.tv_sec = 0; /* 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);
89 if (p->max_idle && p->last_event)
91 ftime = p->last_event + p->max_idle;
100 yaz_log(LOG_DEBUG, "select start %ld", (long) to.tv_sec);
101 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, &to);
102 yaz_log(LOG_DEBUG, "select end");
105 if (yaz_errno() == EINTR)
107 if (statserv_must_terminate())
109 for (p = *iochans; p; p = p->next)
110 p->force_event = EVENT_TIMEOUT;
116 /* Destroy the first member in the chain, and try again */
117 association *assoc = (association *)iochan_getdata(*iochans);
118 COMSTACK conn = assoc->client_link;
121 destroy_association(assoc);
122 iochan_destroy(*iochans);
123 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
128 for (p = *iochans; p; p = p->next)
130 int force_event = p->force_event;
133 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
134 force_event == EVENT_INPUT))
137 (*p->fun)(p, EVENT_INPUT);
139 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
140 force_event == EVENT_OUTPUT))
143 (*p->fun)(p, EVENT_OUTPUT);
145 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
146 force_event == EVENT_EXCEPT))
149 (*p->fun)(p, EVENT_EXCEPT);
151 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
152 p->max_idle) || force_event == EVENT_TIMEOUT))
155 (*p->fun)(p, EVENT_TIMEOUT);
158 for (p = *iochans; p; p = nextp)
166 /* We need to inform the threadlist that this channel has been destroyed */
169 /* Now reset the pointers */
174 for (pr = *iochans; pr; pr = pr->next)
177 assert(pr); /* grave error if it weren't there */