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.
45 #include <yaz/yconfig.h>
47 #include <yaz/comstack.h>
48 #include <yaz/xmalloc.h>
49 #include <yaz/mutex.h>
52 #include "sel_thread.h"
55 static YAZ_MUTEX g_mutex = 0;
56 static int no_iochans = 0;
57 static int no_iochans_total = 0;
59 static int iochan_use(int delta)
63 yaz_mutex_create(&g_mutex);
64 yaz_mutex_enter(g_mutex);
67 no_iochans_total += delta;
69 yaz_mutex_leave(g_mutex);
70 yaz_log(YLOG_DEBUG, "%s iochans=%d",
71 delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
75 int iochans_count(void)
80 int iochans_count_total(void)
85 yaz_mutex_enter(g_mutex);
86 total = no_iochans_total;
87 yaz_mutex_leave(g_mutex);
92 #define iochans_count(x) 0
93 #define iochans_count_total(x) 0
98 sel_thread_t sel_thread;
102 YAZ_MUTEX iochan_mutex;
104 struct yaz_poll_fd *fds;
107 iochan_man_t iochan_man_create(int no_threads)
109 iochan_man_t man = xmalloc(sizeof(*man));
110 man->channel_list = 0;
111 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
113 man->no_threads = no_threads;
114 man->log_level = yaz_log_module_level("iochan");
115 man->iochan_mutex = 0;
118 yaz_mutex_create(&man->iochan_mutex);
122 IOCHAN iochan_destroy_real(IOCHAN chan)
124 IOCHAN next = chan->next;
132 void iochan_man_destroy(iochan_man_t *mp)
137 if ((*mp)->sel_thread)
138 sel_thread_destroy((*mp)->sel_thread);
140 yaz_mutex_enter((*mp)->iochan_mutex);
141 c = (*mp)->channel_list;
142 (*mp)->channel_list = NULL;
143 yaz_mutex_leave((*mp)->iochan_mutex);
145 c = iochan_destroy_real(c);
146 yaz_mutex_destroy(&(*mp)->iochan_mutex);
153 void iochan_add(iochan_man_t man, IOCHAN chan)
156 yaz_mutex_enter(man->iochan_mutex);
157 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
159 chan->next = man->channel_list;
160 man->channel_list = chan;
161 yaz_mutex_leave(man->iochan_mutex);
164 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)
168 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
171 new_iochan->destroyed = 0;
173 new_iochan->flags = flags;
174 new_iochan->fun = cb;
175 new_iochan->last_event = new_iochan->max_idle = 0;
176 new_iochan->next = NULL;
178 new_iochan->thread_users = 0;
179 new_iochan->name = name ? xstrdup(name) : 0;
183 static void work_handler(void *work_data)
185 IOCHAN p = work_data;
187 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
188 p, p->name ? p->name : "", p->this_event);
190 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
191 (*p->fun)(p, EVENT_TIMEOUT);
192 if (!p->destroyed && (p->this_event & EVENT_INPUT))
193 (*p->fun)(p, EVENT_INPUT);
194 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
195 (*p->fun)(p, EVENT_OUTPUT);
196 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
197 (*p->fun)(p, EVENT_EXCEPT);
199 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
200 p->name ? p->name : "", p->this_event);
203 static void run_fun(iochan_man_t man, IOCHAN p)
207 yaz_log(man->log_level,
208 "eventl: work add chan=%p name=%s event=%d", p,
209 p->name ? p->name : "", p->this_event);
211 sel_thread_add(man->sel_thread, p);
217 static int event_loop(iochan_man_t man, IOCHAN *iochans)
219 do /* loop as long as there are active associations to process */
225 struct yaz_poll_fd *fds;
227 int connection_fired = 0;
230 yaz_mutex_enter(man->iochan_mutex);
231 start = man->channel_list;
232 yaz_mutex_leave(man->iochan_mutex);
234 for (p = start; p; p = p->next)
236 if (man->sel_fd != -1)
238 if (no_fds > man->size_fds)
240 man->size_fds = no_fds * 2;
241 man->fds = xrealloc(man->fds, man->size_fds * sizeof(*man->fds));
245 if (man->sel_fd != -1)
247 fds[i].fd = man->sel_fd;
248 fds[i].input_mask = yaz_poll_read;
249 fds[i].client_data = 0;
252 for (p = start; p; p = p->next, i++)
254 fds[i].client_data = p;
256 fds[i].input_mask = 0;
257 if (p->thread_users > 0)
259 if (p->max_idle && p->max_idle < tv_sec)
260 tv_sec = p->max_idle;
263 if (p->flags & EVENT_INPUT)
264 fds[i].input_mask |= yaz_poll_read;
265 if (p->flags & EVENT_OUTPUT)
266 fds[i].input_mask |= yaz_poll_write;
267 if (p->flags & EVENT_EXCEPT)
268 fds[i].input_mask |= yaz_poll_except;
270 yaz_log(man->log_level, "yaz_poll begin nofds=%d", no_fds);
271 res = yaz_poll(fds, no_fds, tv_sec, 0);
272 yaz_log(man->log_level, "yaz_poll returned res=%d", res);
279 yaz_log(YLOG_ERRNO | YLOG_WARN, "poll");
284 if (man->sel_fd != -1)
286 assert(fds[i].fd == man->sel_fd);
287 if (fds[i].output_mask)
291 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
293 while ((chan = sel_thread_result(man->sel_thread)))
295 yaz_log(man->log_level,
296 "eventl: got thread result chan=%p name=%s", chan,
297 chan->name ? chan->name : "");
298 chan->thread_users--;
306 for (p = start; p; p = p->next)
308 yaz_log(man->log_level, "%d channels", no);
310 for (; i < no_fds; i++)
312 time_t now = time(0);
313 p = fds[i].client_data;
317 yaz_log(man->log_level,
318 "eventl: skip destroyed chan=%p name=%s", p,
319 p->name ? p->name : "");
322 if (p->thread_users > 0)
324 yaz_log(man->log_level,
325 "eventl: skip chan=%p name=%s users=%d", p,
326 p->name ? p->name : "", p->thread_users);
330 if (p->max_idle && now - p->last_event > p->max_idle)
333 p->this_event |= EVENT_TIMEOUT;
337 if (fds[i].output_mask & yaz_poll_read)
340 p->this_event |= EVENT_INPUT;
342 if (fds[i].output_mask & yaz_poll_write)
345 p->this_event |= EVENT_OUTPUT;
347 if (fds[i].output_mask & yaz_poll_except)
350 p->this_event |= EVENT_EXCEPT;
353 /* only fire one Z39.50/SRU socket event.. except for timeout */
356 if (!(p->this_event & EVENT_TIMEOUT) &&
357 !strcmp(p->name, "connection_socket"))
359 /* not a timeout and we have a Z39.50/SRU socket */
360 if (connection_fired == 0)
369 assert(inv_start == start);
370 yaz_mutex_enter(man->iochan_mutex);
371 for (nextp = iochans; *nextp; )
374 if (p->destroyed && p->thread_users == 0)
375 *nextp = iochan_destroy_real(p);
379 yaz_mutex_leave(man->iochan_mutex);
384 void iochan_man_events(iochan_man_t man)
386 if (man->no_threads > 0 && !man->sel_thread)
388 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
389 &man->sel_fd, man->no_threads);
390 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
393 event_loop(man, &man->channel_list);
399 * c-file-style: "Stroustrup"
400 * indent-tabs-mode: nil
402 * vim: shiftwidth=4 tabstop=8 expandtab