Fix hard-coded 30 second limit. Unfortunately we have a lot of
[pazpar2-moved-to-github.git] / src / eventl.c
1 /* $Id: eventl.c,v 1.6 2007-04-20 11:43:43 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /*
23  * Based on  ParaZ - a simple tool for harvesting performance data for
24  * parallel operations using Z39.50.
25  * Copyright (c) 2000-2004 Index Data ApS
26  * See LICENSE file for details.
27  */
28
29 /*
30  * Based on revision YAZ' server/eventl.c 1.29.
31  */
32
33 #include <stdio.h>
34 #include <assert.h>
35
36 #if HAVE_CONFIG_H
37 #include <cconfig.h>
38 #endif
39
40 #ifdef WIN32
41 #include <winsock.h>
42 #else
43 #include <unistd.h>
44 #endif
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <string.h>
48
49 #include <yaz/yconfig.h>
50 #include <yaz/log.h>
51 #include <yaz/comstack.h>
52 #include <yaz/xmalloc.h>
53 #include "eventl.h"
54 #include <yaz/statserv.h>
55
56 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
57 {
58     IOCHAN new_iochan;
59
60     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
61         return 0;
62     new_iochan->destroyed = 0;
63     new_iochan->fd = fd;
64     new_iochan->flags = flags;
65     new_iochan->fun = cb;
66     new_iochan->force_event = 0;
67     new_iochan->last_event = new_iochan->max_idle = 0;
68     new_iochan->next = NULL;
69     return new_iochan;
70 }
71
72 int event_loop(IOCHAN *iochans)
73 {
74     do /* loop as long as there are active associations to process */
75     {
76         IOCHAN p, nextp;
77         fd_set in, out, except;
78         int res, max;
79         static struct timeval nullto = {0, 0}, to;
80         struct timeval *timeout;
81
82         FD_ZERO(&in);
83         FD_ZERO(&out);
84         FD_ZERO(&except);
85         timeout = &to; /* hang on select */
86         to.tv_sec = 30;
87         to.tv_usec = 0;
88         max = 0;
89         for (p = *iochans; p; p = p->next)
90         {
91             if (p->fd < 0)
92                 continue;
93             if (p->force_event)
94                 timeout = &nullto;        /* polling select */
95             if (p->flags & EVENT_INPUT)
96                 FD_SET(p->fd, &in);
97             if (p->flags & EVENT_OUTPUT)
98                 FD_SET(p->fd, &out);
99             if (p->flags & EVENT_EXCEPT)
100                 FD_SET(p->fd, &except);
101             if (p->fd > max)
102                 max = p->fd;
103             if (p->max_idle && p->max_idle < to.tv_sec)
104                 to.tv_sec = p->max_idle;
105         }
106         if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
107         {
108             if (errno == EINTR)
109                 continue;
110             else
111                 abort();
112         }
113         for (p = *iochans; p; p = p->next)
114         {
115             int force_event = p->force_event;
116             time_t now = time(0);
117
118             p->force_event = 0;
119             if (!p->destroyed && ((p->max_idle && now - p->last_event >
120                 p->max_idle) || force_event == EVENT_TIMEOUT))
121             {
122                 p->last_event = now;
123                 (*p->fun)(p, EVENT_TIMEOUT);
124             }
125             if (p->fd < 0)
126                 continue;
127             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
128                 force_event == EVENT_INPUT))
129             {
130                 p->last_event = now;
131                 (*p->fun)(p, EVENT_INPUT);
132             }
133             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
134                 force_event == EVENT_OUTPUT))
135             {
136                 p->last_event = now;
137                 (*p->fun)(p, EVENT_OUTPUT);
138             }
139             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
140                 force_event == EVENT_EXCEPT))
141             {
142                 p->last_event = now;
143                 (*p->fun)(p, EVENT_EXCEPT);
144             }
145         }
146         for (p = *iochans; p; p = nextp)
147         {
148             nextp = p->next;
149
150             if (p->destroyed)
151             {
152                 IOCHAN tmp = p, pr;
153
154                 /* Now reset the pointers */
155                 if (p == *iochans)
156                     *iochans = p->next;
157                 else
158                 {
159                     for (pr = *iochans; pr; pr = pr->next)
160                         if (pr->next == p)
161                             break;
162                     assert(pr); /* grave error if it weren't there */
163                     pr->next = p->next;
164                 }
165                 if (nextp == p)
166                     nextp = p->next;
167                 xfree(tmp);
168             }
169         }
170     }
171     while (*iochans);
172     return 0;
173 }
174
175 /*
176  * Local variables:
177  * c-basic-offset: 4
178  * indent-tabs-mode: nil
179  * End:
180  * vim: shiftwidth=4 tabstop=8 expandtab
181  */