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