1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 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>
32 #include <yaz/errno.h>
35 #include <yaz/statserv.h>
37 static int log_level=0;
38 static int log_level_initialized=0;
40 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
44 if (!log_level_initialized)
46 log_level=yaz_log_module_level("eventl");
47 log_level_initialized=1;
50 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
52 new_iochan->destroyed = 0;
54 new_iochan->flags = flags;
56 new_iochan->force_event = 0;
57 new_iochan->last_event = new_iochan->max_idle = 0;
58 new_iochan->next = NULL;
59 new_iochan->chan_id = chan_id;
64 int iochan_is_alive(IOCHAN chan)
66 struct yaz_poll_fd fds;
70 fds.input_mask = yaz_poll_read;
71 res = yaz_poll(&fds, 1, 0, 0);
74 if (!ir_read(chan, EVENT_INPUT))
79 int iochan_event_loop(IOCHAN *iochans)
81 do /* loop as long as there are active associations to process */
87 struct yaz_poll_fd *fds = 0;
91 if (statserv_must_terminate())
93 for (p = *iochans; p; p = p->next)
94 p->force_event = EVENT_TIMEOUT;
96 for (p = *iochans; p; p = p->next)
98 fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
99 for (i = 0, p = *iochans; p; p = p->next, i++)
102 enum yaz_poll_mask input_mask = yaz_poll_none;
103 yaz_log(log_level, "fd=%d flags=%d force_event=%d",
104 p->fd, p->flags, p->force_event);
106 tv_sec = 0; /* polling select */
107 if (p->flags & EVENT_INPUT)
108 yaz_poll_add(input_mask, yaz_poll_read);
109 if (p->flags & EVENT_OUTPUT)
110 yaz_poll_add(input_mask, yaz_poll_write);
111 if (p->flags & EVENT_EXCEPT)
112 yaz_poll_add(input_mask, yaz_poll_except);
113 if (p->max_idle && p->last_event)
115 ftime = p->last_event + p->max_idle;
124 fds[i].input_mask = input_mask;
126 res = yaz_poll(fds, no_fds, tv_sec, 0);
129 if (yaz_errno() == EINTR)
131 if (statserv_must_terminate())
133 for (p = *iochans; p; p = p->next)
134 p->force_event = EVENT_TIMEOUT;
141 yaz_log(YLOG_WARN|YLOG_ERRNO, "yaz_poll");
147 for (i = 0, p = *iochans; p; p = p->next, i++)
149 int force_event = p->force_event;
150 enum yaz_poll_mask output_mask = fds[i].output_mask;
153 if (!p->destroyed && ((output_mask & yaz_poll_read) ||
154 force_event == EVENT_INPUT))
157 (*p->fun)(p, EVENT_INPUT);
159 if (!p->destroyed && ((output_mask & yaz_poll_write) ||
160 force_event == EVENT_OUTPUT))
163 (*p->fun)(p, EVENT_OUTPUT);
165 if (!p->destroyed && ((output_mask & yaz_poll_except) ||
166 force_event == EVENT_EXCEPT))
169 (*p->fun)(p, EVENT_EXCEPT);
171 if (!p->destroyed && ((p->max_idle && now - p->last_event >=
172 p->max_idle) || force_event == EVENT_TIMEOUT))
175 (*p->fun)(p, EVENT_TIMEOUT);
179 for (p = *iochans; p; p = nextp)
187 /* We need to inform the threadlist that this channel has been destroyed */
190 /* Now reset the pointers */
195 for (pr = *iochans; pr; pr = pr->next)
198 assert(pr); /* grave error if it weren't there */
213 * c-file-style: "Stroustrup"
214 * indent-tabs-mode: nil
216 * vim: shiftwidth=4 tabstop=8 expandtab