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