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 iochan_man_t iochan_man_create(int no_threads) {
71 iochan_man_t man = xmalloc(sizeof(*man));
72 man->channel_list = 0;
73 man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
75 man->no_threads = no_threads;
76 man->log_level = yaz_log_module_level("iochan");
77 man->iochan_mutex = 0;
78 yaz_mutex_create(&man->iochan_mutex);
82 void iochan_man_destroy(iochan_man_t *mp) {
85 if ((*mp)->sel_thread)
86 sel_thread_destroy((*mp)->sel_thread);
88 yaz_mutex_enter((*mp)->iochan_mutex);
89 c = (*mp)->channel_list;
90 (*mp)->channel_list = NULL;
91 yaz_mutex_leave((*mp)->iochan_mutex);
93 IOCHAN c_next = c->next;
98 yaz_mutex_destroy(&(*mp)->iochan_mutex);
104 void iochan_add(iochan_man_t man, IOCHAN chan) {
106 yaz_mutex_enter(man->iochan_mutex);
107 yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
109 chan->next = man->channel_list;
110 man->channel_list = chan;
111 yaz_mutex_leave(man->iochan_mutex);
114 IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name) {
117 if (!(new_iochan = (IOCHAN) xmalloc(sizeof(*new_iochan))))
119 new_iochan->destroyed = 0;
121 new_iochan->flags = flags;
122 new_iochan->fun = cb;
123 new_iochan->last_event = new_iochan->max_idle = 0;
124 new_iochan->next = NULL;
126 new_iochan->thread_users = 0;
127 new_iochan->name = name ? xstrdup(name) : 0;
131 static void work_handler(void *work_data) {
132 IOCHAN p = work_data;
134 yaz_log(p->man->log_level, "eventl: work begin chan=%p name=%s event=%d",
135 p, p->name ? p->name : "", p->this_event);
137 if (!p->destroyed && (p->this_event & EVENT_TIMEOUT))
138 (*p->fun)(p, EVENT_TIMEOUT);
139 if (!p->destroyed && (p->this_event & EVENT_INPUT))
140 (*p->fun)(p, EVENT_INPUT);
141 if (!p->destroyed && (p->this_event & EVENT_OUTPUT))
142 (*p->fun)(p, EVENT_OUTPUT);
143 if (!p->destroyed && (p->this_event & EVENT_EXCEPT))
144 (*p->fun)(p, EVENT_EXCEPT);
146 yaz_log(p->man->log_level, "eventl: work end chan=%p name=%s event=%d", p,
147 p->name ? p->name : "", p->this_event);
150 static void run_fun(iochan_man_t man, IOCHAN p) {
152 if (man->sel_thread) {
153 yaz_log(man->log_level,
154 "eventl: work add chan=%p name=%s event=%d", p,
155 p->name ? p->name : "", p->this_event);
157 sel_thread_add(man->sel_thread, p);
163 static int event_loop(iochan_man_t man, IOCHAN *iochans) {
164 do /* loop as long as there are active associations to process */
169 fd_set in, out, except;
171 static struct timeval to;
172 struct timeval *timeout;
174 // struct yaz_poll_fd *fds;
179 timeout = &to; /* hang on select */
183 // INV: start must no change through the loop
185 yaz_mutex_enter(man->iochan_mutex);
186 start = man->channel_list;
187 yaz_mutex_leave(man->iochan_mutex);
189 for (p = start; p; p = p->next) {
192 // fds = (struct yaz_poll_fd *) xmalloc(no_fds * sizeof(*fds));
195 for (p = start; p; p = p->next) {
196 if (p->thread_users > 0)
198 if (p->max_idle && p->max_idle < to.tv_sec)
199 to.tv_sec = p->max_idle;
202 if (p->flags & EVENT_INPUT)
204 if (p->flags & EVENT_OUTPUT)
206 if (p->flags & EVENT_EXCEPT)
207 FD_SET(p->fd, &except);
211 yaz_log(man->log_level, "max=%d nofds=%d", max, man->sel_fd);
213 if (man->sel_fd != -1) {
214 if (man->sel_fd > max)
216 FD_SET(man->sel_fd, &in);
218 yaz_log(man->log_level, "select begin nofds=%d", max);
219 res = select(max + 1, &in, &out, &except, timeout);
220 yaz_log(man->log_level, "select returned res=%d", res);
225 yaz_log(YLOG_ERRNO | YLOG_WARN, "select");
229 if (man->sel_fd != -1) {
230 if (FD_ISSET(man->sel_fd, &in)) {
233 yaz_log(man->log_level, "eventl: sel input on sel_fd=%d",
235 while ((chan = sel_thread_result(man->sel_thread))) {
236 yaz_log(man->log_level,
237 "eventl: got thread result chan=%p name=%s", chan,
238 chan->name ? chan->name : "");
239 chan->thread_users--;
243 if (man->log_level) {
245 for (p = start; p; p = p->next) {
248 yaz_log(man->log_level, "%d channels", no);
250 for (p = start; p; p = p->next) {
251 time_t now = time(0);
254 yaz_log(man->log_level,
255 "eventl: skip destroyed chan=%p name=%s", p,
256 p->name ? p->name : "");
259 if (p->thread_users > 0) {
260 yaz_log(man->log_level,
261 "eventl: skip chan=%p name=%s users=%d", p,
262 p->name ? p->name : "", p->thread_users);
267 if (p->max_idle && now - p->last_event > p->max_idle) {
269 p->this_event |= EVENT_TIMEOUT;
272 if (FD_ISSET(p->fd, &in)) {
274 p->this_event |= EVENT_INPUT;
276 if (FD_ISSET(p->fd, &out)) {
278 p->this_event |= EVENT_OUTPUT;
280 if (FD_ISSET(p->fd, &except)) {
282 p->this_event |= EVENT_EXCEPT;
287 assert(inv_start == start);
288 yaz_mutex_enter(man->iochan_mutex);
289 for (nextp = iochans; *nextp;) {
291 if (p->destroyed && p->thread_users == 0) {
298 yaz_mutex_leave(man->iochan_mutex);
303 void iochan_man_events(iochan_man_t man) {
304 if (man->no_threads > 0 && !man->sel_thread) {
305 man->sel_thread = sel_thread_create(work_handler, 0 /*work_destroy */,
306 &man->sel_fd, man->no_threads);
307 yaz_log(man->log_level, "iochan_man_events. Using %d threads",
310 event_loop(man, &man->channel_list);
313 void pazpar2_sleep(double d) {
315 Sleep( (DWORD) (d * 1000));
318 tv.tv_sec = floor(d);
319 tv.tv_usec = (d - floor(d)) * 1000000;
320 select(0, 0, 0, 0, &tv);
327 * c-file-style: "Stroustrup"
328 * indent-tabs-mode: nil
330 * vim: shiftwidth=4 tabstop=8 expandtab