1 /* This file is part of Pazpar2.
2 Copyright (C) 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) 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>
59 #include "sel_thread.h"
62 static YAZ_MUTEX g_mutex = 0;
63 static int no_iochans = 0;
64 static int no_iochans_total = 0;
66 static int iochan_use(int delta)
70 yaz_mutex_create(&g_mutex);
71 yaz_mutex_enter(g_mutex);
74 no_iochans_total += delta;
76 yaz_mutex_leave(g_mutex);
77 yaz_log(YLOG_DEBUG, "%s iochans=%d",
78 delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
82 int iochans_count(void)
87 int iochans_count_total(void)
92 yaz_mutex_enter(g_mutex);
93 total = no_iochans_total;
94 yaz_mutex_leave(g_mutex);
99 #define iochans_count(x) 0
100 #define iochans_count_total(x) 0
103 struct iochan_man_s {
105 sel_thread_t sel_thread;
109 YAZ_MUTEX iochan_mutex;
111 struct yaz_poll_fd *fds;
114 iochan_man_t iochan_man_create(int no_threads)
116 iochan_man_t man = xmalloc(sizeof(*man));
117 man->channel_list = 0;
118 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
120 man->no_threads = no_threads;
121 man->log_level = yaz_log_module_level("iochan");
122 man->iochan_mutex = 0;
125 yaz_mutex_create(&man->iochan_mutex);
129 IOCHAN iochan_destroy_real(IOCHAN chan)
131 IOCHAN next = chan->next;
139 void iochan_man_destroy(iochan_man_t *mp)
144 if ((*mp)->sel_thread)
145 sel_thread_destroy((*mp)->sel_thread);
147 yaz_mutex_enter((*mp)->iochan_mutex);
148 c = (*mp)->channel_list;
149 (*mp)->channel_list = NULL;
150 yaz_mutex_leave((*mp)->iochan_mutex);
152 c = iochan_destroy_real(c);
153 yaz_mutex_destroy(&(*mp)->iochan_mutex);
160 void iochan_add(iochan_man_t man, IOCHAN chan)
163 yaz_mutex_enter(man->iochan_mutex);
164 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
166 chan->next = man->channel_list;
167 man->channel_list = chan;
168 yaz_mutex_leave(man->iochan_mutex);
171 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
175 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
178 new_iochan->destroyed = 0;
180 new_iochan->flags = flags;
181 new_iochan->fun = cb;
182 new_iochan->last_event = new_iochan->max_idle = 0;
183 new_iochan->next = NULL;
185 new_iochan->thread_users = 0;
186 new_iochan->name = name ? xstrdup(name) : 0;
190 static void work_handler(void *work_data)
192 IOCHAN p = work_data;
194 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
195 p, p->name ? p->name : "", p->this_event);
197 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
198 (*p->fun)(p, EVENT_TIMEOUT);
199 if (!p->destroyed && (p->this_event & EVENT_INPUT))
200 (*p->fun)(p, EVENT_INPUT);
201 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
202 (*p->fun)(p, EVENT_OUTPUT);
203 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
204 (*p->fun)(p, EVENT_EXCEPT);
206 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
207 p->name ? p->name : "", p->this_event);
210 static void run_fun(iochan_man_t man, IOCHAN p)
214 yaz_log(man->log_level,
215 "eventl: work add chan=%p name=%s event=%d", p,
216 p->name ? p->name : "", p->this_event);
218 sel_thread_add(man->sel_thread, p);
224 static int event_loop(iochan_man_t man, IOCHAN *iochans)
226 do /* loop as long as there are active associations to process */
232 static struct timeval to;
233 struct yaz_poll_fd *fds;
235 int connection_fired = 0;
239 yaz_mutex_enter(man->iochan_mutex);
240 start = man->channel_list;
241 yaz_mutex_leave(man->iochan_mutex);
243 for (p = start; p; p = p->next)
245 if (man->sel_fd != -1)
247 if (no_fds > man->size_fds)
249 man->size_fds = no_fds * 2;
250 man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
254 if (man->sel_fd != -1)
256 fds[i].fd = man->sel_fd;
257 fds[i].input_mask = yaz_poll_read;
258 fds[i].client_data = 0;
261 for (p = start; p; p = p->next, i++)
263 fds[i].client_data = p;
265 fds[i].input_mask = 0;
266 if (p->thread_users > 0)
268 if (p->max_idle && p->max_idle < to.tv_sec)
269 to.tv_sec = p->max_idle;
272 if (p->flags & EVENT_INPUT)
273 fds[i].input_mask |= yaz_poll_read;
274 if (p->flags & EVENT_OUTPUT)
275 fds[i].input_mask |= yaz_poll_write;
276 if (p->flags & EVENT_EXCEPT)
277 fds[i].input_mask |= yaz_poll_except;
279 yaz_log(man->log_level, "yaz_poll begin nofds=%d", no_fds);
280 res = yaz_poll(fds, no_fds, to.tv_sec, 0);
281 yaz_log(man->log_level, "yaz_poll returned res=%d", res);
288 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
293 if (man->sel_fd != -1)
295 assert(fds[i].fd == man->sel_fd);
296 if (fds[i].output_mask)
300 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
302 while ((chan = sel_thread_result(man->sel_thread)))
304 yaz_log(man->log_level,
305 "eventl: got thread result chan=%p name=%s", chan,
306 chan->name ? chan->name : "");
307 chan->thread_users--;
315 for (p = start; p; p = p->next)
317 yaz_log(man->log_level, "%d channels", no);
319 for (; i < no_fds; i++)
321 time_t now = time(0);
322 p = fds[i].client_data;
326 yaz_log(man->log_level,
327 "eventl: skip destroyed chan=%p name=%s", p,
328 p->name ? p->name : "");
331 if (p->thread_users > 0)
333 yaz_log(man->log_level,
334 "eventl: skip chan=%p name=%s users=%d", p,
335 p->name ? p->name : "", p->thread_users);
339 if (p->max_idle && now - p->last_event > p->max_idle)
342 p->this_event |= EVENT_TIMEOUT;
346 if (fds[i].output_mask & yaz_poll_read)
349 p->this_event |= EVENT_INPUT;
351 if (fds[i].output_mask & yaz_poll_write)
354 p->this_event |= EVENT_OUTPUT;
356 if (fds[i].output_mask & yaz_poll_except)
359 p->this_event |= EVENT_EXCEPT;
362 /* only fire one Z39.50/SRU socket event.. except for timeout */
365 if (!(p->this_event & EVENT_TIMEOUT) &&
366 !strcmp(p->name, "connection_socket"))
368 /* not a timeout and we have a Z39.50/SRU socket */
369 if (connection_fired == 0)
378 assert(inv_start == start);
379 yaz_mutex_enter(man->iochan_mutex);
380 for (nextp = iochans; *nextp; )
383 if (p->destroyed && p->thread_users == 0)
384 *nextp = iochan_destroy_real(p);
388 yaz_mutex_leave(man->iochan_mutex);
393 void iochan_man_events(iochan_man_t man)
395 if (man->no_threads > 0 && !man->sel_thread)
397 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
398 &man->sel_fd, man->no_threads);
399 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
402 event_loop(man, &man->channel_list);
405 void pazpar2_sleep(double d)
408 Sleep( (DWORD) (d * 1000));
411 tv.tv_sec = floor(d);
412 tv.tv_usec = (d - floor(d)) * 1000000;
413 select(0, 0, 0, 0, &tv);
420 * c-file-style: "Stroustrup"
421 * indent-tabs-mode: nil
423 * vim: shiftwidth=4 tabstop=8 expandtab