1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements event loop handling for GFS.
10 * This source implements the main event loop for the Generic Frontend
21 #include <sys/types.h>
30 #include <yaz/comstack.h>
31 #include <yaz/xmalloc.h>
34 #include <yaz/statserv.h>
36 static int log_level=0;
37 static int log_level_initialized=0;
39 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
43 if (!log_level_initialized)
45 log_level=yaz_log_module_level("eventl");
46 log_level_initialized=1;
49 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
51 new_iochan->destroyed = 0;
53 new_iochan->flags = flags;
55 new_iochan->force_event = 0;
56 new_iochan->last_event = new_iochan->max_idle = 0;
57 new_iochan->next = NULL;
58 new_iochan->chan_id = chan_id;
63 int iochan_is_alive(IOCHAN chan)
65 struct yaz_poll_fd fds;
69 fds.input_mask = yaz_poll_read;
70 res = yaz_poll(&fds, 1, 0, 0);
73 if (!ir_read(chan, EVENT_INPUT))
78 int iochan_event_loop(IOCHAN *iochans)
80 do /* loop as long as there are active associations to process */
86 struct yaz_poll_fd *fds = 0;
90 if (statserv_must_terminate())
92 for (p = *iochans; p; p = p->next)
93 p->force_event = EVENT_TIMEOUT;
95 for (p = *iochans; p; p = p->next)
97 fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
98 for (i = 0, p = *iochans; p; p = p->next, i++)
101 enum yaz_poll_mask input_mask = yaz_poll_none;
102 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
103 p->fd, p->flags, p->force_event);
105 tv_sec = 0; /* polling select */
106 if (p->flags & EVENT_INPUT)
107 yaz_poll_add(input_mask, yaz_poll_read);
108 if (p->flags & EVENT_OUTPUT)
109 yaz_poll_add(input_mask, yaz_poll_write);
110 if (p->flags & EVENT_EXCEPT)
111 yaz_poll_add(input_mask, yaz_poll_except);
112 if (p->max_idle && p->last_event)
114 ftime = p->last_event + p->max_idle;
123 fds[i].input_mask = input_mask;
125 res = yaz_poll(fds, no_fds, tv_sec, 0);
128 if (yaz_errno() == EINTR)
130 if (statserv_must_terminate())
132 for (p = *iochans; p; p = p->next)
133 p->force_event = EVENT_TIMEOUT;
140 /* Destroy the first member in the chain, and try again */
141 association *assoc = (association *)iochan_getdata(*iochans);
142 COMSTACK conn = assoc->client_link;
145 destroy_association(assoc);
146 iochan_destroy(*iochans);
147 yaz_log(log_level, "error select, destroying iochan %p",
152 for (i = 0, p = *iochans; p; p = p->next, i++)
154 int force_event = p->force_event;
155 enum yaz_poll_mask output_mask = fds[i].output_mask;
158 if (!p->destroyed && ((output_mask & yaz_poll_read) ||
159 force_event == EVENT_INPUT))
162 (*p->fun)(p, EVENT_INPUT);
164 if (!p->destroyed && ((output_mask & yaz_poll_write) ||
165 force_event == EVENT_OUTPUT))
168 (*p->fun)(p, EVENT_OUTPUT);
170 if (!p->destroyed && ((output_mask & yaz_poll_except) ||
171 force_event == EVENT_EXCEPT))
174 (*p->fun)(p, EVENT_EXCEPT);
176 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
177 p->max_idle) || force_event == EVENT_TIMEOUT))
180 (*p->fun)(p, EVENT_TIMEOUT);
184 for (p = *iochans; p; p = nextp)
192 /* We need to inform the threadlist that this channel has been destroyed */
195 /* Now reset the pointers */
200 for (pr = *iochans; pr; pr = pr->next)
203 assert(pr); /* grave error if it weren't there */
218 * indent-tabs-mode: nil
220 * vim: shiftwidth=4 tabstop=8 expandtab