int init_ring_buffer (buffer_t *buffer, buffer_config_t *config) { assert (buffer != NULL); assert (config != NULL); assert (config->buffer_size > 0); assert (config->consume_threshold <= config->buffer_size && config->consume_threshold >= 1); assert (config->produce_threshold < config->buffer_size && config->produce_threshold >= 0); zlog_category_t *category = zlog_get_category ("ring_buffer"); assert (category != NULL); zlog_debug (category, "Initializing ring buffer with size %d.", config->buffer_size); // malloc zlog_debug (category, "Allocating buffer memory."); buffer->entries = (buffer_entry_t *)malloc(config->buffer_size * sizeof (buffer_entry_t)); assert (buffer->entries != NULL); // reset buffer memset (buffer->entries, sizeof (buffer->entries), 0); // setup constants buffer->size = config->buffer_size; buffer->count = 0; buffer->consumer_index = 0; buffer->producer_index = 0; buffer->shutdown = false; buffer->consume_threshold = config->consume_threshold; buffer->produce_threshold = config->produce_threshold; // setup sync facilities zlog_debug (category, "Initializing mutex."); int status; status = simple_mutex_init (&buffer->mutex, NULL); assert (status == 0); status = simple_cond_init (&buffer->cond_not_full, NULL); assert (status == 0); status = simple_cond_init (&buffer->cond_not_empty, NULL); assert (status == 0); // hooking methods buffer->produce = _produce; buffer->consume = _consume; buffer->timed_consume = _timed_consume; return 0; }
/** * Initialise the polling system we are using for the gateway. * * In this case we are using the Linux epoll mechanism */ void poll_init() { int i; if (epoll_fd != -1) return; if ((epoll_fd = epoll_create(MAX_EVENTS)) == -1) { perror("epoll_create"); exit(-1); } memset(&pollStats, 0, sizeof(pollStats)); memset(&queueStats, 0, sizeof(queueStats)); bitmask_init(&poll_mask); n_threads = config_threadcount(); if ((thread_data = (THREAD_DATA *)malloc(n_threads * sizeof(THREAD_DATA))) != NULL) { for (i = 0; i < n_threads; i++) { thread_data[i].state = THREAD_STOPPED; } } #if MUTEX_EPOLL simple_mutex_init(&epoll_wait_mutex, "epoll_wait_mutex"); #endif hktask_add("Load Average", poll_loadav, NULL, POLL_LOAD_FREQ); n_avg_samples = 15 * 60 / POLL_LOAD_FREQ; avg_samples = (double *)malloc(sizeof(double) * n_avg_samples); for (i = 0; i < n_avg_samples; i++) avg_samples[i] = 0.0; evqp_samples = (int *)malloc(sizeof(int) * n_avg_samples); for (i = 0; i < n_avg_samples; i++) evqp_samples[i] = 0.0; number_poll_spins = config_nbpolls(); max_poll_sleep = config_pollsleep(); #if PROFILE_POLL plog = memlog_create("EventQueueWaitTime", ML_LONG, 10000); #endif }