Added iochan_man iterator: Protects the initial node in threaded code.
[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->socketfun = NULL;
131     new_iochan->maskfun = NULL;
132     new_iochan->last_event = new_iochan->max_idle = 0;
133     new_iochan->next = NULL;
134     new_iochan->man = 0;
135     new_iochan->thread_users = 0;
136     new_iochan->name = name ? xstrdup(name) : 0;
137     return new_iochan;
138 }
139
140 static void work_handler(void *work_data)
141 {
142     IOCHAN p = work_data;
143
144     yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
145             p, p->name ? p->name : "", p->this_event);
146     
147     if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
148         (*p->fun)(p, EVENT_TIMEOUT);
149     if (!p->destroyed && (p->this_event & EVENT_INPUT))
150         (*p->fun)(p, EVENT_INPUT);
151     if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
152         (*p->fun)(p, EVENT_OUTPUT);
153     if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
154         (*p->fun)(p, EVENT_EXCEPT);
155
156     yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d",
157             p, p->name ? p->name : "", p->this_event);
158 }
159
160 static void run_fun(iochan_man_t man, IOCHAN p)
161 {
162     if (p->this_event)
163     {
164         if (man->sel_thread)
165         {
166             yaz_log(man->log_level, "eventl: work add chan=%p name=%s event=%d",
167                     p, p->name ? p->name : "", p->this_event);
168             p->thread_users++;
169             sel_thread_add(man->sel_thread, p);
170         }
171         else
172             work_handler(p);
173     }
174 }
175
176 static IOCHAN iochan_man_get_first(struct iochan_man_iter *iter, iochan_man_t man) {
177     iter->man = man;
178     iter->first = 1;
179     yaz_mutex_enter(man->iochan_mutex);
180     iter->current = man->channel_list;
181     yaz_log(man->log_level, "iochan_man_get_first : chan=%p ", iter->current);
182     if (!iter->current)
183         yaz_mutex_leave(man->iochan_mutex);
184     return iter->current;
185 }
186
187 static IOCHAN iochan_man_get_next(struct iochan_man_iter *iter) {
188     IOCHAN current = NULL, next = NULL;
189     current = iter->current;
190     assert(current);
191     if (current) {
192         next = current->next;
193         iter->current = iter->current->next;
194         if (iter->first) {
195             yaz_log(iter->man->log_level, "iochan_man_get_next : chan=%p next=%p", current, next);
196             iter->first = 0;
197             yaz_mutex_leave(iter->man->iochan_mutex);
198         }
199     }
200     return iter->current;
201 }
202
203 static int event_loop(iochan_man_t man, IOCHAN *iochans)
204 {
205     do /* loop as long as there are active associations to process */
206     {
207         IOCHAN p, *nextp;
208         fd_set in, out, except;
209         int res, max;
210         static struct timeval to;
211         struct timeval *timeout;
212         static struct iochan_man_iter iter; 
213
214         FD_ZERO(&in);
215         FD_ZERO(&out);
216         FD_ZERO(&except);
217         timeout = &to; /* hang on select */
218         to.tv_sec = 300;
219         to.tv_usec = 0;
220         max = 0;
221         for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter) )
222         {
223             if (p->thread_users > 0)
224                 continue;
225             if (p->maskfun)
226                 p->flags = (*p->maskfun)(p);
227             if (p->socketfun)
228                 p->fd = (*p->socketfun)(p);
229             if (p->max_idle && p->max_idle < to.tv_sec)
230                 to.tv_sec = p->max_idle;
231             if (p->fd < 0)
232                 continue;
233             if (p->flags & EVENT_INPUT)
234                 FD_SET(p->fd, &in);
235             if (p->flags & EVENT_OUTPUT)
236                 FD_SET(p->fd, &out);
237             if (p->flags & EVENT_EXCEPT)
238                 FD_SET(p->fd, &except);
239             if (p->fd > max)
240                 max = p->fd;
241         }
242         yaz_log(man->log_level, "max=%d nofds=%d", max, man->sel_fd);
243         
244         if (man->sel_fd != -1)
245         {
246             if (man->sel_fd > max)
247                 max = man->sel_fd;
248             FD_SET(man->sel_fd, &in);
249         }
250         yaz_log(man->log_level, "select begin nofds=%d", max);
251         res = select(max + 1, &in, &out, &except, timeout);
252         yaz_log(man->log_level, "select returned res=%d", res);
253         if (res < 0)
254         {
255             if (errno == EINTR)
256                 continue;
257             else
258             {
259                 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
260                 return 0;
261             }
262         }
263         if (man->sel_fd != -1)
264         {
265             if (FD_ISSET(man->sel_fd, &in))
266             {
267                 IOCHAN chan;
268
269                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
270                         man->sel_fd);
271                 while ((chan = sel_thread_result(man->sel_thread)))
272                 {
273                     yaz_log(man->log_level, "eventl: got thread result chan=%p name=%s",
274                             chan, chan->name ? chan->name : "");
275                     chan->thread_users--;
276                 }
277             }
278         }
279         if (man->log_level)
280         {
281             int no = 0;
282             for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter)) {
283                 no++;
284             }
285             yaz_log(man->log_level, "%d channels", no);
286         }
287         for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter))
288         {
289             time_t now = time(0);
290             
291             if (p->destroyed)
292             {
293                 yaz_log(man->log_level, "eventl: skip destroyed chan=%p name=%s", p, p->name ? p->name : "");
294                 continue;
295             }
296             if (p->thread_users > 0)
297             {
298                 yaz_log(man->log_level, "eventl: skip chan=%p name=%s users=%d", p, p->name ? p->name : "", p->thread_users);
299                 continue;
300             }
301             p->this_event = 0;
302
303             if (p->max_idle && now - p->last_event > p->max_idle)
304             {
305                 p->last_event = now;
306                 p->this_event |= EVENT_TIMEOUT;
307             }
308             if (p->fd >= 0)
309             {
310                 if (FD_ISSET(p->fd, &in))
311                 {
312                     p->last_event = now;
313                     p->this_event |= EVENT_INPUT;
314                 }
315                 if (FD_ISSET(p->fd, &out))
316                 {
317                     p->last_event = now;
318                     p->this_event |= EVENT_OUTPUT;
319                 }
320                 if (FD_ISSET(p->fd, &except))
321                 {
322                     p->last_event = now;
323                     p->this_event |= EVENT_EXCEPT;
324                 }
325             }
326             run_fun(man, p);
327         }
328         yaz_mutex_enter(man->iochan_mutex);
329         for (nextp = iochans; *nextp; )
330         {
331             IOCHAN p = *nextp;
332             if (p->destroyed && p->thread_users == 0)
333             {
334                 *nextp = p->next;
335                 xfree(p->name);
336                 xfree(p);
337             }
338             else
339                 nextp = &p->next;
340         }
341         yaz_mutex_leave(man->iochan_mutex);
342     }
343     while (*iochans);
344     return 0;
345 }
346
347 void iochan_man_events(iochan_man_t man)
348 {
349     if (man->no_threads > 0 && !man->sel_thread)
350     {
351         man->sel_thread = sel_thread_create(
352             work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
353         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
354                 man->no_threads);
355     }
356     event_loop(man, &man->channel_list);
357 }
358
359 void pazpar2_sleep(double d)
360 {
361 #ifdef WIN32
362     Sleep( (DWORD) (d * 1000));
363 #else
364     struct timeval tv;
365     tv.tv_sec = floor(d);
366     tv.tv_usec = (d - floor(d)) * 1000000;
367     select(0, 0, 0, 0, &tv);
368 #endif
369 }
370
371 /*
372  * Local variables:
373  * c-basic-offset: 4
374  * c-file-style: "Stroustrup"
375  * indent-tabs-mode: nil
376  * End:
377  * vim: shiftwidth=4 tabstop=8 expandtab
378  */
379