Using yaz_poll rather than select 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 };
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(chan);
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 (man->sel_thread) {
199         yaz_log(man->log_level,
200                 "eventl: work add chan=%p name=%s event=%d", p,
201                 p->name ? p->name : "", p->this_event);
202         p->thread_users++;
203         sel_thread_add(man->sel_thread, p);
204     } else
205         work_handler(p);
206 }
207
208 static int event_loop(iochan_man_t man, IOCHAN *iochans) {
209     do /* loop as long as there are active associations to process */
210     {
211         IOCHAN p, *nextp;
212         IOCHAN start;
213         IOCHAN inv_start;
214         int res;
215         static struct timeval to;
216
217         struct yaz_poll_fd *fds;
218         int i, no_fds = 0;
219         int connection_fired = 0;
220         to.tv_sec = 300;
221         to.tv_usec = 0;
222
223         // INV: start must no change through the loop
224
225         yaz_mutex_enter(man->iochan_mutex);
226         start = man->channel_list;
227         yaz_mutex_leave(man->iochan_mutex);
228         inv_start = start;
229         for (p = start; p; p = p->next)
230             if (p->fd >= 0)
231                 no_fds++;
232         if (man->sel_fd != -1)
233             no_fds++;
234         fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
235         i = 0;
236         if (man->sel_fd != -1)
237         {
238             fds[i].fd = man->sel_fd;
239             fds[i].input_mask = 0;
240             if (p->flags & EVENT_INPUT)
241                 fds[i].input_mask |= yaz_poll_read;
242             i++;
243         }
244         for (p = start; p; p = p->next)
245         {
246             if (p->thread_users > 0)
247                 continue;
248             if (p->max_idle && p->max_idle < to.tv_sec)
249                 to.tv_sec = p->max_idle;
250             if (p->fd < 0)
251                 continue;
252             fds[i].fd = p->fd;
253             fds[i].input_mask = 0;
254             if (p->flags & EVENT_INPUT)
255                 fds[i].input_mask |= yaz_poll_read;
256             if (p->flags & EVENT_OUTPUT)
257                 fds[i].input_mask |= yaz_poll_write;
258             if (p->flags & EVENT_EXCEPT)
259                 fds[i].input_mask |= yaz_poll_except;
260             i++;
261         }
262         yaz_log(man->log_level, "yaz_poll begin nofds=%d", no_fds);
263         res = yaz_poll(fds, no_fds, to.tv_sec, 0);
264         yaz_log(man->log_level, "yaz_poll returned res=%d", res);
265         if (res < 0)
266         {
267             if (errno == EINTR)
268                 continue;
269             else {
270                 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
271                 return 0;
272             }
273         }
274         i = 0;
275         if (man->sel_fd != -1)
276         {
277             assert(fds[i].fd == man->sel_fd);
278             if (fds[i].output_mask)
279             {
280                 IOCHAN chan;
281
282                 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
283                         man->sel_fd);
284                 while ((chan = sel_thread_result(man->sel_thread))) {
285                     yaz_log(man->log_level,
286                             "eventl: got thread result chan=%p name=%s", chan,
287                             chan->name ? chan->name : "");
288                     chan->thread_users--;
289                 }
290             }
291             i++;
292         }
293         if (man->log_level)
294         {
295             int no = 0;
296             for (p = start; p; p = p->next)
297                 no++;
298             yaz_log(man->log_level, "%d channels", no);
299         }
300         for (p = start; p; p = p->next)
301         {
302             time_t now = time(0);
303
304             if (p->destroyed)
305             {
306                 yaz_log(man->log_level,
307                         "eventl: skip destroyed chan=%p name=%s", p,
308                         p->name ? p->name : "");
309                 if (p->fd >= 0)
310                     i++;
311                 continue;
312             }
313             if (p->thread_users > 0)
314             {
315                 yaz_log(man->log_level,
316                         "eventl: skip chan=%p name=%s users=%d", p,
317                         p->name ? p->name : "", p->thread_users);
318                 if (p->fd >= 0)
319                     i++;
320                 continue;
321             }
322             p->this_event = 0;
323
324             if (p->max_idle && now - p->last_event > p->max_idle)
325             {
326                 p->last_event = now;
327                 p->this_event |= EVENT_TIMEOUT;
328             }
329             if (p->fd >= 0)
330             {
331                 assert(fds[i].fd == p->fd);
332                 if (fds[i].output_mask & yaz_poll_read)
333                 {
334                     p->last_event = now;
335                     p->this_event |= EVENT_INPUT;
336                 }
337                 if (fds[i].output_mask & yaz_poll_write)
338                 {
339                     p->last_event = now;
340                     p->this_event |= EVENT_OUTPUT;
341                 }
342                 if (fds[i].output_mask & yaz_poll_except)
343                 {
344                     p->last_event = now;
345                     p->this_event |= EVENT_EXCEPT;
346                 }
347                 i++;
348             }
349             /* only fire one Z39.50/SRU socket event.. except for timeout */
350             if (p->this_event) {
351                 if (!(p->this_event & EVENT_TIMEOUT) &&
352                     !strcmp(p->name, "connection_socket"))
353                 {
354                     /* not a timeout and we have a Z39.50/SRU socket */
355                     if (connection_fired == 0)
356                         run_fun(man, p);
357                     connection_fired++;
358                 }
359                 else
360                     run_fun(man, p);
361             }
362         }
363
364         assert(inv_start == start);
365         yaz_mutex_enter(man->iochan_mutex);
366         for (nextp = iochans; *nextp;) {
367             IOCHAN p = *nextp;
368             if (p->destroyed && p->thread_users == 0) {
369                 *nextp = iochan_destroy_real(p);
370             } else
371                 nextp = &p->next;
372         }
373         yaz_mutex_leave(man->iochan_mutex);
374         xfree(fds);
375     } while (*iochans);
376     return 0;
377 }
378
379 void iochan_man_events(iochan_man_t man) {
380     if (man->no_threads > 0 && !man->sel_thread) {
381         man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
382                 &man->sel_fd, man->no_threads);
383         yaz_log(man->log_level, "iochan_man_events. Using %d threads",
384                 man->no_threads);
385     }
386     event_loop(man, &man->channel_list);
387 }
388
389 void pazpar2_sleep(double d) {
390 #ifdef WIN32
391     Sleep( (DWORD) (d * 1000));
392 #else
393     struct timeval tv;
394     tv.tv_sec = floor(d);
395     tv.tv_usec = (d - floor(d)) * 1000000;
396     select(0, 0, 0, 0, &tv);
397 #endif
398 }
399
400 /*
401  * Local variables:
402  * c-basic-offset: 4
403  * c-file-style: "Stroustrup"
404  * indent-tabs-mode: nil
405  * End:
406  * vim: shiftwidth=4 tabstop=8 expandtab
407  */
408