+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 threads %d limit fd %d", no_threads,
+ man->limit_fd);
+ yaz_mutex_create(&man->iochan_mutex);
+ return man;
+}
+
+IOCHAN iochan_destroy_real(IOCHAN chan)
+{
+ IOCHAN next = chan->next;
+ if (chan->name)
+ xfree(chan->name);
+ xfree(chan);
+ iochan_use(-1);
+ return next;
+}
+
+void iochan_man_destroy(iochan_man_t *mp)
+{
+ if (*mp)
+ {
+ IOCHAN c;
+ if ((*mp)->sel_thread)
+ sel_thread_destroy((*mp)->sel_thread);
+
+ yaz_mutex_enter((*mp)->iochan_mutex);
+ c = (*mp)->channel_list;
+ (*mp)->channel_list = NULL;
+ yaz_mutex_leave((*mp)->iochan_mutex);
+ while (c)
+ c = iochan_destroy_real(c);
+ yaz_mutex_destroy(&(*mp)->iochan_mutex);
+ xfree((*mp)->fds);
+ xfree(*mp);
+ *mp = 0;