2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: eventl.c,v 1.5 2005-01-15 19:47:13 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).
27 #include <yaz/yconfig.h>
29 #include <yaz/comstack.h>
30 #include <yaz/xmalloc.h>
33 #include <yaz/statserv.h>
37 #define YAZ_EV_SELECT pth_select
41 #define YAZ_EV_SELECT select
44 static int log_level=0;
45 static int log_level_initialized=0;
47 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
51 if (!log_level_initialized)
53 log_level=yaz_log_module_level("eventl");
54 log_level_initialized=1;
57 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
59 new_iochan->destroyed = 0;
61 new_iochan->flags = flags;
63 new_iochan->force_event = 0;
64 new_iochan->last_event = new_iochan->max_idle = 0;
65 new_iochan->next = NULL;
69 int event_loop(IOCHAN *iochans)
71 do /* loop as long as there are active associations to process */
74 fd_set in, out, except;
76 static struct timeval to;
79 if (statserv_must_terminate())
81 for (p = *iochans; p; p = p->next)
82 p->force_event = EVENT_TIMEOUT;
90 for (p = *iochans; p; p = p->next)
93 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
94 p->fd, p->flags, p->force_event);
96 to.tv_sec = 0; /* polling select */
97 if (p->flags & EVENT_INPUT)
99 if (p->flags & EVENT_OUTPUT)
101 if (p->flags & EVENT_EXCEPT)
102 FD_SET(p->fd, &except);
105 if (p->max_idle && p->last_event)
107 ftime = p->last_event + p->max_idle;
116 yaz_log(log_level, "select start %ld", (long) to.tv_sec);
117 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, &to);
118 yaz_log(log_level, "select end");
121 if (yaz_errno() == EINTR)
123 if (statserv_must_terminate())
125 for (p = *iochans; p; p = p->next)
126 p->force_event = EVENT_TIMEOUT;
132 /* Destroy the first member in the chain, and try again */
133 association *assoc = (association *)iochan_getdata(*iochans);
134 COMSTACK conn = assoc->client_link;
137 destroy_association(assoc);
138 iochan_destroy(*iochans);
139 yaz_log(log_level, "error select, destroying iochan %p",
144 for (p = *iochans; p; p = p->next)
146 int force_event = p->force_event;
149 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
150 force_event == EVENT_INPUT))
153 (*p->fun)(p, EVENT_INPUT);
155 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
156 force_event == EVENT_OUTPUT))
159 (*p->fun)(p, EVENT_OUTPUT);
161 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
162 force_event == EVENT_EXCEPT))
165 (*p->fun)(p, EVENT_EXCEPT);
167 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
168 p->max_idle) || force_event == EVENT_TIMEOUT))
171 (*p->fun)(p, EVENT_TIMEOUT);
174 for (p = *iochans; p; p = nextp)
182 /* We need to inform the threadlist that this channel has been destroyed */
185 /* Now reset the pointers */
190 for (pr = *iochans; pr; pr = pr->next)
193 assert(pr); /* grave error if it weren't there */