1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements event loop handling for GFS.
9 * 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>
34 #include <yaz/errno.h>
37 #include <yaz/statserv.h>
39 static int log_level=0;
40 static int log_level_initialized=0;
42 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
46 if (!log_level_initialized)
48 log_level=yaz_log_module_level("eventl");
49 log_level_initialized=1;
52 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
54 new_iochan->destroyed = 0;
56 new_iochan->flags = flags;
58 new_iochan->force_event = 0;
59 new_iochan->last_event = new_iochan->max_idle = 0;
60 new_iochan->next = NULL;
61 new_iochan->chan_id = chan_id;
66 int iochan_is_alive(IOCHAN chan)
68 struct yaz_poll_fd fds;
72 fds.input_mask = yaz_poll_read;
73 res = yaz_poll(&fds, 1, 0, 0);
76 if (!ir_read(chan, EVENT_INPUT))
81 int iochan_event_loop(IOCHAN *iochans)
83 do /* loop as long as there are active associations to process */
89 struct yaz_poll_fd *fds = 0;
93 for (p = *iochans; p; p = p->next)
95 fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
96 for (i = 0, p = *iochans; p; p = p->next, i++)
99 enum yaz_poll_mask input_mask = yaz_poll_none;
100 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
101 p->fd, p->flags, p->force_event);
103 tv_sec = 0; /* polling select */
104 if (p->flags & EVENT_INPUT)
105 yaz_poll_add(input_mask, yaz_poll_read);
106 if (p->flags & EVENT_OUTPUT)
107 yaz_poll_add(input_mask, yaz_poll_write);
108 if (p->flags & EVENT_EXCEPT)
109 yaz_poll_add(input_mask, yaz_poll_except);
110 if (p->max_idle && p->last_event)
112 ftime = p->last_event + p->max_idle;
117 /* tv_sec will be minimum wait.. */
119 tv_sec = (int) w; /* can hold it because w < tv_sec */
122 fds[i].input_mask = input_mask;
124 res = yaz_poll(fds, no_fds, tv_sec, 0);
127 if (yaz_errno() == EINTR)
134 yaz_log(YLOG_WARN|YLOG_ERRNO, "yaz_poll");
140 for (i = 0, p = *iochans; p; p = p->next, i++)
142 int force_event = p->force_event;
143 enum yaz_poll_mask output_mask = fds[i].output_mask;
146 if (!p->destroyed && ((output_mask & yaz_poll_read) ||
147 force_event == EVENT_INPUT))
150 (*p->fun)(p, EVENT_INPUT);
152 if (!p->destroyed && ((output_mask & yaz_poll_write) ||
153 force_event == EVENT_OUTPUT))
156 (*p->fun)(p, EVENT_OUTPUT);
158 if (!p->destroyed && ((output_mask & yaz_poll_except) ||
159 force_event == EVENT_EXCEPT))
162 (*p->fun)(p, EVENT_EXCEPT);
164 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
165 p->max_idle) || force_event == EVENT_TIMEOUT))
168 (*p->fun)(p, EVENT_TIMEOUT);
172 for (p = *iochans; p; p = nextp)
180 /* We need to inform the threadlist that this channel has been destroyed */
183 /* Now reset the pointers */
188 for (pr = *iochans; pr; pr = pr->next)
191 assert(pr); /* grave error if it weren't there */
206 * c-file-style: "Stroustrup"
207 * indent-tabs-mode: nil
209 * vim: shiftwidth=4 tabstop=8 expandtab