void *reader(void *prm) { char *entry; int num = (int) prm; int total = 0; int rc; cp_mutex_lock(&start_mutex); while (!running) cp_cond_wait(&start_cond, &start_mutex); rc = cp_mutex_unlock(&start_mutex); if (rc == 0) write_err("reader"); while (running || cp_hashtable_count(t[num]) > 0) { cp_mutex_lock(&lock[num]); while (running && cp_list_is_empty(tl[num])) cp_cond_wait(&cond[num], &lock[num]); while ((entry = (char *) cp_list_remove_head(tl[num]))) { cp_hashtable_remove(t[num], entry); if (!silent) printf("[%d]: (%ld) entry: %s\n", num, cp_hashtable_count(t[num]), entry); total++; free(entry); } cp_mutex_unlock(&lock[num]); } printf("\n (-) reader %d: processed %d entries\n", num, total); return NULL; }
cp_thread *cp_thread_pool_get_impl(cp_thread_pool *pool, cp_thread_action action, void *action_prm, cp_thread_stop_fn stop_fn, void *stop_prm, int block) { cp_pooled_thread *pt = NULL; cp_mutex_lock(pool->pool_lock); #ifdef __TRACE__ DEBUGMSG("cp_thread_pool_get_impl (%d) pool size = %d max size = %d\n", block, pool->size, pool->max_size); #endif pt = cp_list_remove_head(pool->free_pool); if (pt == NULL) { if (pool->size < pool->max_size) { pt = cp_pooled_thread_create(pool); if (pt) pool->size++; } if (pt == NULL) /* no thread available and poolsize == max */ { if (!block) /* asked not to block, return NULL */ { cp_mutex_unlock(pool->pool_lock); return NULL; } /* wait for a thread to be released to the pool */ #ifdef _WINDOWS cp_mutex_unlock(pool->pool_lock); #endif while (pool->running && cp_list_is_empty(pool->free_pool)) cp_cond_wait(pool->pool_cond, pool->pool_lock); if (pool->running) pt = cp_list_remove_head(pool->free_pool); if (pt == NULL) /* shouldn't be happening except for shutdown */ { cp_mutex_unlock(pool->pool_lock); return NULL; } } } cp_hashlist_append(pool->in_use, &pt->id, pt); cp_mutex_unlock(pool->pool_lock); cp_pooled_thread_run_stoppable_task(pt, action, action_prm, stop_fn, stop_prm); return pt->worker; }
void gff_close(gff_file_t *gff_file, int free_records) { // Free string members free(gff_file->filename); // Free header entries if (!cp_list_is_empty(gff_file->header_entries)) { cp_list_destroy(gff_file->header_entries); } // Free records list if asked to if (free_records) { cp_list_destroy(gff_file->records); } munmap((void*) gff_file->data, gff_file->data_len); free(gff_file); }