cl_threadpool.c revision 219820
137Srgrimes/*
237Srgrimes * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
337Srgrimes * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
437Srgrimes * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
537Srgrimes *
637Srgrimes * This software is available to you under a choice of one of two
737Srgrimes * licenses.  You may choose to be licensed under the terms of the GNU
837Srgrimes * General Public License (GPL) Version 2, available from the file
99306Sbde * COPYING in the main directory of this source tree, or the
1037Srgrimes * OpenIB.org BSD license below:
11646Sdg *
129306Sbde *     Redistribution and use in source and binary forms, with or
13646Sdg *     without modification, are permitted provided that the following
146489Sjoerg *     conditions are met:
156489Sjoerg *
166489Sjoerg *      - Redistributions of source code must retain the above
176489Sjoerg *        copyright notice, this list of conditions and the following
186489Sjoerg *        disclaimer.
199306Sbde *
209306Sbde *      - Redistributions in binary form must reproduce the above
21646Sdg *        copyright notice, this list of conditions and the following
22646Sdg *        disclaimer in the documentation and/or other materials
23646Sdg *        provided with the distribution.
24646Sdg *
25646Sdg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26646Sdg * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27646Sdg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28646Sdg * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29646Sdg * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30646Sdg * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31646Sdg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
326489Sjoerg * SOFTWARE.
33646Sdg *
34646Sdg */
35646Sdg
36646Sdg/*
37646Sdg * Abstract:
38646Sdg *	Implementation of thread pool.
3937Srgrimes *
4018364Sjkh */
4137Srgrimes
4237Srgrimes#if HAVE_CONFIG_H
4337Srgrimes#  include <config.h>
4437Srgrimes#endif				/* HAVE_CONFIG_H */
4537Srgrimes
4637Srgrimes#include <stdlib.h>
4737Srgrimes#include <string.h>
4837Srgrimes#include <pthread.h>
4937Srgrimes#include <complib/cl_threadpool.h>
5037Srgrimes
5137Srgrimesstatic void cleanup_mutex(void *arg)
5237Srgrimes{
53862Sache	pthread_mutex_unlock(&((cl_thread_pool_t *) arg)->mutex);
5437Srgrimes}
55862Sache
5637Srgrimesstatic void *thread_pool_routine(void *context)
57862Sache{
5837Srgrimes	cl_thread_pool_t *p_thread_pool = (cl_thread_pool_t *) context;
59862Sache
6037Srgrimes	do {
61862Sache		pthread_mutex_lock(&p_thread_pool->mutex);
6237Srgrimes		pthread_cleanup_push(cleanup_mutex, p_thread_pool);
63862Sache		while (!p_thread_pool->events)
6437Srgrimes			pthread_cond_wait(&p_thread_pool->cond,
65862Sache					  &p_thread_pool->mutex);
6637Srgrimes		p_thread_pool->events--;
67862Sache		pthread_cleanup_pop(1);
6837Srgrimes		/* The event has been signalled.  Invoke the callback. */
69862Sache		(*p_thread_pool->pfn_callback) (p_thread_pool->context);
7037Srgrimes	} while (1);
71862Sache
72154Srgrimes	return NULL;
73862Sache}
74154Srgrimes
75862Sachecl_status_t
76154Srgrimescl_thread_pool_init(IN cl_thread_pool_t * const p_thread_pool,
77862Sache		    IN unsigned count,
7837Srgrimes		    IN void (*pfn_callback) (void *),
7937Srgrimes		    IN void *context, IN const char *const name)
8029610Sjoerg{
8129610Sjoerg	int i;
8229610Sjoerg
8329610Sjoerg	CL_ASSERT(p_thread_pool);
8429610Sjoerg	CL_ASSERT(pfn_callback);
8529610Sjoerg
8629610Sjoerg	memset(p_thread_pool, 0, sizeof(*p_thread_pool));
8729610Sjoerg
8829610Sjoerg	if (!count)
8929610Sjoerg		count = cl_proc_count();
9029610Sjoerg
9129610Sjoerg	pthread_mutex_init(&p_thread_pool->mutex, NULL);
9229610Sjoerg	pthread_cond_init(&p_thread_pool->cond, NULL);
9329610Sjoerg
9429610Sjoerg	p_thread_pool->events = 0;
9529610Sjoerg
9629610Sjoerg	p_thread_pool->pfn_callback = pfn_callback;
9729610Sjoerg	p_thread_pool->context = context;
9829610Sjoerg
9929610Sjoerg	p_thread_pool->tid = calloc(count, sizeof(*p_thread_pool->tid));
10037Srgrimes	if (!p_thread_pool->tid) {
10137Srgrimes		cl_thread_pool_destroy(p_thread_pool);
10237Srgrimes		return CL_INSUFFICIENT_MEMORY;
10337Srgrimes	}
10437Srgrimes
10537Srgrimes	p_thread_pool->running_count = count;
10637Srgrimes
10737Srgrimes	for (i = 0; i < count; i++) {
10837Srgrimes		if (pthread_create(&p_thread_pool->tid[i], NULL,
10937Srgrimes				   thread_pool_routine, p_thread_pool) < 0) {
11037Srgrimes			cl_thread_pool_destroy(p_thread_pool);
11137Srgrimes			return CL_INSUFFICIENT_RESOURCES;
11237Srgrimes		}
11337Srgrimes	}
11437Srgrimes
11537Srgrimes	return (CL_SUCCESS);
11637Srgrimes}
11737Srgrimes
11837Srgrimesvoid cl_thread_pool_destroy(IN cl_thread_pool_t * const p_thread_pool)
11937Srgrimes{
12037Srgrimes	int i;
12137Srgrimes
12237Srgrimes	CL_ASSERT(p_thread_pool);
12337Srgrimes
12437Srgrimes	for (i = 0; i < p_thread_pool->running_count; i++)
12537Srgrimes		if (p_thread_pool->tid[i])
12637Srgrimes			pthread_cancel(p_thread_pool->tid[i]);
12737Srgrimes
12837Srgrimes	for (i = 0; i < p_thread_pool->running_count; i++)
12937Srgrimes		if (p_thread_pool->tid[i])
13037Srgrimes			pthread_join(p_thread_pool->tid[i], NULL);
13137Srgrimes
13237Srgrimes	p_thread_pool->running_count = 0;
13337Srgrimes	pthread_cond_destroy(&p_thread_pool->cond);
13437Srgrimes	pthread_mutex_destroy(&p_thread_pool->mutex);
135289Srgrimes
13637Srgrimes	p_thread_pool->events = 0;
13737Srgrimes}
13837Srgrimes
13937Srgrimescl_status_t cl_thread_pool_signal(IN cl_thread_pool_t * const p_thread_pool)
14037Srgrimes{
14137Srgrimes	int ret;
14237Srgrimes	CL_ASSERT(p_thread_pool);
14337Srgrimes	pthread_mutex_lock(&p_thread_pool->mutex);
14437Srgrimes	p_thread_pool->events++;
14537Srgrimes	ret = pthread_cond_signal(&p_thread_pool->cond);
14637Srgrimes	pthread_mutex_unlock(&p_thread_pool->mutex);
14737Srgrimes	return ret;
14837Srgrimes}
14937Srgrimes