YAZ GFS uses yaz_poll.
[yaz-moved-to-github.git] / src / eventl.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: eventl.c,v 1.12 2007-11-09 18:47:50 adam Exp $
6  */
7
8 /**
9  * \file eventl.c
10  * \brief Implements event loop handling for GFS.
11  *
12  * This source implements the main event loop for the Generic Frontend
13  * Server. It uses select(2).
14  */
15
16 #include <assert.h>
17 #include <errno.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25 #if HAVE_SYS_TIME_H
26 #include <sys/time.h>
27 #endif
28
29 #define NPOLL 1
30
31 #if NPOLL
32 #include <yaz/poll.h>
33 #else
34
35 #ifdef WIN32
36 #include <winsock.h>
37 #endif
38 #if HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41 #if HAVE_SYS_SELECT_H
42 #include <sys/select.h>
43 #endif
44 #endif
45
46 #include <yaz/log.h>
47 #include <yaz/comstack.h>
48 #include <yaz/xmalloc.h>
49 #include "eventl.h"
50 #include "session.h"
51 #include <yaz/statserv.h>
52
53 #if YAZ_GNU_THREADS
54 #include <pth.h>
55 #define YAZ_EV_SELECT pth_select
56 #endif
57
58 #ifndef YAZ_EV_SELECT
59 #define YAZ_EV_SELECT select
60 #endif
61
62 static int log_level=0;
63 static int log_level_initialized=0;
64
65 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, int chan_id)
66 {
67     IOCHAN new_iochan;
68
69     if (!log_level_initialized)
70     {
71         log_level=yaz_log_module_level("eventl");
72         log_level_initialized=1;
73     }
74
75     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
76         return 0;
77     new_iochan->destroyed = 0;
78     new_iochan->fd = fd;
79     new_iochan->flags = flags;
80     new_iochan->fun = cb;
81     new_iochan->force_event = 0;
82     new_iochan->last_event = new_iochan->max_idle = 0;
83     new_iochan->next = NULL;
84     new_iochan->chan_id = chan_id;
85     return new_iochan;
86 }
87
88
89 int iochan_is_alive(IOCHAN chan)
90 {
91     static struct timeval to;
92     fd_set in, out, except;
93     int res, max;
94
95     to.tv_sec = 0;
96     to.tv_usec = 0;
97
98     FD_ZERO(&in);
99     FD_ZERO(&out);
100     FD_ZERO(&except);
101
102     FD_SET(chan->fd, &in);
103
104     max = chan->fd + 1;
105
106     res = YAZ_EV_SELECT(max + 1, &in, 0, 0, &to);
107     if (res == 0)
108         return 1;
109     if (!ir_read(chan, EVENT_INPUT))
110         return 0;
111     return 1;
112 }
113
114 int event_loop(IOCHAN *iochans)
115 {
116     do /* loop as long as there are active associations to process */
117     {
118         IOCHAN p, nextp;
119 #if NPOLL
120         int i;
121         int tv_sec = 3600;
122         int no_fds = 0;
123         struct yaz_poll_fd *fds = 0;
124 #else
125         fd_set in, out, except;
126         static struct timeval to;
127         int max;
128 #endif
129         int res;
130         time_t now = time(0);
131
132         if (statserv_must_terminate())
133         {
134             for (p = *iochans; p; p = p->next)
135                 p->force_event = EVENT_TIMEOUT;
136         }
137 #if NPOLL
138         for (p = *iochans; p; p = p->next)
139             no_fds++;
140         fds = xmalloc(no_fds * sizeof(*fds));
141         for (i = 0, p = *iochans; p; p = p->next, i++)
142         {
143             time_t w, ftime;
144             enum yaz_poll_mask input_mask = 0;
145             yaz_log(log_level, "fd=%d flags=%d force_event=%d",
146                     p->fd, p->flags, p->force_event);
147             if (p->force_event)
148                 tv_sec = 0;          /* polling select */
149             if (p->flags & EVENT_INPUT)
150                 input_mask += yaz_poll_read;
151             if (p->flags & EVENT_OUTPUT)
152                 input_mask += yaz_poll_write;
153             if (p->flags & EVENT_EXCEPT)
154                 input_mask += yaz_poll_except;
155             if (p->max_idle && p->last_event)
156             {
157                 ftime = p->last_event + p->max_idle;
158                 if (ftime < now)
159                     w = p->max_idle;
160                 else
161                     w = ftime - now;
162                 if (w < tv_sec)
163                     tv_sec = w;
164             }
165             fds[i].fd = p->fd;
166             fds[i].input_mask = input_mask;
167         }
168         res = yaz_poll(fds, no_fds, tv_sec);
169 #else
170         FD_ZERO(&in);
171         FD_ZERO(&out);
172         FD_ZERO(&except);
173         to.tv_sec = 3600;
174         to.tv_usec = 0;
175         max = 0;
176         for (p = *iochans; p; p = p->next)
177         {
178             time_t w, ftime;
179             yaz_log(log_level, "fd=%d flags=%d force_event=%d",
180                     p->fd, p->flags, p->force_event);
181             if (p->force_event)
182                 to.tv_sec = 0;          /* polling select */
183             if (p->flags & EVENT_INPUT)
184                 FD_SET(p->fd, &in);
185             if (p->flags & EVENT_OUTPUT)
186                 FD_SET(p->fd, &out);
187             if (p->flags & EVENT_EXCEPT)
188                 FD_SET(p->fd, &except);
189             if (p->fd > max)
190                 max = p->fd;
191             if (p->max_idle && p->last_event)
192             {
193                 ftime = p->last_event + p->max_idle;
194                 if (ftime < now)
195                     w = p->max_idle;
196                 else
197                     w = ftime - now;
198                 if (w < to.tv_sec)
199                     to.tv_sec = w;
200             }
201         }
202         yaz_log(log_level, "select start %ld", (long) to.tv_sec);
203         res = YAZ_EV_SELECT(max + 1, &in, &out, &except, &to);
204         yaz_log(log_level, "select end");
205 #endif
206         if (res < 0)
207         {
208             if (yaz_errno() == EINTR)
209             {
210                 if (statserv_must_terminate())
211                 {
212                     for (p = *iochans; p; p = p->next)
213                         p->force_event = EVENT_TIMEOUT;
214                 }
215                 xfree(fds);
216                 continue;
217             }
218             else
219             {
220                 /* Destroy the first member in the chain, and try again */
221                 association *assoc = (association *)iochan_getdata(*iochans);
222                 COMSTACK conn = assoc->client_link;
223
224                 cs_close(conn);
225                 destroy_association(assoc);
226                 iochan_destroy(*iochans);
227                 yaz_log(log_level, "error select, destroying iochan %p",
228                         *iochans);
229             }
230         }
231         now = time(0);
232 #if NPOLL
233         for (i = 0, p = *iochans; p; p = p->next, i++)
234         {
235             int force_event = p->force_event;
236             enum yaz_poll_mask output_mask = fds[i].output_mask;
237
238             p->force_event = 0;
239             if (!p->destroyed && ((output_mask & yaz_poll_read) ||
240                                   force_event == EVENT_INPUT))
241             {
242                 p->last_event = now;
243                 (*p->fun)(p, EVENT_INPUT);
244             }
245             if (!p->destroyed && ((output_mask & yaz_poll_write) ||
246                                   force_event == EVENT_OUTPUT))
247             {
248                 p->last_event = now;
249                 (*p->fun)(p, EVENT_OUTPUT);
250             }
251             if (!p->destroyed && ((output_mask & yaz_poll_except) ||
252                 force_event == EVENT_EXCEPT))
253             {
254                 p->last_event = now;
255                 (*p->fun)(p, EVENT_EXCEPT);
256             }
257             if (!p->destroyed && ((p->max_idle && now - p->last_event >=
258                 p->max_idle) || force_event == EVENT_TIMEOUT))
259             {
260                 p->last_event = now;
261                 (*p->fun)(p, EVENT_TIMEOUT);
262             }
263         }
264         xfree(fds);
265 #else
266         for (p = *iochans; p; p = p->next)
267         {
268             int force_event = p->force_event;
269
270             p->force_event = 0;
271             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
272                 force_event == EVENT_INPUT))
273             {
274                 p->last_event = now;
275                 (*p->fun)(p, EVENT_INPUT);
276             }
277             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
278                 force_event == EVENT_OUTPUT))
279             {
280                 p->last_event = now;
281                 (*p->fun)(p, EVENT_OUTPUT);
282             }
283             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
284                 force_event == EVENT_EXCEPT))
285             {
286                 p->last_event = now;
287                 (*p->fun)(p, EVENT_EXCEPT);
288             }
289             if (!p->destroyed && ((p->max_idle && now - p->last_event >=
290                 p->max_idle) || force_event == EVENT_TIMEOUT))
291             {
292                 p->last_event = now;
293                 (*p->fun)(p, EVENT_TIMEOUT);
294             }
295         }
296 #endif
297         for (p = *iochans; p; p = nextp)
298         {
299             nextp = p->next;
300
301             if (p->destroyed)
302             {
303                 IOCHAN tmp = p, pr;
304
305                 /* We need to inform the threadlist that this channel has been destroyed */
306                 statserv_remove(p);
307
308                 /* Now reset the pointers */
309                 if (p == *iochans)
310                     *iochans = p->next;
311                 else
312                 {
313                     for (pr = *iochans; pr; pr = pr->next)
314                         if (pr->next == p)
315                             break;
316                     assert(pr); /* grave error if it weren't there */
317                     pr->next = p->next;
318                 }
319                 if (nextp == p)
320                     nextp = p->next;
321                 xfree(tmp);
322             }
323         }
324     }
325     while (*iochans);
326     return 0;
327 }
328 /*
329  * Local variables:
330  * c-basic-offset: 4
331  * indent-tabs-mode: nil
332  * End:
333  * vim: shiftwidth=4 tabstop=8 expandtab
334  */
335