1 /* $Id: eventl.c,v 1.5 2007-04-10 08:48:56 adam Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
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
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
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
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.
30 * Based on revision YAZ' server/eventl.c 1.29.
49 #include <yaz/yconfig.h>
51 #include <yaz/comstack.h>
52 #include <yaz/xmalloc.h>
54 #include <yaz/statserv.h>
56 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
60 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
62 new_iochan->destroyed = 0;
64 new_iochan->flags = flags;
66 new_iochan->force_event = 0;
67 new_iochan->last_event = new_iochan->max_idle = 0;
68 new_iochan->next = NULL;
72 int event_loop(IOCHAN *iochans)
74 do /* loop as long as there are active associations to process */
77 fd_set in, out, except;
79 static struct timeval nullto = {0, 0}, to;
80 struct timeval *timeout;
85 timeout = &to; /* hang on select */
89 for (p = *iochans; p; p = p->next)
94 timeout = &nullto; /* polling select */
95 if (p->flags & EVENT_INPUT)
97 if (p->flags & EVENT_OUTPUT)
99 if (p->flags & EVENT_EXCEPT)
100 FD_SET(p->fd, &except);
104 if ((res = select(max + 1, &in, &out, &except, timeout)) < 0)
111 for (p = *iochans; p; p = p->next)
113 int force_event = p->force_event;
114 time_t now = time(0);
117 if (!p->destroyed && ((p->max_idle && now - p->last_event >
118 p->max_idle) || force_event == EVENT_TIMEOUT))
121 (*p->fun)(p, EVENT_TIMEOUT);
125 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
126 force_event == EVENT_INPUT))
129 (*p->fun)(p, EVENT_INPUT);
131 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
132 force_event == EVENT_OUTPUT))
135 (*p->fun)(p, EVENT_OUTPUT);
137 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
138 force_event == EVENT_EXCEPT))
141 (*p->fun)(p, EVENT_EXCEPT);
144 for (p = *iochans; p; p = nextp)
152 /* Now reset the pointers */
157 for (pr = *iochans; pr; pr = pr->next)
160 assert(pr); /* grave error if it weren't there */
176 * indent-tabs-mode: nil
178 * vim: shiftwidth=4 tabstop=8 expandtab