Merge branch 'master' into channel_list_mutex
[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 <math.h>
36 #include <stdio.h>
37 #include <assert.h>
38
39 #ifdef WIN32
40 #include <winsock.h>
41 #else
42 #include <unistd.h>
43 #endif
44 #if HAVE_SYS_TIME_H
45 #include <sys/time.h>
46 #endif
47
48 #include <stdlib.h>
49 #include <errno.h>
50 #include <string.h>
51
52 #include <yaz/yconfig.h>
53 #include <yaz/log.h>
54 #include <yaz/comstack.h>
55 #include <yaz/xmalloc.h>
56 #include <yaz/mutex.h>
57 #include "eventl.h"
58 #include "sel_thread.h"
59
60 struct iochan_man_s {
61     IOCHAN channel_list;
62     sel_thread_t sel_thread;
63     int sel_fd;
64     int no_threads;
65     int log_level;
66     YAZ_MUTEX iochan_mutex;
67 };
68
69 struct iochan_man_iter {
70     iochan_man_t man;
71     IOCHAN current; 
72     int first;
73 };
74
75 iochan_man_t iochan_man_create(int no_threads)
76 {
77     iochan_man_t man = xmalloc(sizeof(*man));
78     man->channel_list = 0;
79     man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
80     man->sel_fd = -1;
81     man->no_threads = no_threads;
82     man->log_level = yaz_log_module_level("iochan");
83     man->iochan_mutex = 0;
84     yaz_mutex_create(&man->iochan_mutex);
85     return man;
86 }
87
88 void iochan_man_destroy(iochan_man_t *mp)
89 {
90     if (*mp)
91     {
92         IOCHAN c;
93         if ((*mp)->sel_thread)
94             sel_thread_destroy((*mp)->sel_thread);
95         
96         c = (*mp)->channel_list;
97         while (c)
98         {
99             IOCHAN c_next = c->next;
100             xfree(c->name);
101             xfree(c);
102             c = c_next;
103         }
104         xfree(*mp);
105         *mp = 0;
106     }
107 }
108
109 void iochan_add(iochan_man_t man, IOCHAN chan)
110 {
111     chan->man = man;
112     yaz_mutex_enter(man->iochan_mutex);
113     yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan, man->channel_list);
114     chan->next = man->channel_list;
115     man->channel_list = chan;
116     yaz_mutex_leave(man->iochan_mutex);
117 }
118
119 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags,
120                      const char *name)
121 {
122     IOCHAN new_iochan;
123
124     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
125         return 0;
126     new_iochan->destroyed = 0;
127     new_iochan->fd = fd;
128     new_iochan->flags = flags;
129     new_iochan->fun = cb;
130     new_iochan->last_event = new_iochan->max_idle = 0;
131     new_iochan->next = NULL;
132     new_iochan->man = 0;
133     new_iochan->thread_users = 0;
134     new_iochan->name = name ? xstrdup(name) : 0;
135     return new_iochan;
136 }
137
138 static void work_handler(void *work_data)
139 {
140     IOCHAN p = work_data;
141
142     yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
143             p, p->name ? p->name : "", p->this_event);
144     
145     if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
146         (*p->fun)(p, EVENT_TIMEOUT);
147     if (!p->destroyed && (p->this_event & EVENT_INPUT))
148         (*p->fun)(p, EVENT_INPUT);
149     if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
150         (*p->fun)(p, EVENT_OUTPUT);
151     if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
152         (*p->fun)(p, EVENT_EXCEPT);
153
154     yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d",
155             p, p->name ? p->name : "", p->this_event);
156 }
157
158 static void run_fun(iochan_man_t man, IOCHAN p)
159 {
160     if (p->this_event)
161     {
162         if (man->sel_thread)
163         {
164             yaz_log(man->log_level, "eventl: work add chan=%p name=%s event=%d",
165                     p, p->name ? p->name : "", p->this_event);
166             p->thread_users++;
167             sel_thread_add(man->sel_thread, p);
168         }
169         else
170             work_handler(p);
171     }
172 }
173
174 static IOCHAN iochan_man_get_first(struct iochan_man_iter *iter, iochan_man_t man) {
175     iter->man = man;
176     iter->first = 1;
177     yaz_mutex_enter(man->iochan_mutex);
178     iter->current = man->channel_list;
179     yaz_log(man->log_level, "iochan_man_get_first : chan=%p ", iter->current);
180     if (!iter->current)
181         yaz_mutex_leave(man->iochan_mutex);
182     return iter->current;
183 }
184
185 static IOCHAN iochan_man_get_next(struct iochan_man_iter *iter) {
186     IOCHAN current = NULL, next = NULL;
187     current = iter->current;
188     assert(current);
189     if (current) {
190         next = current->next;
191         iter->current = iter->current->next;
192         if (iter->first) {
193             yaz_log(iter->man->log_level, "iochan_man_get_next : chan=%p next=%p", current, next);
194             iter->first = 0;
195             yaz_mutex_leave(iter->man->iochan_mutex);
196         }
197     }
198     return iter->current;
199 }
200
201 static int event_loop(iochan_man_t man, IOCHAN *iochans)
202 {
203     do /* loop as long as there are active associations to process */
204     {
205         IOCHAN p, *nextp;
206         fd_set in, out, except;
207         int res, max;
208         static struct timeval to;
209         struct timeval *timeout;
210         static struct iochan_man_iter iter; 
211
212         FD_ZERO(&in);
213         FD_ZERO(&out);
214         FD_ZERO(&except);
215         timeout = &to; /* hang on select */
216         to.tv_sec = 300;
217         to.tv_usec = 0;
218         max = 0;
219         for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter) )
220         {
221             if (p->thread_users > 0)
222                 continue;
223             if (p->max_idle && p->max_idle < to.tv_sec)
224                 to.tv_sec = p->max_idle;
225             if (p->fd < 0)
226                 continue;
227             if (p->flags & EVENT_INPUT)
228                 FD_SET(p->fd, &in);
229             if (p->flags & EVENT_OUTPUT)
230                 FD_SET(p->fd, &out);
231             if (p->flags & EVENT_EXCEPT)
232                 FD_SET(p->fd, &except);
233             if (p->fd > max)
234                 max = p->fd;
235         }
236         yaz_log(man->log_level, "max=%d nofds=%d", max, man->sel_fd);
237         
238         if (man->sel_fd != -1)
239         {
240             if (man->sel_fd > max)
241                 max = man->sel_fd;
242             FD_SET(man->sel_fd, &in);
243         }
244         yaz_log(man->log_level, "select begin nofds=%d", max);
245         res = select(max + 1, &in, &out, &except, timeout);
246         yaz_log(man->log_level, "select returned res=%d", res);
247         if (res < 0)
248         {
249             if (errno == EINTR)
250                 continue;
251             else
252             {
253                 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
254                 return 0;
255             }
256         }
257         if (man->sel_fd != -1)
258         {
259             if (FD_ISSET(man->sel_fd, &in))
260             {
261                 IOCHAN chan;
262
263                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
264                         man->sel_fd);
265                 while ((chan = sel_thread_result(man->sel_thread)))
266                 {
267                     yaz_log(man->log_level, "eventl: got thread result chan=%p name=%s",
268                             chan, chan->name ? chan->name : "");
269                     chan->thread_users--;
270                 }
271             }
272         }
273         if (man->log_level)
274         {
275             int no = 0;
276             for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter)) {
277                 no++;
278             }
279             yaz_log(man->log_level, "%d channels", no);
280         }
281         for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter))
282         {
283             time_t now = time(0);
284             
285             if (p->destroyed)
286             {
287                 yaz_log(man->log_level, "eventl: skip destroyed chan=%p name=%s", p, p->name ? p->name : "");
288                 continue;
289             }
290             if (p->thread_users > 0)
291             {
292                 yaz_log(man->log_level, "eventl: skip chan=%p name=%s users=%d", p, p->name ? p->name : "", p->thread_users);
293                 continue;
294             }
295             p->this_event = 0;
296
297             if (p->max_idle && now - p->last_event > p->max_idle)
298             {
299                 p->last_event = now;
300                 p->this_event |= EVENT_TIMEOUT;
301             }
302             if (p->fd >= 0)
303             {
304                 if (FD_ISSET(p->fd, &in))
305                 {
306                     p->last_event = now;
307                     p->this_event |= EVENT_INPUT;
308                 }
309                 if (FD_ISSET(p->fd, &out))
310                 {
311                     p->last_event = now;
312                     p->this_event |= EVENT_OUTPUT;
313                 }
314                 if (FD_ISSET(p->fd, &except))
315                 {
316                     p->last_event = now;
317                     p->this_event |= EVENT_EXCEPT;
318                 }
319             }
320             run_fun(man, p);
321         }
322         yaz_mutex_enter(man->iochan_mutex);
323         for (nextp = iochans; *nextp; )
324         {
325             IOCHAN p = *nextp;
326             if (p->destroyed && p->thread_users == 0)
327             {
328                 *nextp = p->next;
329                 xfree(p->name);
330                 xfree(p);
331             }
332             else
333                 nextp = &p->next;
334         }
335         yaz_mutex_leave(man->iochan_mutex);
336     }
337     while (*iochans);
338     return 0;
339 }
340
341 void iochan_man_events(iochan_man_t man)
342 {
343     if (man->no_threads > 0 && !man->sel_thread)
344     {
345         man->sel_thread = sel_thread_create(
346             work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
347         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
348                 man->no_threads);
349     }
350     event_loop(man, &man->channel_list);
351 }
352
353 void pazpar2_sleep(double d)
354 {
355 #ifdef WIN32
356     Sleep( (DWORD) (d * 1000));
357 #else
358     struct timeval tv;
359     tv.tv_sec = floor(d);
360     tv.tv_usec = (d - floor(d)) * 1000000;
361     select(0, 0, 0, 0, &tv);
362 #endif
363 }
364
365 /*
366  * Local variables:
367  * c-basic-offset: 4
368  * c-file-style: "Stroustrup"
369  * indent-tabs-mode: nil
370  * End:
371  * vim: shiftwidth=4 tabstop=8 expandtab
372  */
373