cp_pooled_thread_client_interface *
	cp_pooled_thread_client_interface_create
		(cp_pooled_thread_scheduler *owner, 
		 void *client, 
		 int min_threads, 
		 int max_threads,
		 cp_pooled_thread_report_load report_load,
		 cp_pooled_thread_shrink shrink,
		 cp_thread_action action,
		 void *action_prm, 
		 cp_thread_stop_fn stop_fn, 
		 void *stop_prm)
{
	cp_pooled_thread_client_interface *ci = 
		calloc(1, sizeof(cp_pooled_thread_client_interface));
	if (client == NULL)
		cp_fatal(CP_MEMORY_ALLOCATION_FAILURE, "can\'t allocate thread pool client interface");

	ci->owner = owner;
	ci->client = client;
	ci->min = min_threads;
	ci->max = max_threads;
	ci->report_load = report_load;
	ci->shrink = shrink;
	ci->action = action;
	ci->action_prm = action_prm;
	ci->stop_fn = stop_fn;
	ci->stop_prm = stop_prm;

	cp_pooled_thread_scheduler_register_client(owner, ci);

	return ci;
}
Exemple #2
0
static int init_file_service(char *mimetypes_filename, char *doc_path)
{
    int rc = 0;

    if ((rc = checkdir(doc_path)))
	{
        cp_fatal(rc, "can\'t open document root at [%s]", doc_path);
		exit(rc);
	}

    if ((rc = load_mime_types(mimetypes_filename)))
	{
        cp_fatal(rc, "can\'t load mime types from [%s], sorry", 
                 mimetypes_filename); 
		exit(rc);
	}

    document_root = doc_path;
    
    return rc;
}
cp_pooled_thread_scheduler *cp_pooled_thread_scheduler_create(cp_thread_pool *pool)
{
	cp_pooled_thread_scheduler *scheduler = 
		calloc(1, sizeof(cp_pooled_thread_scheduler));
	if (scheduler == NULL)
		cp_fatal(CP_MEMORY_ALLOCATION_FAILURE, "can\'t allocate thread manager");

	scheduler->pool = pool;
	scheduler->client_list = cp_vector_create(20);

#ifdef CP_HAS_SRANDOM
	srandom(time(NULL));
#else
	srand(time(NULL));
#endif

	return scheduler;
}
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;
}