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.35 2003-02-12 15:06:43 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 for (p = *iochans; p; p = p->next)
66 p->force_event = EVENT_TIMEOUT;
71 timeout = &to; /* hang on select */
75 for (p = *iochans; p; p = p->next)
77 yaz_log(LOG_LOG, "fd=%d flags=%d force_event=%d",
78 p->fd, p->flags, p->force_event);
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 yaz_log(LOG_LOG, "select start");
91 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
92 yaz_log(LOG_LOG, "select end");
95 if (yaz_errno() == EINTR)
97 if (statserv_must_terminate())
99 for (p = *iochans; p; p = p->next)
100 p->force_event = EVENT_TIMEOUT;
106 /* Destroy the first member in the chain, and try again */
107 association *assoc = (association *)iochan_getdata(*iochans);
108 COMSTACK conn = assoc->client_link;
111 destroy_association(assoc);
112 iochan_destroy(*iochans);
113 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
117 for (p = *iochans; p; p = p->next)
119 int force_event = p->force_event;
120 time_t now = time(0);
123 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
124 force_event == EVENT_INPUT))
127 (*p->fun)(p, EVENT_INPUT);
129 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
130 force_event == EVENT_OUTPUT))
133 (*p->fun)(p, EVENT_OUTPUT);
135 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
136 force_event == EVENT_EXCEPT))
139 (*p->fun)(p, EVENT_EXCEPT);
141 if (!p->destroyed && ((p->max_idle && now - p->last_event >
142 p->max_idle) || force_event == EVENT_TIMEOUT))
145 (*p->fun)(p, EVENT_TIMEOUT);
148 for (p = *iochans; p; p = nextp)
156 /* We need to inform the threadlist that this channel has been destroyed */
159 /* Now reset the pointers */
164 for (pr = *iochans; pr; pr = pr->next)
167 assert(pr); /* grave error if it weren't there */