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)
81 if ((*mp)->sel_thread)
82 sel_thread_destroy((*mp)->sel_thread);
88 void iochan_add(iochan_man_t man, IOCHAN chan)
91 chan->next = man->channel_list;
92 man->channel_list = chan;
95 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
99 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
101 new_iochan->destroyed = 0;
103 new_iochan->flags = flags;
104 new_iochan->fun = cb;
105 new_iochan->socketfun = NULL;
106 new_iochan->maskfun = NULL;
107 new_iochan->force_event = 0;
108 new_iochan->last_event = new_iochan->max_idle = 0;
109 new_iochan->next = NULL;
111 new_iochan->thread_users = 0;
115 static void work_handler(void *work_data)
117 IOCHAN p = work_data;
118 (*p->fun)(p, p->this_event);
121 static void run_fun(iochan_man_t man, IOCHAN p, int event)
125 p->this_event = event;
128 yaz_log(man->log_level, "eventl: add fun chan=%p event=%d",
131 sel_thread_add(man->sel_thread, p);
134 (*p->fun)(p, p->this_event);
138 static int event_loop(iochan_man_t man, IOCHAN *iochans)
140 do /* loop as long as there are active associations to process */
143 fd_set in, out, except;
145 static struct timeval nullto = {0, 0}, to;
146 struct timeval *timeout;
151 timeout = &to; /* hang on select */
155 for (p = *iochans; p; p = p->next)
157 if (p->thread_users > 0)
160 p->flags = (*p->maskfun)(p);
162 p->fd = (*p->socketfun)(p);
166 timeout = &nullto; /* polling select */
167 if (p->flags & EVENT_INPUT)
169 if (p->flags & EVENT_OUTPUT)
171 if (p->flags & EVENT_EXCEPT)
172 FD_SET(p->fd, &except);
175 if (p->max_idle && p->max_idle < to.tv_sec)
176 to.tv_sec = p->max_idle;
178 if (man->sel_fd != -1)
180 if (man->sel_fd > max)
182 yaz_log(man->log_level, "select on sel fd=%d", man->sel_fd);
183 FD_SET(man->sel_fd, &in);
185 yaz_log(man->log_level, "select begin");
186 res = select(max + 1, &in, &out, &except, timeout);
187 yaz_log(man->log_level, "select returned res=%d", res);
194 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
198 if (man->sel_fd != -1)
200 if (FD_ISSET(man->sel_fd, &in))
204 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
206 while ((chan = sel_thread_result(man->sel_thread)))
208 yaz_log(man->log_level, "eventl: got thread result p=%p",
210 chan->thread_users--;
214 for (p = *iochans; p; p = p->next)
216 int force_event = p->force_event;
217 time_t now = time(0);
220 if (!p->destroyed && ((p->max_idle && now - p->last_event >
221 p->max_idle) || force_event == EVENT_TIMEOUT))
224 run_fun(man, p, EVENT_TIMEOUT);
228 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
229 force_event == EVENT_INPUT))
232 yaz_log(YLOG_DEBUG, "Eventl input event");
233 run_fun(man, p, EVENT_INPUT);
235 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
236 force_event == EVENT_OUTPUT))
239 yaz_log(YLOG_DEBUG, "Eventl output event");
240 run_fun(man, p, EVENT_OUTPUT);
242 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
243 force_event == EVENT_EXCEPT))
246 run_fun(man, p, EVENT_EXCEPT);
249 for (p = *iochans; p; p = nextp)
257 /* Now reset the pointers */
262 for (pr = *iochans; pr; pr = pr->next)
265 assert(pr); /* grave error if it weren't there */
278 void iochan_man_events(iochan_man_t man)
280 if (man->no_threads > 0 && !man->sel_thread)
282 man->sel_thread = sel_thread_create(
283 work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
284 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
287 event_loop(man, &man->channel_list);
293 * c-file-style: "Stroustrup"
294 * indent-tabs-mode: nil
296 * vim: shiftwidth=4 tabstop=8 expandtab