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", delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
81 int iochans_count(void) {
85 int iochans_count_total(void) {
89 yaz_mutex_enter(g_mutex);
90 total = no_iochans_total;
91 yaz_mutex_leave(g_mutex);
96 #define iochans_count(x) 0
97 #define iochans_count_total(x) 0
100 struct iochan_man_s {
102 sel_thread_t sel_thread;
106 YAZ_MUTEX iochan_mutex;
109 iochan_man_t iochan_man_create(int no_threads) {
110 iochan_man_t man = xmalloc(sizeof(*man));
111 man->channel_list = 0;
112 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
114 man->no_threads = no_threads;
115 man->log_level = yaz_log_module_level("iochan");
116 man->iochan_mutex = 0;
117 yaz_mutex_create(&man->iochan_mutex);
121 IOCHAN iochan_destroy_real(IOCHAN chan)
123 IOCHAN next = chan->next;
131 void iochan_man_destroy(iochan_man_t *mp) {
134 if ((*mp)->sel_thread)
135 sel_thread_destroy((*mp)->sel_thread);
137 yaz_mutex_enter((*mp)->iochan_mutex);
138 c = (*mp)->channel_list;
139 (*mp)->channel_list = NULL;
140 yaz_mutex_leave((*mp)->iochan_mutex);
142 c = iochan_destroy_real(c);
144 yaz_mutex_destroy(&(*mp)->iochan_mutex);
150 void iochan_add(iochan_man_t man, IOCHAN chan) {
152 yaz_mutex_enter(man->iochan_mutex);
153 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
155 chan->next = man->channel_list;
156 man->channel_list = chan;
157 yaz_mutex_leave(man->iochan_mutex);
160 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name) {
163 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
166 new_iochan->destroyed = 0;
168 new_iochan->flags = flags;
169 new_iochan->fun = cb;
170 new_iochan->last_event = new_iochan->max_idle = 0;
171 new_iochan->next = NULL;
173 new_iochan->thread_users = 0;
174 new_iochan->name = name ? xstrdup(name) : 0;
178 static void work_handler(void *work_data) {
179 IOCHAN p = work_data;
181 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
182 p, p->name ? p->name : "", p->this_event);
184 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
185 (*p->fun)(p, EVENT_TIMEOUT);
186 if (!p->destroyed && (p->this_event & EVENT_INPUT))
187 (*p->fun)(p, EVENT_INPUT);
188 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
189 (*p->fun)(p, EVENT_OUTPUT);
190 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
191 (*p->fun)(p, EVENT_EXCEPT);
193 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
194 p->name ? p->name : "", p->this_event);
197 static void run_fun(iochan_man_t man, IOCHAN p) {
198 if (man->sel_thread) {
199 yaz_log(man->log_level,
200 "eventl: work add chan=%p name=%s event=%d", p,
201 p->name ? p->name : "", p->this_event);
203 sel_thread_add(man->sel_thread, p);
208 static int event_loop(iochan_man_t man, IOCHAN *iochans) {
209 do /* loop as long as there are active associations to process */
215 static struct timeval to;
217 struct yaz_poll_fd *fds;
219 int connection_fired = 0;
223 // INV: start must no change through the loop
225 yaz_mutex_enter(man->iochan_mutex);
226 start = man->channel_list;
227 yaz_mutex_leave(man->iochan_mutex);
229 for (p = start; p; p = p->next)
232 if (man->sel_fd != -1)
234 fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
236 if (man->sel_fd != -1)
238 fds[i].fd = man->sel_fd;
239 fds[i].input_mask = 0;
240 if (p->flags & EVENT_INPUT)
241 fds[i].input_mask |= yaz_poll_read;
244 for (p = start; p; p = p->next)
246 if (p->thread_users > 0)
248 if (p->max_idle && p->max_idle < to.tv_sec)
249 to.tv_sec = p->max_idle;
253 fds[i].input_mask = 0;
254 if (p->flags & EVENT_INPUT)
255 fds[i].input_mask |= yaz_poll_read;
256 if (p->flags & EVENT_OUTPUT)
257 fds[i].input_mask |= yaz_poll_write;
258 if (p->flags & EVENT_EXCEPT)
259 fds[i].input_mask |= yaz_poll_except;
262 yaz_log(man->log_level, "yaz_poll begin nofds=%d", no_fds);
263 res = yaz_poll(fds, no_fds, to.tv_sec, 0);
264 yaz_log(man->log_level, "yaz_poll returned res=%d", res);
270 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
275 if (man->sel_fd != -1)
277 assert(fds[i].fd == man->sel_fd);
278 if (fds[i].output_mask)
282 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
284 while ((chan = sel_thread_result(man->sel_thread))) {
285 yaz_log(man->log_level,
286 "eventl: got thread result chan=%p name=%s", chan,
287 chan->name ? chan->name : "");
288 chan->thread_users--;
296 for (p = start; p; p = p->next)
298 yaz_log(man->log_level, "%d channels", no);
300 for (p = start; p; p = p->next)
302 time_t now = time(0);
306 yaz_log(man->log_level,
307 "eventl: skip destroyed chan=%p name=%s", p,
308 p->name ? p->name : "");
313 if (p->thread_users > 0)
315 yaz_log(man->log_level,
316 "eventl: skip chan=%p name=%s users=%d", p,
317 p->name ? p->name : "", p->thread_users);
324 if (p->max_idle && now - p->last_event > p->max_idle)
327 p->this_event |= EVENT_TIMEOUT;
331 assert(fds[i].fd == p->fd);
332 if (fds[i].output_mask & yaz_poll_read)
335 p->this_event |= EVENT_INPUT;
337 if (fds[i].output_mask & yaz_poll_write)
340 p->this_event |= EVENT_OUTPUT;
342 if (fds[i].output_mask & yaz_poll_except)
345 p->this_event |= EVENT_EXCEPT;
349 /* only fire one Z39.50/SRU socket event.. except for timeout */
351 if (!(p->this_event & EVENT_TIMEOUT) &&
352 !strcmp(p->name, "connection_socket"))
354 /* not a timeout and we have a Z39.50/SRU socket */
355 if (connection_fired == 0)
364 assert(inv_start == start);
365 yaz_mutex_enter(man->iochan_mutex);
366 for (nextp = iochans; *nextp;) {
368 if (p->destroyed && p->thread_users == 0) {
369 *nextp = iochan_destroy_real(p);
373 yaz_mutex_leave(man->iochan_mutex);
379 void iochan_man_events(iochan_man_t man) {
380 if (man->no_threads > 0 && !man->sel_thread) {
381 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
382 &man->sel_fd, man->no_threads);
383 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
386 event_loop(man, &man->channel_list);
389 void pazpar2_sleep(double d) {
391 Sleep( (DWORD) (d * 1000));
394 tv.tv_sec = floor(d);
395 tv.tv_usec = (d - floor(d)) * 1000000;
396 select(0, 0, 0, 0, &tv);
403 * c-file-style: "Stroustrup"
404 * indent-tabs-mode: nil
406 * vim: shiftwidth=4 tabstop=8 expandtab