void *writer(void *prm) { int i, num; long count = (long) prm; char kbuf[30]; char *entry; cp_mutex_lock(&start_mutex); while (!running) cp_cond_wait(&start_cond, &start_mutex); cp_mutex_unlock(&start_mutex); for (i = 0; i < count; i++) { sprintf(kbuf, "ENTRY %d", i); entry = strdup(kbuf); num = i % COUNT; if (!silent) printf("writing (%d): %s\n", num, entry); cp_mutex_lock(&lock[i % COUNT]); cp_hashtable_put(t[num], entry, entry); cp_list_append(tl[num], entry); cp_cond_broadcast(&cond[i % COUNT]); cp_mutex_unlock(&lock[i % COUNT]); } return NULL; }
int add_gff_record(gff_record_t* record, gff_file_t *gff_file) { void *result = cp_list_append(gff_file->records, record); if (result != NULL) { LOG_DEBUG_F("record %zu\n", cp_list_item_count(gff_file->records)); } else { LOG_DEBUG_F("record %zu not inserted\n", cp_list_item_count(gff_file->records)); } return result != NULL; }
int add_gff_header_entry(gff_header_entry_t *header_entry, gff_file_t *gff_file) { void *result = cp_list_append(gff_file->header_entries, header_entry); if (result != NULL) { LOG_DEBUG_F("header entry %zu\n", cp_list_item_count(gff_file->header_entries)); } else { LOG_DEBUG_F("header entry %zu not inserted\n", cp_list_item_count(gff_file->header_entries)); } return result != NULL; }
static int cp_thread_pool_set_available(cp_thread_pool *pool, cp_pooled_thread *thread) { cp_mutex_lock(pool->pool_lock); cp_hashlist_remove(pool->in_use, &thread->id); cp_list_append(pool->free_pool, thread); // pool->size--; cp_cond_signal(pool->pool_cond); cp_mutex_unlock(pool->pool_lock); return 0; }
cp_thread_pool *cp_thread_pool_create(int min_size, int max_size) { int rc; cp_thread_pool *pool = calloc(1, sizeof(cp_thread_pool)); if (pool == NULL) cp_fatal(CP_MEMORY_ALLOCATION_FAILURE, "can\'t allocate thread pool structure"); pool->min_size = min_size; pool->max_size = max_size; pool->running = 1; pool->free_pool = cp_list_create(); if (pool->free_pool == NULL) cp_fatal(CP_MEMORY_ALLOCATION_FAILURE, "can\'t allocate thread pool list"); pool->in_use = cp_hashlist_create(10, cp_hash_long, cp_hash_compare_long); if (pool->in_use == NULL) cp_fatal(CP_MEMORY_ALLOCATION_FAILURE, "can\'t allocate thread pool running list"); pool->pool_lock = (cp_mutex *) malloc(sizeof(cp_mutex)); if (pool->pool_lock == NULL) { cp_error(CP_MEMORY_ALLOCATION_FAILURE, "can\'t create mutex"); goto THREAD_POOL_CREATE_CANCEL; } if ((rc = cp_mutex_init(pool->pool_lock, NULL))) { cp_error(rc, "can\'t create mutex"); goto THREAD_POOL_CREATE_CANCEL; } pool->pool_cond = (cp_cond *) malloc(sizeof(cp_cond)); if (pool->pool_cond == NULL) { cp_error(rc, "can\'t create condition variable"); cp_mutex_destroy(pool->pool_lock); free(pool->pool_lock); goto THREAD_POOL_CREATE_CANCEL; } if ((rc = cp_cond_init(pool->pool_cond, NULL))) { cp_error(rc, "can\'t create condition variable"); free(pool->pool_cond); cp_mutex_destroy(pool->pool_lock); free(pool->pool_lock); goto THREAD_POOL_CREATE_CANCEL; } for ( ; pool->size < pool->min_size; pool->size++) { cp_pooled_thread *pt = cp_pooled_thread_create(pool); if (pt == NULL) cp_fatal(CP_THREAD_CREATION_FAILURE, "can\'t create thread pool (created %d threads, minimum pool size is %d", pool->size, pool->min_size); cp_list_append(pool->free_pool, pt); } return pool; THREAD_POOL_CREATE_CANCEL: cp_list_destroy_custom(pool->free_pool, (cp_destructor_fn) cp_pooled_thread_destroy); cp_hashlist_destroy_custom(pool->in_use, NULL, (cp_destructor_fn) cp_pooled_thread_destroy); free(pool); return NULL; }
int cp_db_register_dbms(char *name, void (*shutdown_call)()) { cp_list_append(shutdown_hook, shutdown_call); return cp_list_item_count(shutdown_hook); }