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