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