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.
47 #include <yaz/yconfig.h>
49 #include <yaz/comstack.h>
50 #include <yaz/xmalloc.h>
51 #include <yaz/mutex.h>
54 #include "sel_thread.h"
57 static YAZ_MUTEX g_mutex = 0;
58 static int no_iochans = 0;
59 static int no_iochans_total = 0;
61 static int iochan_use(int delta)
65 yaz_mutex_create(&g_mutex);
66 yaz_mutex_enter(g_mutex);
69 no_iochans_total += delta;
71 yaz_mutex_leave(g_mutex);
72 yaz_log(YLOG_DEBUG, "%s iochans=%d",
73 delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
77 int iochans_count(void)
82 int iochans_count_total(void)
87 yaz_mutex_enter(g_mutex);
88 total = no_iochans_total;
89 yaz_mutex_leave(g_mutex);
94 #define iochans_count(x) 0
95 #define iochans_count_total(x) 0
100 sel_thread_t sel_thread;
104 YAZ_MUTEX iochan_mutex;
106 struct yaz_poll_fd *fds;
109 iochan_man_t iochan_man_create(int no_threads)
111 iochan_man_t man = xmalloc(sizeof(*man));
112 man->channel_list = 0;
113 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
115 man->no_threads = no_threads;
116 man->log_level = yaz_log_module_level("iochan");
117 man->iochan_mutex = 0;
120 yaz_mutex_create(&man->iochan_mutex);
124 IOCHAN iochan_destroy_real(IOCHAN chan)
126 IOCHAN next = chan->next;
134 void iochan_man_destroy(iochan_man_t *mp)
139 if ((*mp)->sel_thread)
140 sel_thread_destroy((*mp)->sel_thread);
142 yaz_mutex_enter((*mp)->iochan_mutex);
143 c = (*mp)->channel_list;
144 (*mp)->channel_list = NULL;
145 yaz_mutex_leave((*mp)->iochan_mutex);
147 c = iochan_destroy_real(c);
148 yaz_mutex_destroy(&(*mp)->iochan_mutex);
155 void iochan_add(iochan_man_t man, IOCHAN chan)
158 yaz_mutex_enter(man->iochan_mutex);
159 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
161 chan->next = man->channel_list;
162 man->channel_list = chan;
163 yaz_mutex_leave(man->iochan_mutex);
166 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
170 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
173 new_iochan->destroyed = 0;
175 new_iochan->flags = flags;
176 new_iochan->fun = cb;
177 new_iochan->last_event = new_iochan->max_idle = 0;
178 new_iochan->next = NULL;
180 new_iochan->thread_users = 0;
181 new_iochan->name = name ? xstrdup(name) : 0;
185 static void work_handler(void *work_data)
187 IOCHAN p = work_data;
189 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
190 p, p->name ? p->name : "", p->this_event);
192 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
193 (*p->fun)(p, EVENT_TIMEOUT);
194 if (!p->destroyed && (p->this_event & EVENT_INPUT))
195 (*p->fun)(p, EVENT_INPUT);
196 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
197 (*p->fun)(p, EVENT_OUTPUT);
198 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
199 (*p->fun)(p, EVENT_EXCEPT);
201 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
202 p->name ? p->name : "", p->this_event);
205 static void run_fun(iochan_man_t man, IOCHAN p)
209 yaz_log(man->log_level,
210 "eventl: work add chan=%p name=%s event=%d", p,
211 p->name ? p->name : "", p->this_event);
213 sel_thread_add(man->sel_thread, p);
219 static int event_loop(iochan_man_t man, IOCHAN *iochans)
221 do /* loop as long as there are active associations to process */
227 struct yaz_poll_fd *fds;
229 int connection_fired = 0;
232 yaz_mutex_enter(man->iochan_mutex);
233 start = man->channel_list;
234 yaz_mutex_leave(man->iochan_mutex);
236 for (p = start; p; p = p->next)
238 if (man->sel_fd != -1)
240 if (no_fds > man->size_fds)
242 man->size_fds = no_fds * 2;
243 man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
247 if (man->sel_fd != -1)
249 fds[i].fd = man->sel_fd;
250 fds[i].input_mask = yaz_poll_read;
251 fds[i].client_data = 0;
254 for (p = start; p; p = p->next, i++)
257 fds[i].client_data = p;
259 fds[i].input_mask = 0;
260 if (p->thread_users > 0)
262 if (p->max_idle && p->max_idle < tv_sec)
263 tv_sec = p->max_idle;
266 if (p->flags & EVENT_INPUT)
267 fds[i].input_mask |= yaz_poll_read;
268 if (p->flags & EVENT_OUTPUT)
269 fds[i].input_mask |= yaz_poll_write;
270 if (p->flags & EVENT_EXCEPT)
271 fds[i].input_mask |= yaz_poll_except;
272 if (fds[i].input_mask)
276 yaz_log(man->log_level, "yaz_poll begin nofds=%d", no_fds);
277 res = yaz_poll(fds, no_fds, tv_sec, 0);
278 yaz_log(man->log_level, "yaz_poll returned res=%d", res);
285 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
289 if (man->sel_fd != -1)
292 assert(fds[i].fd == man->sel_fd);
293 if (fds[i].output_mask)
297 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
299 while ((chan = sel_thread_result(man->sel_thread)))
301 yaz_log(man->log_level,
302 "eventl: got thread result chan=%p name=%s", chan,
303 chan->name ? chan->name : "");
304 chan->thread_users--;
311 for (p = start; p; p = p->next)
313 yaz_log(man->log_level, "%d channels", no);
315 for (p = start; p; p = p->next)
317 time_t now = time(0);
322 yaz_log(man->log_level,
323 "eventl: skip destroyed chan=%p name=%s", p,
324 p->name ? p->name : "");
327 if (p->thread_users > 0)
329 yaz_log(man->log_level,
330 "eventl: skip chan=%p name=%s users=%d", p,
331 p->name ? p->name : "", p->thread_users);
335 if (p->max_idle && now - p->last_event > p->max_idle)
338 p->this_event |= EVENT_TIMEOUT;
340 if (fds[i].fd >= 0 && p->fd == fds[i].fd)
342 if (fds[i].output_mask & yaz_poll_read)
345 p->this_event |= EVENT_INPUT;
347 if (fds[i].output_mask & yaz_poll_write)
350 p->this_event |= EVENT_OUTPUT;
352 if (fds[i].output_mask & yaz_poll_except)
355 p->this_event |= EVENT_EXCEPT;
358 /* only fire one Z39.50/SRU socket event.. except for timeout */
361 if (!(p->this_event & EVENT_TIMEOUT) &&
362 !strcmp(p->name, "connection_socket"))
364 /* not a timeout and we have a Z39.50/SRU socket */
365 if (connection_fired == 0)
374 assert(inv_start == start);
375 yaz_mutex_enter(man->iochan_mutex);
376 for (nextp = iochans; *nextp; )
379 if (p->destroyed && p->thread_users == 0)
380 *nextp = iochan_destroy_real(p);
384 yaz_mutex_leave(man->iochan_mutex);
389 void iochan_man_events(iochan_man_t man)
391 if (man->no_threads > 0 && !man->sel_thread)
393 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
394 &man->sel_fd, man->no_threads);
395 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
398 event_loop(man, &man->channel_list);
404 * c-file-style: "Stroustrup"
405 * indent-tabs-mode: nil
407 * vim: shiftwidth=4 tabstop=8 expandtab