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