1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2013 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-2013 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 */
214 fd_set in, out, except;
216 static struct timeval to;
217 struct timeval *timeout;
219 // struct yaz_poll_fd *fds;
224 timeout = &to; /* hang on select */
228 // INV: start must no change through the loop
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) {
237 // fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
240 for (p = start; p; p = p->next) {
241 if (p->thread_users > 0)
243 if (p->max_idle && p->max_idle < to.tv_sec)
244 to.tv_sec = p->max_idle;
247 if (p->flags & EVENT_INPUT)
249 if (p->flags & EVENT_OUTPUT)
251 if (p->flags & EVENT_EXCEPT)
252 FD_SET(p->fd, &except);
256 yaz_log(man->log_level, "max=%d sel_fd=%d", max, man->sel_fd);
258 if (man->sel_fd != -1) {
259 if (man->sel_fd > max)
261 FD_SET(man->sel_fd, &in);
263 yaz_log(man->log_level, "select begin nofds=%d", max);
264 res = select(max + 1, &in, &out, &except, timeout);
265 yaz_log(man->log_level, "select returned res=%d", res);
270 yaz_log(YLOG_ERRNO | YLOG_WARN, "select");
274 if (man->sel_fd != -1) {
275 if (FD_ISSET(man->sel_fd, &in)) {
278 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
280 while ((chan = sel_thread_result(man->sel_thread))) {
281 yaz_log(man->log_level,
282 "eventl: got thread result chan=%p name=%s", chan,
283 chan->name ? chan->name : "");
284 chan->thread_users--;
288 if (man->log_level) {
290 for (p = start; p; p = p->next) {
293 yaz_log(man->log_level, "%d channels", no);
296 for (p = start; p; p = p->next) {
297 time_t now = time(0);
300 yaz_log(man->log_level,
301 "eventl: skip destroyed chan=%p name=%s", p,
302 p->name ? p->name : "");
305 if (p->thread_users > 0) {
306 yaz_log(man->log_level,
307 "eventl: skip chan=%p name=%s users=%d", p,
308 p->name ? p->name : "", p->thread_users);
313 if (p->max_idle && now - p->last_event > p->max_idle) {
315 p->this_event |= EVENT_TIMEOUT;
318 if (FD_ISSET(p->fd, &in)) {
320 p->this_event |= EVENT_INPUT;
322 if (FD_ISSET(p->fd, &out)) {
324 p->this_event |= EVENT_OUTPUT;
326 if (FD_ISSET(p->fd, &except)) {
328 p->this_event |= EVENT_EXCEPT;
331 /* only fire one Z39.50/SRU socket event.. except for timeout */
333 if (!(p->this_event & EVENT_TIMEOUT) &&
334 !strcmp(p->name, "connection_socket")) {
335 /* not a timeout and we have a Z39.50/SRU socket */
343 assert(inv_start == start);
344 yaz_mutex_enter(man->iochan_mutex);
345 for (nextp = iochans; *nextp;) {
347 if (p->destroyed && p->thread_users == 0) {
348 *nextp = iochan_destroy_real(p);
352 yaz_mutex_leave(man->iochan_mutex);
357 void iochan_man_events(iochan_man_t man) {
358 if (man->no_threads > 0 && !man->sel_thread) {
359 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
360 &man->sel_fd, man->no_threads);
361 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
364 event_loop(man, &man->channel_list);
367 void pazpar2_sleep(double d) {
369 Sleep( (DWORD) (d * 1000));
372 tv.tv_sec = floor(d);
373 tv.tv_usec = (d - floor(d)) * 1000000;
374 select(0, 0, 0, 0, &tv);
381 * c-file-style: "Stroustrup"
382 * indent-tabs-mode: nil
384 * vim: shiftwidth=4 tabstop=8 expandtab