2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: eventl.c,v 1.16 2007-11-12 08:41:56 adam Exp $
10 * \brief Implements event loop handling for GFS.
12 * This source implements the main event loop for the Generic Frontend
23 #include <sys/types.h>
32 #include <yaz/comstack.h>
33 #include <yaz/xmalloc.h>
36 #include <yaz/statserv.h>
38 static int log_level=0;
39 static int log_level_initialized=0;
41 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
45 if (!log_level_initialized)
47 log_level=yaz_log_module_level("eventl");
48 log_level_initialized=1;
51 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
53 new_iochan->destroyed = 0;
55 new_iochan->flags = flags;
57 new_iochan->force_event = 0;
58 new_iochan->last_event = new_iochan->max_idle = 0;
59 new_iochan->next = NULL;
60 new_iochan->chan_id = chan_id;
65 int iochan_is_alive(IOCHAN chan)
67 struct yaz_poll_fd fds;
71 fds.input_mask = yaz_poll_read;
72 res = yaz_poll(&fds, 1, 0, 0);
75 if (!ir_read(chan, EVENT_INPUT))
80 int iochan_event_loop(IOCHAN *iochans)
82 do /* loop as long as there are active associations to process */
88 struct yaz_poll_fd *fds = 0;
92 if (statserv_must_terminate())
94 for (p = *iochans; p; p = p->next)
95 p->force_event = EVENT_TIMEOUT;
97 for (p = *iochans; p; p = p->next)
99 fds = xmalloc(no_fds * sizeof(*fds));
100 for (i = 0, p = *iochans; p; p = p->next, i++)
103 enum yaz_poll_mask input_mask = 0;
104 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
105 p->fd, p->flags, p->force_event);
107 tv_sec = 0; /* polling select */
108 if (p->flags & EVENT_INPUT)
109 input_mask += yaz_poll_read;
110 if (p->flags & EVENT_OUTPUT)
111 input_mask += yaz_poll_write;
112 if (p->flags & EVENT_EXCEPT)
113 input_mask += yaz_poll_except;
114 if (p->max_idle && p->last_event)
116 ftime = p->last_event + p->max_idle;
125 fds[i].input_mask = input_mask;
127 res = yaz_poll(fds, no_fds, tv_sec, 0);
130 if (yaz_errno() == EINTR)
132 if (statserv_must_terminate())
134 for (p = *iochans; p; p = p->next)
135 p->force_event = EVENT_TIMEOUT;
142 /* Destroy the first member in the chain, and try again */
143 association *assoc = (association *)iochan_getdata(*iochans);
144 COMSTACK conn = assoc->client_link;
147 destroy_association(assoc);
148 iochan_destroy(*iochans);
149 yaz_log(log_level, "error select, destroying iochan %p",
154 for (i = 0, p = *iochans; p; p = p->next, i++)
156 int force_event = p->force_event;
157 enum yaz_poll_mask output_mask = fds[i].output_mask;
160 if (!p->destroyed && ((output_mask & yaz_poll_read) ||
161 force_event == EVENT_INPUT))
164 (*p->fun)(p, EVENT_INPUT);
166 if (!p->destroyed && ((output_mask & yaz_poll_write) ||
167 force_event == EVENT_OUTPUT))
170 (*p->fun)(p, EVENT_OUTPUT);
172 if (!p->destroyed && ((output_mask & yaz_poll_except) ||
173 force_event == EVENT_EXCEPT))
176 (*p->fun)(p, EVENT_EXCEPT);
178 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
179 p->max_idle) || force_event == EVENT_TIMEOUT))
182 (*p->fun)(p, EVENT_TIMEOUT);
186 for (p = *iochans; p; p = nextp)
194 /* We need to inform the threadlist that this channel has been destroyed */
197 /* Now reset the pointers */
202 for (pr = *iochans; pr; pr = pr->next)
205 assert(pr); /* grave error if it weren't there */
220 * indent-tabs-mode: nil
222 * vim: shiftwidth=4 tabstop=8 expandtab