1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2010 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-2010 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"
63 sel_thread_t sel_thread;
67 YAZ_MUTEX iochan_mutex;
70 struct iochan_man_iter {
76 iochan_man_t iochan_man_create(int no_threads) {
77 iochan_man_t man = xmalloc(sizeof(*man));
78 man->channel_list = 0;
79 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
81 man->no_threads = no_threads;
82 man->log_level = yaz_log_module_level("iochan");
83 man->iochan_mutex = 0;
84 yaz_mutex_create(&man->iochan_mutex);
88 void iochan_man_destroy(iochan_man_t *mp) {
91 if ((*mp)->sel_thread)
92 sel_thread_destroy((*mp)->sel_thread);
94 c = (*mp)->channel_list;
96 IOCHAN c_next = c->next;
106 void iochan_add(iochan_man_t man, IOCHAN chan) {
108 yaz_mutex_enter(man->iochan_mutex);
109 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
111 chan->next = man->channel_list;
112 man->channel_list = chan;
113 yaz_mutex_leave(man->iochan_mutex);
116 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name) {
119 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
121 new_iochan->destroyed = 0;
123 new_iochan->flags = flags;
124 new_iochan->fun = cb;
125 new_iochan->last_event = new_iochan->max_idle = 0;
126 new_iochan->next = NULL;
128 new_iochan->thread_users = 0;
129 new_iochan->name = name ? xstrdup(name) : 0;
133 static void work_handler(void *work_data) {
134 IOCHAN p = work_data;
136 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
137 p, p->name ? p->name : "", p->this_event);
139 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
140 (*p->fun)(p, EVENT_TIMEOUT);
141 if (!p->destroyed && (p->this_event & EVENT_INPUT))
142 (*p->fun)(p, EVENT_INPUT);
143 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
144 (*p->fun)(p, EVENT_OUTPUT);
145 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
146 (*p->fun)(p, EVENT_EXCEPT);
148 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
149 p->name ? p->name : "", p->this_event);
152 static void run_fun(iochan_man_t man, IOCHAN p) {
154 if (man->sel_thread) {
155 yaz_log(man->log_level,
156 "eventl: work add chan=%p name=%s event=%d", p,
157 p->name ? p->name : "", p->this_event);
159 sel_thread_add(man->sel_thread, p);
165 static IOCHAN iochan_man_get_first(struct iochan_man_iter *iter,
169 yaz_mutex_enter(man->iochan_mutex);
170 iter->current = man->channel_list;
171 yaz_log(man->log_level, "iochan_man_get_first : chan=%p ", iter->current);
173 yaz_mutex_leave(man->iochan_mutex);
174 return iter->current;
177 static IOCHAN iochan_man_get_next(struct iochan_man_iter *iter) {
178 IOCHAN current = NULL, next = NULL;
179 current = iter->current;
182 next = current->next;
183 iter->current = iter->current->next;
185 yaz_log(iter->man->log_level,
186 "iochan_man_get_next : chan=%p next=%p", current, next);
188 yaz_mutex_leave(iter->man->iochan_mutex);
191 return iter->current;
194 static int event_loop(iochan_man_t man, IOCHAN *iochans) {
195 do /* loop as long as there are active associations to process */
199 fd_set in, out, except;
201 static struct timeval to;
202 struct timeval *timeout;
203 static struct iochan_man_iter iter;
205 // struct yaz_poll_fd *fds;
210 timeout = &to; /* hang on select */
214 // INV: Start must no change through the loop
216 start = iochan_man_get_first(&iter, man);
217 IOCHAN inv_start = start;
218 for (p = start; p; p = iochan_man_get_next(&iter)) {
221 // fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
224 for (p = start; p; p = p->next) {
225 if (p->thread_users > 0)
227 if (p->max_idle && p->max_idle < to.tv_sec)
228 to.tv_sec = p->max_idle;
231 if (p->flags & EVENT_INPUT)
233 if (p->flags & EVENT_OUTPUT)
235 if (p->flags & EVENT_EXCEPT)
236 FD_SET(p->fd, &except);
240 yaz_log(man->log_level, "max=%d nofds=%d", max, man->sel_fd);
242 if (man->sel_fd != -1) {
243 if (man->sel_fd > max)
245 FD_SET(man->sel_fd, &in);
247 yaz_log(man->log_level, "select begin nofds=%d", max);
248 res = select(max + 1, &in, &out, &except, timeout);
249 yaz_log(man->log_level, "select returned res=%d", res);
254 yaz_log(YLOG_ERRNO | YLOG_WARN, "select");
258 if (man->sel_fd != -1) {
259 if (FD_ISSET(man->sel_fd, &in)) {
262 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
264 while ((chan = sel_thread_result(man->sel_thread))) {
265 yaz_log(man->log_level,
266 "eventl: got thread result chan=%p name=%s", chan,
267 chan->name ? chan->name : "");
268 chan->thread_users--;
272 if (man->log_level) {
274 for (p = iochan_man_get_first(&iter, man); p; p
275 = iochan_man_get_next(&iter)) {
278 yaz_log(man->log_level, "%d channels", no);
280 for (p = start; p; p = p->next) {
281 time_t now = time(0);
284 yaz_log(man->log_level,
285 "eventl: skip destroyed chan=%p name=%s", p,
286 p->name ? p->name : "");
289 if (p->thread_users > 0) {
290 yaz_log(man->log_level,
291 "eventl: skip chan=%p name=%s users=%d", p,
292 p->name ? p->name : "", p->thread_users);
297 if (p->max_idle && now - p->last_event > p->max_idle) {
299 p->this_event |= EVENT_TIMEOUT;
302 if (FD_ISSET(p->fd, &in)) {
304 p->this_event |= EVENT_INPUT;
306 if (FD_ISSET(p->fd, &out)) {
308 p->this_event |= EVENT_OUTPUT;
310 if (FD_ISSET(p->fd, &except)) {
312 p->this_event |= EVENT_EXCEPT;
317 assert(inv_start == start);
318 yaz_mutex_enter(man->iochan_mutex);
319 for (nextp = iochans; *nextp;) {
321 if (p->destroyed && p->thread_users == 0) {
328 yaz_mutex_leave(man->iochan_mutex);
333 void iochan_man_events(iochan_man_t man) {
334 if (man->no_threads > 0 && !man->sel_thread) {
335 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
336 &man->sel_fd, man->no_threads);
337 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
340 event_loop(man, &man->channel_list);
343 void pazpar2_sleep(double d) {
345 Sleep( (DWORD) (d * 1000));
348 tv.tv_sec = floor(d);
349 tv.tv_usec = (d - floor(d)) * 1000000;
350 select(0, 0, 0, 0, &tv);
357 * c-file-style: "Stroustrup"
358 * indent-tabs-mode: nil
360 * vim: shiftwidth=4 tabstop=8 expandtab