2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: eventl.c,v 1.34 2002-11-26 16:56:21 adam Exp $
20 #include <yaz/yconfig.h>
22 #include <yaz/comstack.h>
23 #include <yaz/xmalloc.h>
26 #include <yaz/statserv.h>
30 #define YAZ_EV_SELECT pth_select
34 #define YAZ_EV_SELECT select
37 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
41 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
43 new_iochan->destroyed = 0;
45 new_iochan->flags = flags;
47 new_iochan->force_event = 0;
48 new_iochan->last_event = new_iochan->max_idle = 0;
49 new_iochan->next = NULL;
53 int event_loop(IOCHAN *iochans)
55 do /* loop as long as there are active associations to process */
58 fd_set in, out, except;
60 static struct timeval nullto = {0, 0}, to;
61 struct timeval *timeout;
63 if (statserv_must_terminate())
65 for (p = *iochans; p; p = p->next)
66 p->force_event = EVENT_TIMEOUT;
71 timeout = &to; /* hang on select */
75 for (p = *iochans; p; p = p->next)
78 timeout = &nullto; /* polling select */
79 if (p->flags & EVENT_INPUT)
81 if (p->flags & EVENT_OUTPUT)
83 if (p->flags & EVENT_EXCEPT)
84 FD_SET(p->fd, &except);
88 res = YAZ_EV_SELECT(max + 1, &in, &out, &except, timeout);
91 if (yaz_errno() == EINTR)
93 if (statserv_must_terminate())
95 for (p = *iochans; p; p = p->next)
96 p->force_event = EVENT_TIMEOUT;
102 /* Destroy the first member in the chain, and try again */
103 association *assoc = (association *)iochan_getdata(*iochans);
104 COMSTACK conn = assoc->client_link;
107 destroy_association(assoc);
108 iochan_destroy(*iochans);
109 yaz_log(LOG_DEBUG, "error select, destroying iochan %p",
113 for (p = *iochans; p; p = p->next)
115 int force_event = p->force_event;
116 time_t now = time(0);
119 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
120 force_event == EVENT_INPUT))
123 (*p->fun)(p, EVENT_INPUT);
125 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
126 force_event == EVENT_OUTPUT))
129 (*p->fun)(p, EVENT_OUTPUT);
131 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
132 force_event == EVENT_EXCEPT))
135 (*p->fun)(p, EVENT_EXCEPT);
137 if (!p->destroyed && ((p->max_idle && now - p->last_event >
138 p->max_idle) || force_event == EVENT_TIMEOUT))
141 (*p->fun)(p, EVENT_TIMEOUT);
144 for (p = *iochans; p; p = nextp)
152 /* We need to inform the threadlist that this channel has been destroyed */
155 /* Now reset the pointers */
160 for (pr = *iochans; pr; pr = pr->next)
163 assert(pr); /* grave error if it weren't there */