events: fix event type being propagated
[pazpar2-moved-to-github.git] / src / eventl.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2010 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 /*
21  * Based on  ParaZ - a simple tool for harvesting performance data for
22  * parallel operations using Z39.50.
23  * Copyright (C) 2006-2010 Index Data ApS
24  * See LICENSE file for details.
25  */
26
27 /*
28  * Based on revision YAZ' server/eventl.c 1.29.
29  */
30
31 #if HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <stdio.h>
36 #include <assert.h>
37
38 #ifdef WIN32
39 #include <winsock.h>
40 #else
41 #include <unistd.h>
42 #endif
43 #if HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <string.h>
50
51 #include <yaz/yconfig.h>
52 #include <yaz/log.h>
53 #include <yaz/comstack.h>
54 #include <yaz/xmalloc.h>
55 #include "eventl.h"
56 #include "sel_thread.h"
57
58 struct iochan_man_s {
59     IOCHAN channel_list;
60     sel_thread_t sel_thread;
61     int sel_fd;
62     int no_threads;
63     int log_level;
64 };
65
66 iochan_man_t iochan_man_create(int no_threads)
67 {
68     iochan_man_t man = xmalloc(sizeof(*man));
69     man->channel_list = 0;
70     man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
71     man->sel_fd = -1;
72     man->no_threads = no_threads;
73     man->log_level = YLOG_DEBUG;
74     return man;
75 }
76
77 void iochan_man_destroy(iochan_man_t *mp)
78 {
79     if (*mp)
80     {
81         if ((*mp)->sel_thread)
82             sel_thread_destroy((*mp)->sel_thread);
83         xfree(*mp);
84         *mp = 0;
85     }
86 }
87
88 void iochan_add(iochan_man_t man, IOCHAN chan)
89 {
90     chan->man = man;
91     chan->next = man->channel_list;
92     man->channel_list = chan;
93 }
94
95 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
96 {
97     IOCHAN new_iochan;
98
99     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
100         return 0;
101     new_iochan->destroyed = 0;
102     new_iochan->fd = fd;
103     new_iochan->flags = flags;
104     new_iochan->fun = cb;
105     new_iochan->socketfun = NULL;
106     new_iochan->maskfun = NULL;
107     new_iochan->force_event = 0;
108     new_iochan->last_event = new_iochan->max_idle = 0;
109     new_iochan->next = NULL;
110     new_iochan->man = 0;
111     new_iochan->thread_users = 0;
112     return new_iochan;
113 }
114
115 static void work_handler(void *work_data)
116 {
117     IOCHAN p = work_data;
118     if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
119         (*p->fun)(p, EVENT_TIMEOUT);
120     if (!p->destroyed && (p->this_event & EVENT_INPUT))
121         (*p->fun)(p, EVENT_INPUT);
122     if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
123         (*p->fun)(p, EVENT_OUTPUT);
124     if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
125         (*p->fun)(p, EVENT_EXCEPT);
126 }
127
128 static void run_fun(iochan_man_t man, IOCHAN p)
129 {
130     if (!p->destroyed && p->this_event)
131     {
132         if (man->sel_thread)
133         {
134             yaz_log(man->log_level, "eventl: add fun chan=%p event=%d",
135                     p, p->this_event);
136             p->thread_users++;
137             sel_thread_add(man->sel_thread, p);
138         }
139         else
140             work_handler(p);
141     }
142 }
143
144 static int event_loop(iochan_man_t man, IOCHAN *iochans)
145 {
146     do /* loop as long as there are active associations to process */
147     {
148         IOCHAN p, nextp;
149         fd_set in, out, except;
150         int res, max;
151         static struct timeval nullto = {0, 0}, to;
152         struct timeval *timeout;
153
154         FD_ZERO(&in);
155         FD_ZERO(&out);
156         FD_ZERO(&except);
157         timeout = &to; /* hang on select */
158         to.tv_sec = 15;
159         to.tv_usec = 0;
160         max = 0;
161         for (p = *iochans; p; p = p->next)
162         {
163             if (p->thread_users > 0)
164                 continue;
165             if (p->maskfun)
166                 p->flags = (*p->maskfun)(p);
167             if (p->socketfun)
168                 p->fd = (*p->socketfun)(p);
169             if (p->fd < 0)
170                 continue;
171             if (p->force_event)
172                 timeout = &nullto;        /* polling select */
173             if (p->flags & EVENT_INPUT)
174                 FD_SET(p->fd, &in);
175             if (p->flags & EVENT_OUTPUT)
176                 FD_SET(p->fd, &out);
177             if (p->flags & EVENT_EXCEPT)
178                 FD_SET(p->fd, &except);
179             if (p->fd > max)
180                 max = p->fd;
181             if (p->max_idle && p->max_idle < to.tv_sec)
182                 to.tv_sec = p->max_idle;
183         }
184         if (man->sel_fd != -1)
185         {
186             if (man->sel_fd > max)
187                 max = man->sel_fd;
188             yaz_log(man->log_level, "select on sel fd=%d", man->sel_fd);
189             FD_SET(man->sel_fd, &in);
190         }
191         yaz_log(man->log_level, "select begin");
192         res = select(max + 1, &in, &out, &except, timeout);
193         yaz_log(man->log_level, "select returned res=%d", res);
194         if (res < 0)
195         {
196             if (errno == EINTR)
197                 continue;
198             else
199             {
200                 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
201                 return 0;
202             }
203         }
204         if (man->sel_fd != -1)
205         {
206             if (FD_ISSET(man->sel_fd, &in))
207             {
208                 IOCHAN chan;
209
210                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
211                         man->sel_fd);
212                 while ((chan = sel_thread_result(man->sel_thread)))
213                 {
214                     yaz_log(man->log_level, "eventl: got thread result p=%p",
215                             chan);
216                     chan->thread_users--;
217                 }
218             }
219         }
220         for (p = *iochans; p; p = p->next)
221         {
222             int force_event = p->force_event;
223             time_t now = time(0);
224             
225             if (p->thread_users > 0)
226             {
227                 yaz_log(man->log_level, "eventl: skip chan=%p users=%d", p, p->thread_users);
228                 continue;
229             }
230             p->this_event = 0;
231             p->force_event = 0;
232             if (!p->destroyed && ((p->max_idle && now - p->last_event >
233                                    p->max_idle) || force_event == EVENT_TIMEOUT))
234             {
235                 p->last_event = now;
236                 p->this_event |= EVENT_TIMEOUT;
237             }
238             if (p->fd >= 0)
239             {
240                 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
241                                       force_event == EVENT_INPUT))
242                 {
243                     p->last_event = now;
244                     yaz_log(YLOG_DEBUG, "Eventl input event");
245                     p->this_event |= EVENT_INPUT;
246                 }
247                 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
248                                       force_event == EVENT_OUTPUT))
249                 {
250                     p->last_event = now;
251                     yaz_log(YLOG_DEBUG, "Eventl output event");
252                     p->this_event |= EVENT_OUTPUT;
253                 }
254                 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
255                                       force_event == EVENT_EXCEPT))
256                 {
257                     p->last_event = now;
258                     p->this_event |= EVENT_EXCEPT;
259                 }
260             }
261             run_fun(man, p);
262         }
263         for (p = *iochans; p; p = nextp)
264         {
265             nextp = p->next;
266
267             if (p->destroyed && p->thread_users == 0)
268             {
269                 IOCHAN tmp = p, pr;
270
271                 /* Now reset the pointers */
272                 if (p == *iochans)
273                     *iochans = p->next;
274                 else
275                 {
276                     for (pr = *iochans; pr; pr = pr->next)
277                         if (pr->next == p)
278                             break;
279                     assert(pr); /* grave error if it weren't there */
280                     pr->next = p->next;
281                 }
282                 if (nextp == p)
283                     nextp = p->next;
284                 xfree(tmp);
285             }
286         }
287     }
288     while (*iochans);
289     return 0;
290 }
291
292 void iochan_man_events(iochan_man_t man)
293 {
294     if (man->no_threads > 0 && !man->sel_thread)
295     {
296         man->sel_thread = sel_thread_create(
297             work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
298         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
299                 man->no_threads);
300     }
301     event_loop(man, &man->channel_list);
302 }
303
304 /*
305  * Local variables:
306  * c-basic-offset: 4
307  * c-file-style: "Stroustrup"
308  * indent-tabs-mode: nil
309  * End:
310  * vim: shiftwidth=4 tabstop=8 expandtab
311  */
312