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->last_event = new_iochan->max_idle = 0;
131 new_iochan->next = NULL;
133 new_iochan->thread_users = 0;
134 new_iochan->name = name ? xstrdup(name) : 0;
138 static void work_handler(void *work_data)
140 IOCHAN p = work_data;
142 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
143 p, p->name ? p->name : "", p->this_event);
145 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
146 (*p->fun)(p, EVENT_TIMEOUT);
147 if (!p->destroyed && (p->this_event & EVENT_INPUT))
148 (*p->fun)(p, EVENT_INPUT);
149 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
150 (*p->fun)(p, EVENT_OUTPUT);
151 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
152 (*p->fun)(p, EVENT_EXCEPT);
154 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d",
155 p, p->name ? p->name : "", p->this_event);
158 static void run_fun(iochan_man_t man, IOCHAN p)
164 yaz_log(man->log_level, "eventl: work add chan=%p name=%s event=%d",
165 p, p->name ? p->name : "", p->this_event);
167 sel_thread_add(man->sel_thread, p);
174 static IOCHAN iochan_man_get_first(struct iochan_man_iter *iter, iochan_man_t man) {
177 yaz_mutex_enter(man->iochan_mutex);
178 iter->current = man->channel_list;
179 yaz_log(man->log_level, "iochan_man_get_first : chan=%p ", iter->current);
181 yaz_mutex_leave(man->iochan_mutex);
182 return iter->current;
185 static IOCHAN iochan_man_get_next(struct iochan_man_iter *iter) {
186 IOCHAN current = NULL, next = NULL;
187 current = iter->current;
190 next = current->next;
191 iter->current = iter->current->next;
193 yaz_log(iter->man->log_level, "iochan_man_get_next : chan=%p next=%p", current, next);
195 yaz_mutex_leave(iter->man->iochan_mutex);
198 return iter->current;
201 static int event_loop(iochan_man_t man, IOCHAN *iochans)
203 do /* loop as long as there are active associations to process */
206 fd_set in, out, except;
208 static struct timeval to;
209 struct timeval *timeout;
210 static struct iochan_man_iter iter;
215 timeout = &to; /* hang on select */
219 for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter) )
221 if (p->thread_users > 0)
223 if (p->max_idle && p->max_idle < to.tv_sec)
224 to.tv_sec = p->max_idle;
227 if (p->flags & EVENT_INPUT)
229 if (p->flags & EVENT_OUTPUT)
231 if (p->flags & EVENT_EXCEPT)
232 FD_SET(p->fd, &except);
236 yaz_log(man->log_level, "max=%d nofds=%d", max, man->sel_fd);
238 if (man->sel_fd != -1)
240 if (man->sel_fd > max)
242 FD_SET(man->sel_fd, &in);
244 yaz_log(man->log_level, "select begin nofds=%d", max);
245 res = select(max + 1, &in, &out, &except, timeout);
246 yaz_log(man->log_level, "select returned res=%d", res);
253 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
257 if (man->sel_fd != -1)
259 if (FD_ISSET(man->sel_fd, &in))
263 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
265 while ((chan = sel_thread_result(man->sel_thread)))
267 yaz_log(man->log_level, "eventl: got thread result chan=%p name=%s",
268 chan, chan->name ? chan->name : "");
269 chan->thread_users--;
276 for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter)) {
279 yaz_log(man->log_level, "%d channels", no);
281 for (p = iochan_man_get_first(&iter, man); p; p = iochan_man_get_next(&iter))
283 time_t now = time(0);
287 yaz_log(man->log_level, "eventl: skip destroyed chan=%p name=%s", p, p->name ? p->name : "");
290 if (p->thread_users > 0)
292 yaz_log(man->log_level, "eventl: skip chan=%p name=%s users=%d", p, p->name ? p->name : "", p->thread_users);
297 if (p->max_idle && now - p->last_event > p->max_idle)
300 p->this_event |= EVENT_TIMEOUT;
304 if (FD_ISSET(p->fd, &in))
307 p->this_event |= EVENT_INPUT;
309 if (FD_ISSET(p->fd, &out))
312 p->this_event |= EVENT_OUTPUT;
314 if (FD_ISSET(p->fd, &except))
317 p->this_event |= EVENT_EXCEPT;
322 yaz_mutex_enter(man->iochan_mutex);
323 for (nextp = iochans; *nextp; )
326 if (p->destroyed && p->thread_users == 0)
335 yaz_mutex_leave(man->iochan_mutex);
341 void iochan_man_events(iochan_man_t man)
343 if (man->no_threads > 0 && !man->sel_thread)
345 man->sel_thread = sel_thread_create(
346 work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
347 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
350 event_loop(man, &man->channel_list);
353 void pazpar2_sleep(double d)
356 Sleep( (DWORD) (d * 1000));
359 tv.tv_sec = floor(d);
360 tv.tv_usec = (d - floor(d)) * 1000000;
361 select(0, 0, 0, 0, &tv);
368 * c-file-style: "Stroustrup"
369 * indent-tabs-mode: nil
371 * vim: shiftwidth=4 tabstop=8 expandtab