Event manager do not produce log output by default
[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     (*p->fun)(p, p->this_event);
119 }
120
121 static void run_fun(iochan_man_t man, IOCHAN p, int event)
122 {
123     if (!p->destroyed)
124     {
125         p->this_event = event;
126         if (man->sel_thread)
127         {
128             yaz_log(man->log_level, "eventl: add fun chan=%p event=%d",
129                     p, event);
130             p->thread_users++;
131             sel_thread_add(man->sel_thread, p);
132         }
133         else
134             (*p->fun)(p, p->this_event);
135     }
136 }
137
138 static int event_loop(iochan_man_t man, IOCHAN *iochans)
139 {
140     do /* loop as long as there are active associations to process */
141     {
142         IOCHAN p, nextp;
143         fd_set in, out, except;
144         int res, max;
145         static struct timeval nullto = {0, 0}, to;
146         struct timeval *timeout;
147
148         FD_ZERO(&in);
149         FD_ZERO(&out);
150         FD_ZERO(&except);
151         timeout = &to; /* hang on select */
152         to.tv_sec = 15;
153         to.tv_usec = 0;
154         max = 0;
155         for (p = *iochans; p; p = p->next)
156         {
157             if (p->thread_users > 0)
158                 continue;
159             if (p->maskfun)
160                 p->flags = (*p->maskfun)(p);
161             if (p->socketfun)
162                 p->fd = (*p->socketfun)(p);
163             if (p->fd < 0)
164                 continue;
165             if (p->force_event)
166                 timeout = &nullto;        /* polling select */
167             if (p->flags & EVENT_INPUT)
168                 FD_SET(p->fd, &in);
169             if (p->flags & EVENT_OUTPUT)
170                 FD_SET(p->fd, &out);
171             if (p->flags & EVENT_EXCEPT)
172                 FD_SET(p->fd, &except);
173             if (p->fd > max)
174                 max = p->fd;
175             if (p->max_idle && p->max_idle < to.tv_sec)
176                 to.tv_sec = p->max_idle;
177         }
178         if (man->sel_fd != -1)
179         {
180             if (man->sel_fd > max)
181                 max = man->sel_fd;
182             yaz_log(man->log_level, "select on sel fd=%d", man->sel_fd);
183             FD_SET(man->sel_fd, &in);
184         }
185         yaz_log(man->log_level, "select begin");
186         res = select(max + 1, &in, &out, &except, timeout);
187         yaz_log(man->log_level, "select returned res=%d", res);
188         if (res < 0)
189         {
190             if (errno == EINTR)
191                 continue;
192             else
193             {
194                 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
195                 return 0;
196             }
197         }
198         if (man->sel_fd != -1)
199         {
200             if (FD_ISSET(man->sel_fd, &in))
201             {
202                 IOCHAN chan;
203
204                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
205                         man->sel_fd);
206                 while ((chan = sel_thread_result(man->sel_thread)))
207                 {
208                     yaz_log(man->log_level, "eventl: got thread result p=%p",
209                             chan);
210                     chan->thread_users--;
211                 }
212             }
213         }
214         for (p = *iochans; p; p = p->next)
215         {
216             int force_event = p->force_event;
217             time_t now = time(0);
218
219             p->force_event = 0;
220             if (!p->destroyed && ((p->max_idle && now - p->last_event >
221                 p->max_idle) || force_event == EVENT_TIMEOUT))
222             {
223                 p->last_event = now;
224                 run_fun(man, p, EVENT_TIMEOUT);
225             }
226             if (p->fd < 0)
227                 continue;
228             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
229                 force_event == EVENT_INPUT))
230             {
231                 p->last_event = now;
232                 yaz_log(YLOG_DEBUG, "Eventl input event");
233                 run_fun(man, p, EVENT_INPUT);
234             }
235             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
236                 force_event == EVENT_OUTPUT))
237             {
238                 p->last_event = now;
239                 yaz_log(YLOG_DEBUG, "Eventl output event");
240                 run_fun(man, p, EVENT_OUTPUT);
241             }
242             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
243                 force_event == EVENT_EXCEPT))
244             {
245                 p->last_event = now;
246                 run_fun(man, p, EVENT_EXCEPT);
247             }
248         }
249         for (p = *iochans; p; p = nextp)
250         {
251             nextp = p->next;
252
253             if (p->destroyed)
254             {
255                 IOCHAN tmp = p, pr;
256
257                 /* Now reset the pointers */
258                 if (p == *iochans)
259                     *iochans = p->next;
260                 else
261                 {
262                     for (pr = *iochans; pr; pr = pr->next)
263                         if (pr->next == p)
264                             break;
265                     assert(pr); /* grave error if it weren't there */
266                     pr->next = p->next;
267                 }
268                 if (nextp == p)
269                     nextp = p->next;
270                 xfree(tmp);
271             }
272         }
273     }
274     while (*iochans);
275     return 0;
276 }
277
278 void iochan_man_events(iochan_man_t man)
279 {
280     if (man->no_threads > 0 && !man->sel_thread)
281     {
282         man->sel_thread = sel_thread_create(
283             work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
284         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
285                 man->no_threads);
286     }
287     event_loop(man, &man->channel_list);
288 }
289
290 /*
291  * Local variables:
292  * c-basic-offset: 4
293  * c-file-style: "Stroustrup"
294  * indent-tabs-mode: nil
295  * End:
296  * vim: shiftwidth=4 tabstop=8 expandtab
297  */
298