2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: eventl.c,v 1.12 2007-11-09 18:47:50 adam Exp $
10 * \brief Implements event loop handling for GFS.
12 * This source implements the main event loop for the Generic Frontend
13 * Server. It uses select(2).
23 #include <sys/types.h>
42 #include <sys/select.h>
47 #include <yaz/comstack.h>
48 #include <yaz/xmalloc.h>
51 #include <yaz/statserv.h>
55 #define YAZ_EV_SELECT pth_select
59 #define YAZ_EV_SELECT select
62 static int log_level=0;
63 static int log_level_initialized=0;
65 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
69 if (!log_level_initialized)
71 log_level=yaz_log_module_level("eventl");
72 log_level_initialized=1;
75 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
77 new_iochan->destroyed = 0;
79 new_iochan->flags = flags;
81 new_iochan->force_event = 0;
82 new_iochan->last_event = new_iochan->max_idle = 0;
83 new_iochan->next = NULL;
84 new_iochan->chan_id = chan_id;
89 int iochan_is_alive(IOCHAN chan)
91 static struct timeval to;
92 fd_set in, out, except;
102 FD_SET(chan->fd, &in);
106 res = YAZ_EV_SELECT(max + 1, &in, 0, 0, &to);
109 if (!ir_read(chan, EVENT_INPUT))
114 int event_loop(IOCHAN *iochans)
116 do /* loop as long as there are active associations to process */
123 struct yaz_poll_fd *fds = 0;
125 fd_set in, out, except;
126 static struct timeval to;
130 time_t now = time(0);
132 if (statserv_must_terminate())
134 for (p = *iochans; p; p = p->next)
135 p->force_event = EVENT_TIMEOUT;
138 for (p = *iochans; p; p = p->next)
140 fds = xmalloc(no_fds * sizeof(*fds));
141 for (i = 0, p = *iochans; p; p = p->next, i++)
144 enum yaz_poll_mask input_mask = 0;
145 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
146 p->fd, p->flags, p->force_event);
148 tv_sec = 0; /* polling select */
149 if (p->flags & EVENT_INPUT)
150 input_mask += yaz_poll_read;
151 if (p->flags & EVENT_OUTPUT)
152 input_mask += yaz_poll_write;
153 if (p->flags & EVENT_EXCEPT)
154 input_mask += yaz_poll_except;
155 if (p->max_idle && p->last_event)
157 ftime = p->last_event + p->max_idle;
166 fds[i].input_mask = input_mask;
168 res = yaz_poll(fds, no_fds, tv_sec);
176 for (p = *iochans; p; p = p->next)
179 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
180 p->fd, p->flags, p->force_event);
182 to.tv_sec = 0; /* polling select */
183 if (p->flags & EVENT_INPUT)
185 if (p->flags & EVENT_OUTPUT)
187 if (p->flags & EVENT_EXCEPT)
188 FD_SET(p->fd, &except);
191 if (p->max_idle && p->last_event)
193 ftime = p->last_event + p->max_idle;
202 yaz_log(log_level, "select start %ld", (long) to.tv_sec);
203 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, &to);
204 yaz_log(log_level, "select end");
208 if (yaz_errno() == EINTR)
210 if (statserv_must_terminate())
212 for (p = *iochans; p; p = p->next)
213 p->force_event = EVENT_TIMEOUT;
220 /* Destroy the first member in the chain, and try again */
221 association *assoc = (association *)iochan_getdata(*iochans);
222 COMSTACK conn = assoc->client_link;
225 destroy_association(assoc);
226 iochan_destroy(*iochans);
227 yaz_log(log_level, "error select, destroying iochan %p",
233 for (i = 0, p = *iochans; p; p = p->next, i++)
235 int force_event = p->force_event;
236 enum yaz_poll_mask output_mask = fds[i].output_mask;
239 if (!p->destroyed && ((output_mask & yaz_poll_read) ||
240 force_event == EVENT_INPUT))
243 (*p->fun)(p, EVENT_INPUT);
245 if (!p->destroyed && ((output_mask & yaz_poll_write) ||
246 force_event == EVENT_OUTPUT))
249 (*p->fun)(p, EVENT_OUTPUT);
251 if (!p->destroyed && ((output_mask & yaz_poll_except) ||
252 force_event == EVENT_EXCEPT))
255 (*p->fun)(p, EVENT_EXCEPT);
257 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
258 p->max_idle) || force_event == EVENT_TIMEOUT))
261 (*p->fun)(p, EVENT_TIMEOUT);
266 for (p = *iochans; p; p = p->next)
268 int force_event = p->force_event;
271 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
272 force_event == EVENT_INPUT))
275 (*p->fun)(p, EVENT_INPUT);
277 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
278 force_event == EVENT_OUTPUT))
281 (*p->fun)(p, EVENT_OUTPUT);
283 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
284 force_event == EVENT_EXCEPT))
287 (*p->fun)(p, EVENT_EXCEPT);
289 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
290 p->max_idle) || force_event == EVENT_TIMEOUT))
293 (*p->fun)(p, EVENT_TIMEOUT);
297 for (p = *iochans; p; p = nextp)
305 /* We need to inform the threadlist that this channel has been destroyed */
308 /* Now reset the pointers */
313 for (pr = *iochans; pr; pr = pr->next)
316 assert(pr); /* grave error if it weren't there */
331 * indent-tabs-mode: nil
333 * vim: shiftwidth=4 tabstop=8 expandtab