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;
65 iochan_man_t iochan_man_create(int use_threads)
67 iochan_man_t man = xmalloc(sizeof(*man));
68 man->channel_list = 0;
69 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
71 man->use_threads = use_threads;
75 void iochan_man_destroy(iochan_man_t *mp)
79 if ((*mp)->sel_thread)
80 sel_thread_destroy((*mp)->sel_thread);
86 void iochan_add(iochan_man_t man, IOCHAN chan)
89 chan->next = man->channel_list;
90 man->channel_list = chan;
93 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags)
97 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
99 new_iochan->destroyed = 0;
101 new_iochan->flags = flags;
102 new_iochan->fun = cb;
103 new_iochan->socketfun = NULL;
104 new_iochan->maskfun = NULL;
105 new_iochan->force_event = 0;
106 new_iochan->last_event = new_iochan->max_idle = 0;
107 new_iochan->next = NULL;
109 new_iochan->thread_users = 0;
113 static void work_handler(void *work_data)
115 IOCHAN p = work_data;
116 (*p->fun)(p, p->this_event);
119 static void run_fun(iochan_man_t man, IOCHAN p, int event)
123 p->this_event = event;
126 yaz_log(YLOG_LOG, "eventl: add fun chan=%p event=%d",
129 sel_thread_add(man->sel_thread, p);
132 (*p->fun)(p, p->this_event);
136 static int event_loop(iochan_man_t man, IOCHAN *iochans)
138 do /* loop as long as there are active associations to process */
141 fd_set in, out, except;
143 static struct timeval nullto = {0, 0}, to;
144 struct timeval *timeout;
149 timeout = &to; /* hang on select */
153 for (p = *iochans; p; p = p->next)
155 if (p->thread_users > 0)
158 p->flags = (*p->maskfun)(p);
160 p->fd = (*p->socketfun)(p);
164 timeout = &nullto; /* polling select */
165 if (p->flags & EVENT_INPUT)
167 if (p->flags & EVENT_OUTPUT)
169 if (p->flags & EVENT_EXCEPT)
170 FD_SET(p->fd, &except);
173 if (p->max_idle && p->max_idle < to.tv_sec)
174 to.tv_sec = p->max_idle;
176 if (man->sel_fd != -1)
178 if (man->sel_fd > max)
180 yaz_log(YLOG_LOG, "select on sel fd=%d", man->sel_fd);
181 FD_SET(man->sel_fd, &in);
183 yaz_log(YLOG_LOG, "select begin");
184 res = select(max + 1, &in, &out, &except, timeout);
185 yaz_log(YLOG_LOG, "select returned res=%d", res);
192 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
196 if (man->sel_fd != -1)
198 if (FD_ISSET(man->sel_fd, &in))
202 yaz_log(YLOG_LOG, "eventl: sel input on sel_fd=%d",
204 while ((chan = sel_thread_result(man->sel_thread)))
206 yaz_log(YLOG_LOG, "eventl: got thread result p=%p",
208 chan->thread_users--;
212 for (p = *iochans; p; p = p->next)
214 int force_event = p->force_event;
215 time_t now = time(0);
218 if (!p->destroyed && ((p->max_idle && now - p->last_event >
219 p->max_idle) || force_event == EVENT_TIMEOUT))
222 run_fun(man, p, EVENT_TIMEOUT);
226 if (!p->destroyed && (FD_ISSET(p->fd, &in) ||
227 force_event == EVENT_INPUT))
230 yaz_log(YLOG_DEBUG, "Eventl input event");
231 run_fun(man, p, EVENT_INPUT);
233 if (!p->destroyed && (FD_ISSET(p->fd, &out) ||
234 force_event == EVENT_OUTPUT))
237 yaz_log(YLOG_DEBUG, "Eventl output event");
238 run_fun(man, p, EVENT_OUTPUT);
240 if (!p->destroyed && (FD_ISSET(p->fd, &except) ||
241 force_event == EVENT_EXCEPT))
244 run_fun(man, p, EVENT_EXCEPT);
247 for (p = *iochans; p; p = nextp)
255 /* Now reset the pointers */
260 for (pr = *iochans; pr; pr = pr->next)
263 assert(pr); /* grave error if it weren't there */
276 void iochan_man_events(iochan_man_t man)
278 if (man->use_threads && !man->sel_thread)
280 man->sel_thread = sel_thread_create(
281 work_handler, 0 /*work_destroy */, &man->sel_fd, 10);
282 yaz_log(YLOG_LOG, "iochan_man_events. sel_thread started");
284 event_loop(man, &man->channel_list);
290 * c-file-style: "Stroustrup"
291 * indent-tabs-mode: nil
293 * vim: shiftwidth=4 tabstop=8 expandtab