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.
52 #include <yaz/yconfig.h>
54 #include <yaz/comstack.h>
55 #include <yaz/xmalloc.h>
56 #include <yaz/mutex.h>
58 #include "sel_thread.h"
62 sel_thread_t sel_thread;
66 YAZ_MUTEX iochan_mutex;
69 struct iochan_man_iter {
75 iochan_man_t iochan_man_create(int no_threads)
77 iochan_man_t man = xmalloc(sizeof(*man));
78 man->channel_list = 0;
79 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
81 man->no_threads = no_threads;
82 man->log_level = yaz_log_module_level("iochan");
83 man->iochan_mutex = 0;
84 yaz_mutex_create(&man->iochan_mutex);
88 void iochan_man_destroy(iochan_man_t *mp)
93 if ((*mp)->sel_thread)
94 sel_thread_destroy((*mp)->sel_thread);
96 c = (*mp)->channel_list;
99 IOCHAN c_next = c->next;
109 void iochan_add(iochan_man_t man, IOCHAN chan)
112 yaz_mutex_enter(man->iochan_mutex);
113 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan, man->channel_list);
114 chan->next = man->channel_list;
115 man->channel_list = chan;
116 yaz_mutex_leave(man->iochan_mutex);
119 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags,
124 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
126 new_iochan->destroyed = 0;
128 new_iochan->flags = flags;
129 new_iochan->fun = cb;
130 new_iochan->socketfun = NULL;
131 new_iochan->maskfun = NULL;
132 new_iochan->last_event = new_iochan->max_idle = 0;
133 new_iochan->next = NULL;
135 new_iochan->thread_users = 0;
136 new_iochan->name = name ? xstrdup(name) : 0;
140 static void work_handler(void *work_data)
142 IOCHAN p = work_data;
144 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
145 p, p->name ? p->name : "", p->this_event);
147 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
148 (*p->fun)(p, EVENT_TIMEOUT);
149 if (!p->destroyed && (p->this_event & EVENT_INPUT))
150 (*p->fun)(p, EVENT_INPUT);
151 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
152 (*p->fun)(p, EVENT_OUTPUT);
153 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
154 (*p->fun)(p, EVENT_EXCEPT);
156 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d",
157 p, p->name ? p->name : "", p->this_event);
160 static void run_fun(iochan_man_t man, IOCHAN p)
166 yaz_log(man->log_level, "eventl: work add chan=%p name=%s event=%d",
167 p, p->name ? p->name : "", p->this_event);
169 sel_thread_add(man->sel_thread, p);
176 static IOCHAN iochan_man_get_first(struct iochan_man_iter *iter, iochan_man_t man) {
179 yaz_mutex_enter(man->iochan_mutex);
180 iter->current = man->channel_list;
181 yaz_log(man->log_level, "iochan_man_get_first : chan=%p ", iter->current);
183 yaz_mutex_leave(man->iochan_mutex);
184 return iter->current;
187 static IOCHAN iochan_man_get_next(struct iochan_man_iter *iter) {
188 IOCHAN current = NULL, next = NULL;
189 current = iter->current;
192 next = current->next;
193 iter->current = iter->current->next;
195 yaz_log(iter->man->log_level, "iochan_man_get_next : chan=%p next=%p", current, next);
197 yaz_mutex_leave(iter->man->iochan_mutex);
200 return iter->current;
203 static int event_loop(iochan_man_t man, IOCHAN *iochans)
205 do /* loop as long as there are active associations to process */
208 fd_set in, out, except;
210 static struct timeval to;
211 struct timeval *timeout;
212 static struct iochan_man_iter iter;
217 timeout = &to; /* hang on select */
221 for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter) )
223 if (p->thread_users > 0)
226 p->flags = (*p->maskfun)(p);
228 p->fd = (*p->socketfun)(p);
229 if (p->max_idle && p->max_idle < to.tv_sec)
230 to.tv_sec = p->max_idle;
233 if (p->flags & EVENT_INPUT)
235 if (p->flags & EVENT_OUTPUT)
237 if (p->flags & EVENT_EXCEPT)
238 FD_SET(p->fd, &except);
242 yaz_log(man->log_level, "max=%d nofds=%d", max, man->sel_fd);
244 if (man->sel_fd != -1)
246 if (man->sel_fd > max)
248 FD_SET(man->sel_fd, &in);
250 yaz_log(man->log_level, "select begin nofds=%d", max);
251 res = select(max + 1, &in, &out, &except, timeout);
252 yaz_log(man->log_level, "select returned res=%d", res);
259 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
263 if (man->sel_fd != -1)
265 if (FD_ISSET(man->sel_fd, &in))
269 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
271 while ((chan = sel_thread_result(man->sel_thread)))
273 yaz_log(man->log_level, "eventl: got thread result chan=%p name=%s",
274 chan, chan->name ? chan->name : "");
275 chan->thread_users--;
282 for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter)) {
285 yaz_log(man->log_level, "%d channels", no);
287 for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter))
289 time_t now = time(0);
293 yaz_log(man->log_level, "eventl: skip destroyed chan=%p name=%s", p, p->name ? p->name : "");
296 if (p->thread_users > 0)
298 yaz_log(man->log_level, "eventl: skip chan=%p name=%s users=%d", p, p->name ? p->name : "", p->thread_users);
303 if (p->max_idle && now - p->last_event > p->max_idle)
306 p->this_event |= EVENT_TIMEOUT;
310 if (FD_ISSET(p->fd, &in))
313 p->this_event |= EVENT_INPUT;
315 if (FD_ISSET(p->fd, &out))
318 p->this_event |= EVENT_OUTPUT;
320 if (FD_ISSET(p->fd, &except))
323 p->this_event |= EVENT_EXCEPT;
328 yaz_mutex_enter(man->iochan_mutex);
329 for (nextp = iochans; *nextp; )
332 if (p->destroyed && p->thread_users == 0)
341 yaz_mutex_leave(man->iochan_mutex);
347 void iochan_man_events(iochan_man_t man)
349 if (man->no_threads > 0 && !man->sel_thread)
351 man->sel_thread = sel_thread_create(
352 work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
353 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
356 event_loop(man, &man->channel_list);
359 void pazpar2_sleep(double d)
362 Sleep( (DWORD) (d * 1000));
365 tv.tv_sec = floor(d);
366 tv.tv_usec = (d - floor(d)) * 1000000;
367 select(0, 0, 0, 0, &tv);
374 * c-file-style: "Stroustrup"
375 * indent-tabs-mode: nil
377 * vim: shiftwidth=4 tabstop=8 expandtab