static void* logger_main(void *arg) { qlogger_t *thread; thread = (qlogger_t*)arg; thread->running = 1; (*thread->done)(); qengine_loop(thread->engine); qmailbox_free(&logger->box); qengine_destroy(logger->engine); destroy_logs(); qlog_destroy_free_list(); return NULL; }
int qlog_thread_new(qmem_pool_t *pool, int thread_num) { int i; int result, fd; g_log_thread = qalloc(pool, sizeof(qlog_thread_t)); if (g_log_thread == NULL) { return -1; } if (pthread_key_create(&g_thread_log_key, log_key_destroy) < 0) { return -1; } g_log_thread->engine = qengine_new(pool); if (g_log_thread->engine == NULL) { qfree(pool, g_log_thread, sizeof(qlog_thread_t)); g_log_thread = NULL; return -1; } g_log_thread->thread_num = thread_num; g_log_thread->signals = qalloc(pool, thread_num * sizeof(qsignal_t*)); if (g_log_thread->signals == NULL) { qengine_destroy(g_log_thread->engine); qfree(pool, g_log_thread, sizeof(qlog_thread_t)); g_log_thread = NULL; return -1; } for (i = 0; i < thread_num; ++i) { g_log_thread->signals[i] = qsignal_new(pool); fd = qsignal_get_fd(g_log_thread->signals[i]); qengine_add_event(g_log_thread->engine, fd, QEVENT_READ, thread_log_box, g_log_thread->signals[i]); } g_log_thread->started = 0; result = pthread_create(&g_log_thread->id, NULL, main_loop, g_log_thread); qassert(result == 0); /* ugly, but works */ while (g_log_thread->started == 0) { usleep(100); } return 0; }
static void* main_loop(void *arg) { int i, fd; qsignal_t *signal; qlog_thread_t *thread; thread = (qlog_thread_t*)arg; thread->started = 1; while (!thread->stop && qengine_loop(thread->engine) == 0) { } /* * now the server terminate, do the clean work */ for (i = 0; i < g_log_thread->thread_num; ++i) { signal = g_log_thread->signals[i]; fd = qsignal_get_fd(signal); qengine_del_event(g_log_thread->engine, fd, QEVENT_READ); thread_log_box(0, -1, signal); } qengine_destroy(g_log_thread->engine); return NULL; }