/** \brief see if we can create and destroy without problems */ static void test_create_destroy(void) { int fd; sel_thread_t p = sel_thread_create(work_handler, 0, &fd, 1); YAZ_CHECK(p); if (!p) return; sel_thread_destroy(p); }
sel_thread_t sel_thread_create(void (*work_handler)(void *work_data), void (*work_destroy)(void *work_data), int *read_fd, int no_of_threads) { int i; NMEM nmem = nmem_create(); sel_thread_t p = nmem_malloc(nmem, sizeof(*p)); assert(work_handler); /* work_destroy may be NULL */ assert(read_fd); assert(no_of_threads >= 1); p->nmem = nmem; #ifdef WIN32 /* use port 12119 temporarily on Windos and hope for the best */ p->spipe = yaz_spipe_create(12119, 0); #else p->spipe = yaz_spipe_create(0, 0); #endif if (!p->spipe) { nmem_destroy(nmem); return 0; } *read_fd = p->read_fd = yaz_spipe_get_read_fd(p->spipe); p->write_fd = yaz_spipe_get_write_fd(p->spipe); p->input_queue = 0; p->output_queue = 0; p->free_queue = 0; p->work_handler = work_handler; p->work_destroy = work_destroy; p->no_threads = 0; /* we if need to destroy */ p->stop_flag = 0; p->mutex = 0; yaz_mutex_create(&p->mutex); yaz_cond_create(&p->input_data); if (p->input_data == 0) /* condition variable could not be created? */ { sel_thread_destroy(p); return 0; } p->no_threads = no_of_threads; p->thread_id = nmem_malloc(nmem, sizeof(*p->thread_id) * p->no_threads); for (i = 0; i < p->no_threads; i++) p->thread_id[i] = yaz_thread_create(sel_thread_handler, p); return p; }
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); *mp = 0; } }
/** brief use the fd for something */ static void test_for_real_work(int no_threads) { int thread_fd; sel_thread_t p = sel_thread_create(work_handler, work_destroy, &thread_fd, no_threads); YAZ_CHECK(p); if (p) { iochan_man_t chan_man = iochan_man_create(10); IOCHAN chan = iochan_create(thread_fd, iochan_handler, EVENT_INPUT|EVENT_TIMEOUT, "test_chan"); iochan_settimeout(chan, 1); iochan_setdata(chan, p); iochan_add(chan_man, chan); iochan_man_events(chan_man); sel_thread_destroy(p); iochan_man_destroy(&chan_man); } }