fc188a3aba3178426894272a66682e9c8dfe19fb
[pazpar2-moved-to-github.git] / eventl.c
1 /*
2  * ParaZ - a simple tool for harvesting performance data for parallel
3  * operations using Z39.50.
4  * Copyright (c) 2000-2004 Index Data ApS
5  * See LICENSE file for details.
6  */
7
8 /*
9  * $Id: eventl.c,v 1.1 2006-11-14 20:44:37 quinn Exp $
10  * Based on revision YAZ' server/eventl.c 1.29.
11  */
12
13 #include <stdio.h>
14 #include <assert.h>
15 #ifdef WIN32
16 #include <winsock.h>
17 #else
18 #include <unistd.h>
19 #endif
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23
24 #include <yaz/yconfig.h>
25 #include <yaz/log.h>
26 #include <yaz/comstack.h>
27 #include <yaz/xmalloc.h>
28 #include "eventl.h"
29 #include <yaz/statserv.h>
30
31 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
32 {
33     IOCHAN new_iochan;
34
35     if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
36         return 0;
37     new_iochan->destroyed = 0;
38     new_iochan->fd = fd;
39     new_iochan->flags = flags;
40     new_iochan->fun = cb;
41     new_iochan->force_event = 0;
42     new_iochan->last_event = new_iochan->max_idle = 0;
43     new_iochan->next = NULL;
44     return new_iochan;
45 }
46
47 int event_loop(IOCHAN *iochans)
48 {
49     do /* loop as long as there are active associations to process */
50     {
51         IOCHAN p, nextp;
52         fd_set in, out, except;
53         int res, max;
54         static struct timeval nullto = {0, 0}, to;
55         struct timeval *timeout;
56
57         FD_ZERO(&in);
58         FD_ZERO(&out);
59         FD_ZERO(&except);
60         timeout = &to; /* hang on select */
61         to.tv_sec = 30;
62         to.tv_usec = 0;
63         max = 0;
64         for (p = *iochans; p; p = p->next)
65         {
66             if (p->force_event)
67                 timeout = &nullto;        /* polling select */
68             if (p->flags & EVENT_INPUT)
69                 FD_SET(p->fd, &in);
70             if (p->flags & EVENT_OUTPUT)
71                 FD_SET(p->fd, &out);
72             if (p->flags & EVENT_EXCEPT)
73                 FD_SET(p->fd, &except);
74             if (p->fd > max)
75                 max = p->fd;
76         }
77         if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
78         {
79             if (errno == EINTR)
80                 continue;
81             else
82                 abort();
83         }
84         for (p = *iochans; p; p = p->next)
85         {
86             int force_event = p->force_event;
87             time_t now = time(0);
88
89             p->force_event = 0;
90             if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
91                 force_event == EVENT_INPUT))
92             {
93                 p->last_event = now;
94                 (*p->fun)(p, EVENT_INPUT);
95             }
96             if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
97                 force_event == EVENT_OUTPUT))
98             {
99                 p->last_event = now;
100                 (*p->fun)(p, EVENT_OUTPUT);
101             }
102             if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
103                 force_event == EVENT_EXCEPT))
104             {
105                 p->last_event = now;
106                 (*p->fun)(p, EVENT_EXCEPT);
107             }
108             if (!p->destroyed && ((p->max_idle && now - p->last_event >
109                 p->max_idle) || force_event == EVENT_TIMEOUT))
110             {
111                 p->last_event = now;
112                 (*p->fun)(p, EVENT_TIMEOUT);
113             }
114         }
115         for (p = *iochans; p; p = nextp)
116         {
117             nextp = p->next;
118
119             if (p->destroyed)
120             {
121                 IOCHAN tmp = p, pr;
122
123                 /* Now reset the pointers */
124                 if (p == *iochans)
125                     *iochans = p->next;
126                 else
127                 {
128                     for (pr = *iochans; pr; pr = pr->next)
129                         if (pr->next == p)
130                             break;
131                     assert(pr); /* grave error if it weren't there */
132                     pr->next = p->next;
133                 }
134                 if (nextp == p)
135                     nextp = p->next;
136                 xfree(tmp);
137             }
138         }
139     }
140     while (*iochans);
141     return 0;
142 }