Logging channels and mutexes + a few other things
[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         IOCHAN c;
82         if ((*mp)->sel_thread)
83             sel_thread_destroy((*mp)->sel_thread);
84         
85         c = (*mp)->channel_list;
86         while (c)
87         {
88             IOCHAN c_next = c->next;
89             xfree(c->name);
90             xfree(c);
91             c = c_next;
92         }
93         xfree(*mp);
94         *mp = 0;
95     }
96 }
97
98 void iochan_add(iochan_man_t man, IOCHAN chan)
99 {
100     chan->man = man;
101     chan->next = man->channel_list;
102     man->channel_list = chan;
103 }
104
105 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags,
106                      const char *name)
107 {
108     IOCHAN new_iochan;
109
110     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
111         return 0;
112     new_iochan->destroyed = 0;
113     new_iochan->fd = fd;
114     new_iochan->flags = flags;
115     new_iochan->fun = cb;
116     new_iochan->socketfun = NULL;
117     new_iochan->maskfun = NULL;
118     new_iochan->force_event = 0;
119     new_iochan->last_event = new_iochan->max_idle = 0;
120     new_iochan->next = NULL;
121     new_iochan->man = 0;
122     new_iochan->thread_users = 0;
123     new_iochan->name = name ? xstrdup(name) : 0;
124     return new_iochan;
125 }
126
127 static void work_handler(void *work_data)
128 {
129     IOCHAN p = work_data;
130
131     yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
132             p, p->name ? p->name : "", p->this_event);
133     
134     if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
135         (*p->fun)(p, EVENT_TIMEOUT);
136     if (!p->destroyed && (p->this_event & EVENT_INPUT))
137         (*p->fun)(p, EVENT_INPUT);
138     if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
139         (*p->fun)(p, EVENT_OUTPUT);
140     if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
141         (*p->fun)(p, EVENT_EXCEPT);
142
143     yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d",
144             p, p->name ? p->name : "", p->this_event);
145 }
146
147 static void run_fun(iochan_man_t man, IOCHAN p)
148 {
149     if (p->this_event)
150     {
151         if (man->sel_thread)
152         {
153             yaz_log(man->log_level, "eventl: add fun chan=%p name=%s event=%d",
154                     p, p->name ? p->name : "", p->this_event);
155             p->thread_users++;
156             sel_thread_add(man->sel_thread, p);
157         }
158         else
159             work_handler(p);
160     }
161 }
162
163 static int event_loop(iochan_man_t man, IOCHAN *iochans)
164 {
165     do /* loop as long as there are active associations to process */
166     {
167         IOCHAN p, *nextp;
168         fd_set in, out, except;
169         int res, max;
170         static struct timeval nullto = {0, 0}, to;
171         struct timeval *timeout;
172
173         FD_ZERO(&in);
174         FD_ZERO(&out);
175         FD_ZERO(&except);
176         timeout = &to; /* hang on select */
177         to.tv_sec = 300;
178         to.tv_usec = 0;
179         max = 0;
180         for (p = *iochans; p; p = p->next)
181         {
182             if (p->thread_users > 0)
183                 continue;
184             if (p->maskfun)
185                 p->flags = (*p->maskfun)(p);
186             if (p->socketfun)
187                 p->fd = (*p->socketfun)(p);
188             if (p->max_idle && p->max_idle < to.tv_sec)
189                 to.tv_sec = p->max_idle;
190             if (p->fd < 0)
191                 continue;
192             if (p->force_event)
193                 timeout = &nullto;        /* polling select */
194             if (p->flags & EVENT_INPUT)
195                 FD_SET(p->fd, &in);
196             if (p->flags & EVENT_OUTPUT)
197                 FD_SET(p->fd, &out);
198             if (p->flags & EVENT_EXCEPT)
199                 FD_SET(p->fd, &except);
200             if (p->fd > max)
201                 max = p->fd;
202         }
203         if (man->sel_fd != -1)
204         {
205             if (man->sel_fd > max)
206                 max = man->sel_fd;
207             FD_SET(man->sel_fd, &in);
208         }
209         yaz_log(man->log_level, "select begin nofds=%d", max);
210         res = select(max + 1, &in, &out, &except, timeout);
211         yaz_log(man->log_level, "select returned res=%d", res);
212         if (res < 0)
213         {
214             if (errno == EINTR)
215                 continue;
216             else
217             {
218                 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
219                 return 0;
220             }
221         }
222         if (man->sel_fd != -1)
223         {
224             if (FD_ISSET(man->sel_fd, &in))
225             {
226                 IOCHAN chan;
227
228                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
229                         man->sel_fd);
230                 while ((chan = sel_thread_result(man->sel_thread)))
231                 {
232                     yaz_log(man->log_level, "eventl: got thread result chan=%p name=%s",
233                             chan, chan->name ? chan->name : "");
234                     chan->thread_users--;
235                 }
236             }
237         }
238         for (p = *iochans; p; p = p->next)
239         {
240             int force_event = p->force_event;
241             time_t now = time(0);
242             
243             if (p->destroyed)
244             {
245                 yaz_log(man->log_level, "eventl: skip destroyed chan=%p name=%s", p, p->name ? p->name : "");
246                 continue;
247             }
248             if (p->thread_users > 0)
249             {
250                 yaz_log(man->log_level, "eventl: skip chan=%p name=%s users=%d", p, p->name ? p->name : "", p->thread_users);
251                 continue;
252             }
253             p->this_event = 0;
254             p->force_event = 0;
255
256             if ((p->max_idle && now - p->last_event > p->max_idle) 
257                 || force_event == EVENT_TIMEOUT)
258             {
259                 p->last_event = now;
260                 p->this_event |= EVENT_TIMEOUT;
261             }
262             if (p->fd >= 0)
263             {
264                 if (FD_ISSET(p->fd, &in) || force_event == EVENT_INPUT)
265                 {
266                     p->last_event = now;
267                     yaz_log(YLOG_DEBUG, "Eventl input event");
268                     p->this_event |= EVENT_INPUT;
269                 }
270                 if (FD_ISSET(p->fd, &out) || force_event == EVENT_OUTPUT)
271                 {
272                     p->last_event = now;
273                     yaz_log(YLOG_DEBUG, "Eventl output event");
274                     p->this_event |= EVENT_OUTPUT;
275                 }
276                 if (FD_ISSET(p->fd, &except) || force_event == EVENT_EXCEPT)
277                 {
278                     p->last_event = now;
279                     p->this_event |= EVENT_EXCEPT;
280                 }
281             }
282             run_fun(man, p);
283         }
284         for (nextp = iochans; *nextp; )
285         {
286             IOCHAN p = *nextp;
287             if (p->destroyed && p->thread_users == 0)
288             {
289                 *nextp = p->next;
290                 xfree(p->name);
291                 xfree(p);
292             }
293             else
294                 nextp = &p->next;
295         }
296     }
297     while (*iochans);
298     return 0;
299 }
300
301 void iochan_man_events(iochan_man_t man)
302 {
303     if (man->no_threads > 0 && !man->sel_thread)
304     {
305         man->sel_thread = sel_thread_create(
306             work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
307         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
308                 man->no_threads);
309     }
310     event_loop(man, &man->channel_list);
311 }
312
313 /*
314  * Local variables:
315  * c-basic-offset: 4
316  * c-file-style: "Stroustrup"
317  * indent-tabs-mode: nil
318  * End:
319  * vim: shiftwidth=4 tabstop=8 expandtab
320  */
321