2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: eventl.c,v 1.11 2007-01-03 08:42:15 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>
35 #include <sys/select.h>
38 #include <yaz/yconfig.h>
40 #include <yaz/comstack.h>
41 #include <yaz/xmalloc.h>
44 #include <yaz/statserv.h>
48 #define YAZ_EV_SELECT pth_select
52 #define YAZ_EV_SELECT select
55 static int log_level=0;
56 static int log_level_initialized=0;
58 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
62 if (!log_level_initialized)
64 log_level=yaz_log_module_level("eventl");
65 log_level_initialized=1;
68 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
70 new_iochan->destroyed = 0;
72 new_iochan->flags = flags;
74 new_iochan->force_event = 0;
75 new_iochan->last_event = new_iochan->max_idle = 0;
76 new_iochan->next = NULL;
77 new_iochan->chan_id = chan_id;
81 int iochan_is_alive(IOCHAN chan)
83 static struct timeval to;
84 fd_set in, out, except;
94 FD_SET(chan->fd, &in);
98 res = YAZ_EV_SELECT(max + 1, &in, 0, 0, &to);
101 if (!ir_read(chan, EVENT_INPUT))
106 int event_loop(IOCHAN *iochans)
108 do /* loop as long as there are active associations to process */
111 fd_set in, out, except;
113 static struct timeval to;
114 time_t now = time(0);
116 if (statserv_must_terminate())
118 for (p = *iochans; p; p = p->next)
119 p->force_event = EVENT_TIMEOUT;
127 for (p = *iochans; p; p = p->next)
130 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
131 p->fd, p->flags, p->force_event);
133 to.tv_sec = 0; /* polling select */
134 if (p->flags & EVENT_INPUT)
136 if (p->flags & EVENT_OUTPUT)
138 if (p->flags & EVENT_EXCEPT)
139 FD_SET(p->fd, &except);
142 if (p->max_idle && p->last_event)
144 ftime = p->last_event + p->max_idle;
153 yaz_log(log_level, "select start %ld", (long) to.tv_sec);
154 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, &to);
155 yaz_log(log_level, "select end");
158 if (yaz_errno() == EINTR)
160 if (statserv_must_terminate())
162 for (p = *iochans; p; p = p->next)
163 p->force_event = EVENT_TIMEOUT;
169 /* Destroy the first member in the chain, and try again */
170 association *assoc = (association *)iochan_getdata(*iochans);
171 COMSTACK conn = assoc->client_link;
174 destroy_association(assoc);
175 iochan_destroy(*iochans);
176 yaz_log(log_level, "error select, destroying iochan %p",
181 for (p = *iochans; p; p = p->next)
183 int force_event = p->force_event;
186 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
187 force_event == EVENT_INPUT))
190 (*p->fun)(p, EVENT_INPUT);
192 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
193 force_event == EVENT_OUTPUT))
196 (*p->fun)(p, EVENT_OUTPUT);
198 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
199 force_event == EVENT_EXCEPT))
202 (*p->fun)(p, EVENT_EXCEPT);
204 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
205 p->max_idle) || force_event == EVENT_TIMEOUT))
208 (*p->fun)(p, EVENT_TIMEOUT);
211 for (p = *iochans; p; p = nextp)
219 /* We need to inform the threadlist that this channel has been destroyed */
222 /* Now reset the pointers */
227 for (pr = *iochans; pr; pr = pr->next)
230 assert(pr); /* grave error if it weren't there */
245 * indent-tabs-mode: nil
247 * vim: shiftwidth=4 tabstop=8 expandtab