+#include "sel_thread.h"
+
+#if 1
+static YAZ_MUTEX g_mutex = 0;
+static int no_iochans = 0;
+static int no_iochans_total = 0;
+
+static int iochan_use(int delta)
+{
+ int iochans;
+ if (!g_mutex)
+ yaz_mutex_create(&g_mutex);
+ yaz_mutex_enter(g_mutex);
+ no_iochans += delta;
+ if (delta > 0)
+ no_iochans_total += delta;
+ iochans = no_iochans;
+ yaz_mutex_leave(g_mutex);
+ yaz_log(YLOG_DEBUG, "%s iochans=%d",
+ delta == 0 ? "" : (delta > 0 ? "INC" : "DEC"), iochans);
+ return iochans;
+}
+
+int iochans_count(void)
+{
+ return iochan_use(0);
+}
+
+int iochans_count_total(void)
+{
+ int total = 0;
+ if (!g_mutex)
+ return 0;
+ yaz_mutex_enter(g_mutex);
+ total = no_iochans_total;
+ yaz_mutex_leave(g_mutex);
+ return total;
+}
+#else
+#define iochan_use(x)
+#define iochans_count(x) 0
+#define iochans_count_total(x) 0
+#endif
+
+struct iochan_man_s {
+ IOCHAN channel_list;
+ sel_thread_t sel_thread;
+ int sel_fd;
+ int no_threads;
+ int log_level;
+ YAZ_MUTEX iochan_mutex;
+ int size_fds;
+ int limit_fd;
+ struct yaz_poll_fd *fds;
+};
+
+iochan_man_t iochan_man_create(int no_threads, int max_sockets)
+{
+ iochan_man_t man = xmalloc(sizeof(*man));
+ man->channel_list = 0;
+ man->sel_thread = 0; /* can't create sel_thread yet because we may fork */
+ man->sel_fd = -1;
+ man->no_threads = no_threads;
+ man->log_level = yaz_log_module_level("iochan");
+ man->iochan_mutex = 0;
+ man->size_fds = 0;
+ man->fds = 0;
+ man->limit_fd = 0;
+#if HAVE_GETRLIMIT
+ {
+ struct rlimit limit_data;
+ getrlimit(RLIMIT_NOFILE, &limit_data);
+ yaz_log(YLOG_LOG, "getrlimit NOFILE cur=%ld max=%ld",
+ (long) limit_data.rlim_cur, (long) limit_data.rlim_max);
+ man->limit_fd = limit_data.rlim_cur - 200;
+ }
+#endif
+ if (max_sockets)
+ man->limit_fd = max_sockets;
+ yaz_log(YLOG_LOG, "iochan max threads %d max sockets %d",
+ no_threads, max_sockets);
+ yaz_mutex_create(&man->iochan_mutex);
+ return man;
+}