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>
57 #include "sel_thread.h"
61 sel_thread_t sel_thread;
67 iochan_man_t iochan_man_create(int no_threads)
69 iochan_man_t man = xmalloc(sizeof(*man));
70 man->channel_list = 0;
71 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
73 man->no_threads = no_threads;
74 man->log_level = yaz_log_module_level("iochan");
79 void iochan_man_destroy(iochan_man_t *mp)
84 if ((*mp)->sel_thread)
85 sel_thread_destroy((*mp)->sel_thread);
87 c = (*mp)->channel_list;
90 IOCHAN c_next = c->next;
100 void iochan_add(iochan_man_t man, IOCHAN chan)
103 chan->next = man->channel_list;
104 man->channel_list = chan;
107 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags,
112 if (!(new_iochan = (IOCHAN)xmalloc(sizeof(*new_iochan))))
114 new_iochan->destroyed = 0;
116 new_iochan->flags = flags;
117 new_iochan->fun = cb;
118 new_iochan->last_event = new_iochan->max_idle = 0;
119 new_iochan->next = NULL;
121 new_iochan->thread_users = 0;
122 new_iochan->name = name ? xstrdup(name) : 0;
126 static void work_handler(void *work_data)
128 IOCHAN p = work_data;
130 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
131 p, p->name ? p->name : "", p->this_event);
133 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
134 (*p->fun)(p, EVENT_TIMEOUT);
135 if (!p->destroyed && (p->this_event & EVENT_INPUT))
136 (*p->fun)(p, EVENT_INPUT);
137 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
138 (*p->fun)(p, EVENT_OUTPUT);
139 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
140 (*p->fun)(p, EVENT_EXCEPT);
142 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d",
143 p, p->name ? p->name : "", p->this_event);
146 static void run_fun(iochan_man_t man, IOCHAN p)
152 yaz_log(man->log_level, "eventl: work add chan=%p name=%s event=%d",
153 p, p->name ? p->name : "", p->this_event);
155 sel_thread_add(man->sel_thread, p);
162 static int event_loop(iochan_man_t man, IOCHAN *iochans)
164 do /* loop as long as there are active associations to process */
167 fd_set in, out, except;
169 static struct timeval to;
170 struct timeval *timeout;
175 timeout = &to; /* hang on select */
179 for (p = *iochans; p; p = p->next)
181 if (p->thread_users > 0)
183 if (p->max_idle && p->max_idle < to.tv_sec)
184 to.tv_sec = p->max_idle;
187 if (p->flags & EVENT_INPUT)
189 if (p->flags & EVENT_OUTPUT)
191 if (p->flags & EVENT_EXCEPT)
192 FD_SET(p->fd, &except);
196 if (man->sel_fd != -1)
198 if (man->sel_fd > max)
200 FD_SET(man->sel_fd, &in);
202 yaz_log(man->log_level, "select begin nofds=%d", max);
203 res = select(max + 1, &in, &out, &except, timeout);
204 yaz_log(man->log_level, "select returned res=%d", res);
211 yaz_log(YLOG_ERRNO|YLOG_WARN, "select");
215 if (man->sel_fd != -1)
217 if (FD_ISSET(man->sel_fd, &in))
221 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
223 while ((chan = sel_thread_result(man->sel_thread)))
225 yaz_log(man->log_level, "eventl: got thread result chan=%p name=%s",
226 chan, chan->name ? chan->name : "");
227 chan->thread_users--;
234 for (p = *iochans; p; p = p->next)
236 yaz_log(man->log_level, "%d channels", no);
238 for (p = *iochans; p; p = p->next)
240 time_t now = time(0);
244 yaz_log(man->log_level, "eventl: skip destroyed chan=%p name=%s", p, p->name ? p->name : "");
247 if (p->thread_users > 0)
249 yaz_log(man->log_level, "eventl: skip chan=%p name=%s users=%d", p, p->name ? p->name : "", p->thread_users);
254 if (p->max_idle && now - p->last_event > p->max_idle)
257 p->this_event |= EVENT_TIMEOUT;
261 if (FD_ISSET(p->fd, &in))
264 p->this_event |= EVENT_INPUT;
266 if (FD_ISSET(p->fd, &out))
269 p->this_event |= EVENT_OUTPUT;
271 if (FD_ISSET(p->fd, &except))
274 p->this_event |= EVENT_EXCEPT;
279 for (nextp = iochans; *nextp; )
282 if (p->destroyed && p->thread_users == 0)
296 void iochan_man_events(iochan_man_t man)
298 if (man->no_threads > 0 && !man->sel_thread)
300 man->sel_thread = sel_thread_create(
301 work_handler, 0 /*work_destroy */, &man->sel_fd, man->no_threads);
302 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
305 event_loop(man, &man->channel_list);
308 void pazpar2_sleep(double d)
311 Sleep( (DWORD) (d * 1000));
314 tv.tv_sec = floor(d);
315 tv.tv_usec = (d - floor(d)) * 1000000;
316 select(0, 0, 0, 0, &tv);
323 * c-file-style: "Stroustrup"
324 * indent-tabs-mode: nil
326 * vim: shiftwidth=4 tabstop=8 expandtab