1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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, int *watch_sig)
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)
130 if (watch_sig && *watch_sig)
136 yaz_log(YLOG_WARN|YLOG_ERRNO, "yaz_poll");
142 for (i = 0, p = *iochans; p; p = p->next, i++)
144 int force_event = p->force_event;
145 enum yaz_poll_mask output_mask = fds[i].output_mask;
148 if (!p->destroyed && ((output_mask & yaz_poll_read) ||
149 force_event == EVENT_INPUT))
152 (*p->fun)(p, EVENT_INPUT);
154 if (!p->destroyed && ((output_mask & yaz_poll_write) ||
155 force_event == EVENT_OUTPUT))
158 (*p->fun)(p, EVENT_OUTPUT);
160 if (!p->destroyed && ((output_mask & yaz_poll_except) ||
161 force_event == EVENT_EXCEPT))
164 (*p->fun)(p, EVENT_EXCEPT);
166 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
167 p->max_idle) || force_event == EVENT_TIMEOUT))
170 (*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 */
208 * c-file-style: "Stroustrup"
209 * indent-tabs-mode: nil
211 * vim: shiftwidth=4 tabstop=8 expandtab