1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 Index Data
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
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
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
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.
28 * Based on revision YAZ' server/eventl.c 1.29.
51 #include <yaz/yconfig.h>
53 #include <yaz/comstack.h>
54 #include <yaz/xmalloc.h>
56 #include "sel_thread.h"
60 sel_thread_t sel_thread;
66 iochan_man_t iochan_man_create(int no_threads)
68 iochan_man_t man = xmalloc(sizeof(*man));
69 man->channel_list = 0;
70 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
72 man->no_threads = no_threads;
73 man->log_level = YLOG_DEBUG;
77 void iochan_man_destroy(iochan_man_t *mp)
82 if ((*mp)->sel_thread)
83 sel_thread_destroy((*mp)->sel_thread);
85 c = (*mp)->channel_list;
88 IOCHAN c_next = c->next;
97 void iochan_add(iochan_man_t man, IOCHAN chan)
100 chan->next = man->channel_list;
101 man->channel_list = chan;
104 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
108 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
110 new_iochan->destroyed = 0;
112 new_iochan->flags = flags;
113 new_iochan->fun = cb;
114 new_iochan->socketfun = NULL;
115 new_iochan->maskfun = NULL;
116 new_iochan->force_event = 0;
117 new_iochan->last_event = new_iochan->max_idle = 0;
118 new_iochan->next = NULL;
120 new_iochan->thread_users = 0;
124 static void work_handler(void *work_data)
126 IOCHAN p = work_data;
127 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
128 (*p->fun)(p, EVENT_TIMEOUT);
129 if (!p->destroyed && (p->this_event & EVENT_INPUT))
130 (*p->fun)(p, EVENT_INPUT);
131 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
132 (*p->fun)(p, EVENT_OUTPUT);
133 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
134 (*p->fun)(p, EVENT_EXCEPT);
137 static void run_fun(iochan_man_t man, IOCHAN p)
143 yaz_log(man->log_level, "eventl: add fun chan=%p event=%d",
146 sel_thread_add(man->sel_thread, p);
153 static int event_loop(iochan_man_t man, IOCHAN *iochans)
155 do /* loop as long as there are active associations to process */
158 fd_set in, out, except;
160 static struct timeval nullto = {0, 0}, to;
161 struct timeval *timeout;
166 timeout = &to; /* hang on select */
170 for (p = *iochans; p; p = p->next)
172 if (p->thread_users > 0)
175 p->flags = (*p->maskfun)(p);
177 p->fd = (*p->socketfun)(p);
178 if (p->max_idle && p->max_idle < to.tv_sec)
179 to.tv_sec = p->max_idle;
183 timeout = &nullto; /* polling select */
184 if (p->flags & EVENT_INPUT)
186 if (p->flags & EVENT_OUTPUT)
188 if (p->flags & EVENT_EXCEPT)
189 FD_SET(p->fd, &except);
193 if (man->sel_fd != -1)
195 if (man->sel_fd > max)
197 yaz_log(man->log_level, "select on sel fd=%d", man->sel_fd);
198 FD_SET(man->sel_fd, &in);
200 yaz_log(man->log_level, "select begin");
201 res = select(max + 1, &in, &out, &except, timeout);
202 yaz_log(man->log_level, "select returned res=%d", res);
209 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
213 if (man->sel_fd != -1)
215 if (FD_ISSET(man->sel_fd, &in))
219 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
221 while ((chan = sel_thread_result(man->sel_thread)))
223 yaz_log(man->log_level, "eventl: got thread result p=%p",
225 chan->thread_users--;
229 for (p = *iochans; p; p = p->next)
231 int force_event = p->force_event;
232 time_t now = time(0);
234 if (p->thread_users > 0 || p->destroyed)
236 yaz_log(man->log_level, "eventl: skip chan=%p users=%d", p, p->thread_users);
242 if ((p->max_idle && now - p->last_event > p->max_idle)
243 || force_event == EVENT_TIMEOUT)
246 p->this_event |= EVENT_TIMEOUT;
250 if (FD_ISSET(p->fd, &in) || force_event == EVENT_INPUT)
253 yaz_log(YLOG_DEBUG, "Eventl input event");
254 p->this_event |= EVENT_INPUT;
256 if (FD_ISSET(p->fd, &out) || force_event == EVENT_OUTPUT)
259 yaz_log(YLOG_DEBUG, "Eventl output event");
260 p->this_event |= EVENT_OUTPUT;
262 if (FD_ISSET(p->fd, &except) || force_event == EVENT_EXCEPT)
265 p->this_event |= EVENT_EXCEPT;
270 for (nextp = iochans; *nextp; )
273 if (p->destroyed && p->thread_users == 0)
286 void iochan_man_events(iochan_man_t man)
288 if (man->no_threads > 0 && !man->sel_thread)
290 man->sel_thread = sel_thread_create(
291 work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
292 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
295 event_loop(man, &man->channel_list);
301 * c-file-style: "Stroustrup"
302 * indent-tabs-mode: nil
304 * vim: shiftwidth=4 tabstop=8 expandtab