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