/* * Initializes the thread subsystem, creating various worker threads. * * nthreads Number of worker event handler threads to spawn * main_base Event base for main thread */ void thread_init(int nthr, struct event_base *main_base, void (*dispatcher_callback)(int, short, void *)) { int i; nthreads = nthr + 1; pthread_mutex_init(&stats_lock, NULL); pthread_mutex_init(&init_lock, NULL); pthread_cond_init(&init_cond, NULL); pthread_mutex_init(&cqi_freelist_lock, NULL); cqi_freelist = NULL; threads = calloc(nthreads, sizeof(LIBEVENT_THREAD)); if (! threads) { settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL, "Can't allocate thread descriptors: %s", strerror(errno)); exit(1); } thread_ids = calloc(nthreads, sizeof(pthread_t)); if (! thread_ids) { perror("Can't allocate thread descriptors"); exit(1); } setup_dispatcher(main_base, dispatcher_callback); for (i = 0; i < nthreads; i++) { if (!create_notification_pipe(&threads[i])) { exit(1); } threads[i].index = i; setup_thread(&threads[i], i == (nthreads - 1)); } /* Create threads after we've done all the libevent setup. */ for (i = 0; i < nthreads; i++) { create_worker(worker_libevent, &threads[i], &thread_ids[i]); threads[i].thread_id = thread_ids[i]; } tap_thread = &threads[nthreads - 1]; /* Wait for all the threads to set themselves up before returning. */ pthread_mutex_lock(&init_lock); while (init_count < nthreads) { pthread_cond_wait(&init_cond, &init_lock); } pthread_mutex_unlock(&init_lock); }
/** A work queue is a mechanism to allow thread-to-thread * communication in a libevent-based, multithreaded system. * * One thread can send work to another thread. The receiving thread * should be libevent-based, with a processing loop handled by * libevent. * * Use work_queue_init() to initialize a work_queue structure, * where the work_queue structure memory is owned by the caller. * * Returns true on success. */ bool work_queue_init(work_queue *m, struct event_base *event_base) { cb_assert(m != NULL); memset(m, 0, sizeof(work_queue)); cb_mutex_initialize(&m->work_lock); m->work_head = NULL; m->work_tail = NULL; m->num_items = 0; m->tot_sends = 0; m->tot_recvs = 0; m->event_base = event_base; cb_assert(m->event_base != NULL); if (!create_notification_pipe(m)) { return false; } event_set(&m->event, m->recv_fd, EV_READ | EV_PERSIST, work_recv, m); event_base_set(m->event_base, &m->event); if (event_add(&m->event, 0) == 0) { #ifdef WORK_DEBUG moxi_log_write("work_queue_init %x %x %x %d %d %u %llu\n", (int) pthread_self(), (int) m, (int) m->event_base, m->send_fd, m->recv_fd, m->work_head != NULL, m->tot_sends); #endif return true; } #ifdef WORK_DEBUG moxi_log_write("work_queue_init error\n"); #endif return false; }
static void setup_dispatcher(struct event_base *main_base, void (*dispatcher_callback)(int, short, void *)) { memset(&dispatcher_thread, 0, sizeof(dispatcher_thread)); dispatcher_thread.type = DISPATCHER; dispatcher_thread.base = main_base; dispatcher_thread.thread_id = pthread_self(); if (!create_notification_pipe(&dispatcher_thread)) { exit(1); } /* Listen for notifications from other threads */ event_set(&dispatcher_thread.notify_event, dispatcher_thread.notify[0], EV_READ | EV_PERSIST, dispatcher_callback, &dispatcher_callback); event_base_set(dispatcher_thread.base, &dispatcher_thread.notify_event); if (event_add(&dispatcher_thread.notify_event, 0) == -1) { settings.extensions.logger->log(EXTENSION_LOG_WARNING, NULL, "Can't monitor libevent notify pipe\n"); exit(1); } }