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