+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;
+ struct yaz_poll_fd *fds;
+};
+
+iochan_man_t iochan_man_create(int no_threads)
+{
+ 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;
+ 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;
+ }
+}
+
+void iochan_add(iochan_man_t man, IOCHAN chan)
+{
+ chan->man = man;
+ yaz_mutex_enter(man->iochan_mutex);
+ yaz_log(man->log_level, "iochan_add : chan=%p channel list=%p", chan,
+ man->channel_list);
+ chan->next = man->channel_list;
+ man->channel_list = chan;
+ yaz_mutex_leave(man->iochan_mutex);
+}
+
+IOCHAN iochan_create(int fd, IOC_CALLBACK cb, int flags, const char *name)