1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2012 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-2012 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) {
199 if (man->sel_thread) {
200 yaz_log(man->log_level,
201 "eventl: work add chan=%p name=%s event=%d", p,
202 p->name ? p->name : "", p->this_event);
204 sel_thread_add(man->sel_thread, p);
210 static int event_loop(iochan_man_t man, IOCHAN *iochans) {
211 do /* loop as long as there are active associations to process */
216 fd_set in, out, except;
218 static struct timeval to;
219 struct timeval *timeout;
221 // struct yaz_poll_fd *fds;
226 timeout = &to; /* hang on select */
230 // INV: start must no change through the loop
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) {
239 // fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
242 for (p = start; p; p = p->next) {
243 if (p->thread_users > 0)
245 if (p->max_idle && p->max_idle < to.tv_sec)
246 to.tv_sec = p->max_idle;
249 if (p->flags & EVENT_INPUT)
251 if (p->flags & EVENT_OUTPUT)
253 if (p->flags & EVENT_EXCEPT)
254 FD_SET(p->fd, &except);
258 yaz_log(man->log_level, "max=%d sel_fd=%d", max, man->sel_fd);
260 if (man->sel_fd != -1) {
261 if (man->sel_fd > max)
263 FD_SET(man->sel_fd, &in);
265 yaz_log(man->log_level, "select begin nofds=%d", max);
266 res = select(max + 1, &in, &out, &except, timeout);
267 yaz_log(man->log_level, "select returned res=%d", res);
272 yaz_log(YLOG_ERRNO | YLOG_WARN, "select");
276 if (man->sel_fd != -1) {
277 if (FD_ISSET(man->sel_fd, &in)) {
280 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
282 while ((chan = sel_thread_result(man->sel_thread))) {
283 yaz_log(man->log_level,
284 "eventl: got thread result chan=%p name=%s", chan,
285 chan->name ? chan->name : "");
286 chan->thread_users--;
290 if (man->log_level) {
292 for (p = start; p; p = p->next) {
295 yaz_log(man->log_level, "%d channels", no);
297 for (p = start; p; p = p->next) {
298 time_t now = time(0);
301 yaz_log(man->log_level,
302 "eventl: skip destroyed chan=%p name=%s", p,
303 p->name ? p->name : "");
306 if (p->thread_users > 0) {
307 yaz_log(man->log_level,
308 "eventl: skip chan=%p name=%s users=%d", p,
309 p->name ? p->name : "", p->thread_users);
314 if (p->max_idle && now - p->last_event > p->max_idle) {
316 p->this_event |= EVENT_TIMEOUT;
319 if (FD_ISSET(p->fd, &in)) {
321 p->this_event |= EVENT_INPUT;
323 if (FD_ISSET(p->fd, &out)) {
325 p->this_event |= EVENT_OUTPUT;
327 if (FD_ISSET(p->fd, &except)) {
329 p->this_event |= EVENT_EXCEPT;
334 assert(inv_start == start);
335 yaz_mutex_enter(man->iochan_mutex);
336 for (nextp = iochans; *nextp;) {
338 if (p->destroyed && p->thread_users == 0) {
339 *nextp = iochan_destroy_real(p);
343 yaz_mutex_leave(man->iochan_mutex);
348 void iochan_man_events(iochan_man_t man) {
349 if (man->no_threads > 0 && !man->sel_thread) {
350 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
351 &man->sel_fd, man->no_threads);
352 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
355 event_loop(man, &man->channel_list);
358 void pazpar2_sleep(double d) {
360 Sleep( (DWORD) (d * 1000));
363 tv.tv_sec = floor(d);
364 tv.tv_usec = (d - floor(d)) * 1000000;
365 select(0, 0, 0, 0, &tv);
372 * c-file-style: "Stroustrup"
373 * indent-tabs-mode: nil
375 * vim: shiftwidth=4 tabstop=8 expandtab