Re-use fds array; fix for sel_fd in use PAZ-947
[pazpar2-moved-to-github.git] / src / eventl.c
1 /* This file is part of Pazpar2.
2  Copyright (C) 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) 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     int size_fds;
108     struct yaz_poll_fd *fds;
109 };
110
111 iochan_man_t iochan_man_create(int no_threads) {
112     iochan_man_t man = xmalloc(sizeof(*man));
113     man->channel_list = 0;
114     man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
115     man->sel_fd = -1;
116     man->no_threads = no_threads;
117     man->log_level = yaz_log_module_level("iochan");
118     man->iochan_mutex = 0;
119     man->size_fds = 0;
120     man->fds = 0;
121     yaz_mutex_create(&man->iochan_mutex);
122     return man;
123 }
124
125 IOCHAN iochan_destroy_real(IOCHAN chan)
126 {
127     IOCHAN next = chan->next;
128     if (chan->name)
129         xfree(chan->name);
130     xfree(chan);
131     iochan_use(-1);
132     return next;
133 }
134
135 void iochan_man_destroy(iochan_man_t *mp) {
136     if (*mp) {
137         IOCHAN c;
138         if ((*mp)->sel_thread)
139             sel_thread_destroy((*mp)->sel_thread);
140
141         yaz_mutex_enter((*mp)->iochan_mutex);
142         c = (*mp)->channel_list;
143         (*mp)->channel_list = NULL;
144         yaz_mutex_leave((*mp)->iochan_mutex);
145         while (c) {
146             c = iochan_destroy_real(c);
147         }
148         yaz_mutex_destroy(&(*mp)->iochan_mutex);
149         xfree((*mp)->fds);
150         xfree(*mp);
151         *mp = 0;
152     }
153 }
154
155 void iochan_add(iochan_man_t man, IOCHAN chan) {
156     chan->man = man;
157     yaz_mutex_enter(man->iochan_mutex);
158     yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
159             man->channel_list);
160     chan->next = man->channel_list;
161     man->channel_list = chan;
162     yaz_mutex_leave(man->iochan_mutex);
163 }
164
165 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name) {
166     IOCHAN new_iochan;
167
168     if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
169         return 0;
170     iochan_use(1);
171     new_iochan->destroyed = 0;
172     new_iochan->fd = fd;
173     new_iochan->flags = flags;
174     new_iochan->fun = cb;
175     new_iochan->last_event = new_iochan->max_idle = 0;
176     new_iochan->next = NULL;
177     new_iochan->man = 0;
178     new_iochan->thread_users = 0;
179     new_iochan->name = name ? xstrdup(name) : 0;
180     return new_iochan;
181 }
182
183 static void work_handler(void *work_data) {
184     IOCHAN p = work_data;
185
186     yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
187             p, p->name ? p->name : "", p->this_event);
188
189     if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
190         (*p->fun)(p, EVENT_TIMEOUT);
191     if (!p->destroyed && (p->this_event & EVENT_INPUT))
192         (*p->fun)(p, EVENT_INPUT);
193     if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
194         (*p->fun)(p, EVENT_OUTPUT);
195     if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
196         (*p->fun)(p, EVENT_EXCEPT);
197
198     yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
199             p->name ? p->name : "", p->this_event);
200 }
201
202 static void run_fun(iochan_man_t man, IOCHAN p) {
203     if (man->sel_thread) {
204         yaz_log(man->log_level,
205                 "eventl: work add chan=%p name=%s event=%d", p,
206                 p->name ? p->name : "", p->this_event);
207         p->thread_users++;
208         sel_thread_add(man->sel_thread, p);
209     } else
210         work_handler(p);
211 }
212
213 static int event_loop(iochan_man_t man, IOCHAN *iochans) {
214     do /* loop as long as there are active associations to process */
215     {
216         IOCHAN p, *nextp;
217         IOCHAN start;
218         IOCHAN inv_start;
219         int res;
220         static struct timeval to;
221         struct yaz_poll_fd *fds;
222         int i, no_fds = 0;
223         int connection_fired = 0;
224         to.tv_sec = 300;
225         to.tv_usec = 0;
226
227         // INV: start must no change through the loop
228
229         yaz_mutex_enter(man->iochan_mutex);
230         start = man->channel_list;
231         yaz_mutex_leave(man->iochan_mutex);
232         inv_start = start;
233         for (p = start; p; p = p->next)
234             if (p->fd >= 0)
235                 no_fds++;
236         if (man->sel_fd != -1)
237             no_fds++;
238         if (no_fds > man->size_fds)
239         {
240             man->size_fds = no_fds * 2;
241             man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
242         }
243         fds = man->fds;
244         i = 0;
245         if (man->sel_fd != -1)
246         {
247             fds[i].fd = man->sel_fd;
248             fds[i].input_mask = yaz_poll_read;
249             i++;
250         }
251         for (p = start; p; p = p->next)
252         {
253             if (p->thread_users > 0)
254                 continue;
255             if (p->max_idle && p->max_idle < to.tv_sec)
256                 to.tv_sec = p->max_idle;
257             if (p->fd < 0)
258                 continue;
259             fds[i].fd = p->fd;
260             fds[i].input_mask = 0;
261             if (p->flags & EVENT_INPUT)
262                 fds[i].input_mask |= yaz_poll_read;
263             if (p->flags & EVENT_OUTPUT)
264                 fds[i].input_mask |= yaz_poll_write;
265             if (p->flags & EVENT_EXCEPT)
266                 fds[i].input_mask |= yaz_poll_except;
267             i++;
268         }
269         yaz_log(man->log_level, "yaz_poll begin nofds=%d", no_fds);
270         res = yaz_poll(fds, no_fds, to.tv_sec, 0);
271         yaz_log(man->log_level, "yaz_poll returned res=%d", res);
272         if (res < 0)
273         {
274             if (errno == EINTR)
275                 continue;
276             else {
277                 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
278                 return 0;
279             }
280         }
281         i = 0;
282         if (man->sel_fd != -1)
283         {
284             assert(fds[i].fd == man->sel_fd);
285             if (fds[i].output_mask)
286             {
287                 IOCHAN chan;
288
289                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
290                         man->sel_fd);
291                 while ((chan = sel_thread_result(man->sel_thread))) {
292                     yaz_log(man->log_level,
293                             "eventl: got thread result chan=%p name=%s", chan,
294                             chan->name ? chan->name : "");
295                     chan->thread_users--;
296                 }
297             }
298             i++;
299         }
300         if (man->log_level)
301         {
302             int no = 0;
303             for (p = start; p; p = p->next)
304                 no++;
305             yaz_log(man->log_level, "%d channels", no);
306         }
307         for (p = start; p; p = p->next)
308         {
309             time_t now = time(0);
310
311             if (p->destroyed)
312             {
313                 yaz_log(man->log_level,
314                         "eventl: skip destroyed chan=%p name=%s", p,
315                         p->name ? p->name : "");
316                 if (p->fd >= 0)
317                     i++;
318                 continue;
319             }
320             if (p->thread_users > 0)
321             {
322                 yaz_log(man->log_level,
323                         "eventl: skip chan=%p name=%s users=%d", p,
324                         p->name ? p->name : "", p->thread_users);
325                 if (p->fd >= 0)
326                     i++;
327                 continue;
328             }
329             p->this_event = 0;
330
331             if (p->max_idle && now - p->last_event > p->max_idle)
332             {
333                 p->last_event = now;
334                 p->this_event |= EVENT_TIMEOUT;
335             }
336             if (p->fd >= 0)
337             {
338                 assert(fds[i].fd == p->fd);
339                 if (fds[i].output_mask & yaz_poll_read)
340                 {
341                     p->last_event = now;
342                     p->this_event |= EVENT_INPUT;
343                 }
344                 if (fds[i].output_mask & yaz_poll_write)
345                 {
346                     p->last_event = now;
347                     p->this_event |= EVENT_OUTPUT;
348                 }
349                 if (fds[i].output_mask & yaz_poll_except)
350                 {
351                     p->last_event = now;
352                     p->this_event |= EVENT_EXCEPT;
353                 }
354                 i++;
355             }
356             /* only fire one Z39.50/SRU socket event.. except for timeout */
357             if (p->this_event) {
358                 if (!(p->this_event & EVENT_TIMEOUT) &&
359                     !strcmp(p->name, "connection_socket"))
360                 {
361                     /* not a timeout and we have a Z39.50/SRU socket */
362                     if (connection_fired == 0)
363                         run_fun(man, p);
364                     connection_fired++;
365                 }
366                 else
367                     run_fun(man, p);
368             }
369         }
370
371         assert(inv_start == start);
372         yaz_mutex_enter(man->iochan_mutex);
373         for (nextp = iochans; *nextp;) {
374             IOCHAN p = *nextp;
375             if (p->destroyed && p->thread_users == 0) {
376                 *nextp = iochan_destroy_real(p);
377             } else
378                 nextp = &p->next;
379         }
380         yaz_mutex_leave(man->iochan_mutex);
381     } while (*iochans);
382     return 0;
383 }
384
385 void iochan_man_events(iochan_man_t man) {
386     if (man->no_threads > 0 && !man->sel_thread) {
387         man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
388                 &man->sel_fd, man->no_threads);
389         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
390                 man->no_threads);
391     }
392     event_loop(man, &man->channel_list);
393 }
394
395 void pazpar2_sleep(double d) {
396 #ifdef WIN32
397     Sleep( (DWORD) (d * 1000));
398 #else
399     struct timeval tv;
400     tv.tv_sec = floor(d);
401     tv.tv_usec = (d - floor(d)) * 1000000;
402     select(0, 0, 0, 0, &tv);
403 #endif
404 }
405
406 /*
407  * Local variables:
408  * c-basic-offset: 4
409  * c-file-style: "Stroustrup"
410  * indent-tabs-mode: nil
411  * End:
412  * vim: shiftwidth=4 tabstop=8 expandtab
413  */
414