xref: /freebsd/contrib/ofed/opensm/complib/cl_threadpool.c (revision 87181516ef48be852d5e5fee53c6e0dbfc62f21e)
1*d6b92ffaSHans Petter Selasky /*
2*d6b92ffaSHans Petter Selasky  * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
3*d6b92ffaSHans Petter Selasky  * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4*d6b92ffaSHans Petter Selasky  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5*d6b92ffaSHans Petter Selasky  *
6*d6b92ffaSHans Petter Selasky  * This software is available to you under a choice of one of two
7*d6b92ffaSHans Petter Selasky  * licenses.  You may choose to be licensed under the terms of the GNU
8*d6b92ffaSHans Petter Selasky  * General Public License (GPL) Version 2, available from the file
9*d6b92ffaSHans Petter Selasky  * COPYING in the main directory of this source tree, or the
10*d6b92ffaSHans Petter Selasky  * OpenIB.org BSD license below:
11*d6b92ffaSHans Petter Selasky  *
12*d6b92ffaSHans Petter Selasky  *     Redistribution and use in source and binary forms, with or
13*d6b92ffaSHans Petter Selasky  *     without modification, are permitted provided that the following
14*d6b92ffaSHans Petter Selasky  *     conditions are met:
15*d6b92ffaSHans Petter Selasky  *
16*d6b92ffaSHans Petter Selasky  *      - Redistributions of source code must retain the above
17*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
18*d6b92ffaSHans Petter Selasky  *        disclaimer.
19*d6b92ffaSHans Petter Selasky  *
20*d6b92ffaSHans Petter Selasky  *      - Redistributions in binary form must reproduce the above
21*d6b92ffaSHans Petter Selasky  *        copyright notice, this list of conditions and the following
22*d6b92ffaSHans Petter Selasky  *        disclaimer in the documentation and/or other materials
23*d6b92ffaSHans Petter Selasky  *        provided with the distribution.
24*d6b92ffaSHans Petter Selasky  *
25*d6b92ffaSHans Petter Selasky  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*d6b92ffaSHans Petter Selasky  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*d6b92ffaSHans Petter Selasky  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*d6b92ffaSHans Petter Selasky  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*d6b92ffaSHans Petter Selasky  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*d6b92ffaSHans Petter Selasky  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*d6b92ffaSHans Petter Selasky  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*d6b92ffaSHans Petter Selasky  * SOFTWARE.
33*d6b92ffaSHans Petter Selasky  *
34*d6b92ffaSHans Petter Selasky  */
35*d6b92ffaSHans Petter Selasky 
36*d6b92ffaSHans Petter Selasky /*
37*d6b92ffaSHans Petter Selasky  * Abstract:
38*d6b92ffaSHans Petter Selasky  *	Implementation of thread pool.
39*d6b92ffaSHans Petter Selasky  *
40*d6b92ffaSHans Petter Selasky  */
41*d6b92ffaSHans Petter Selasky 
42*d6b92ffaSHans Petter Selasky #if HAVE_CONFIG_H
43*d6b92ffaSHans Petter Selasky #  include <config.h>
44*d6b92ffaSHans Petter Selasky #endif				/* HAVE_CONFIG_H */
45*d6b92ffaSHans Petter Selasky 
46*d6b92ffaSHans Petter Selasky #include <stdlib.h>
47*d6b92ffaSHans Petter Selasky #include <string.h>
48*d6b92ffaSHans Petter Selasky #include <pthread.h>
49*d6b92ffaSHans Petter Selasky #include <complib/cl_threadpool.h>
50*d6b92ffaSHans Petter Selasky 
cleanup_mutex(void * arg)51*d6b92ffaSHans Petter Selasky static void cleanup_mutex(void *arg)
52*d6b92ffaSHans Petter Selasky {
53*d6b92ffaSHans Petter Selasky 	pthread_mutex_unlock(&((cl_thread_pool_t *) arg)->mutex);
54*d6b92ffaSHans Petter Selasky }
55*d6b92ffaSHans Petter Selasky 
thread_pool_routine(void * context)56*d6b92ffaSHans Petter Selasky static void *thread_pool_routine(void *context)
57*d6b92ffaSHans Petter Selasky {
58*d6b92ffaSHans Petter Selasky 	cl_thread_pool_t *p_thread_pool = (cl_thread_pool_t *) context;
59*d6b92ffaSHans Petter Selasky 
60*d6b92ffaSHans Petter Selasky 	do {
61*d6b92ffaSHans Petter Selasky 		pthread_mutex_lock(&p_thread_pool->mutex);
62*d6b92ffaSHans Petter Selasky 		pthread_cleanup_push(cleanup_mutex, p_thread_pool);
63*d6b92ffaSHans Petter Selasky 		while (!p_thread_pool->events)
64*d6b92ffaSHans Petter Selasky 			pthread_cond_wait(&p_thread_pool->cond,
65*d6b92ffaSHans Petter Selasky 					  &p_thread_pool->mutex);
66*d6b92ffaSHans Petter Selasky 		p_thread_pool->events--;
67*d6b92ffaSHans Petter Selasky 		pthread_cleanup_pop(1);
68*d6b92ffaSHans Petter Selasky 		/* The event has been signalled.  Invoke the callback. */
69*d6b92ffaSHans Petter Selasky 		(*p_thread_pool->pfn_callback) (p_thread_pool->context);
70*d6b92ffaSHans Petter Selasky 	} while (1);
71*d6b92ffaSHans Petter Selasky 
72*d6b92ffaSHans Petter Selasky 	return NULL;
73*d6b92ffaSHans Petter Selasky }
74*d6b92ffaSHans Petter Selasky 
cl_thread_pool_init(IN cl_thread_pool_t * const p_thread_pool,IN unsigned count,IN void (* pfn_callback)(void *),IN void * context,IN const char * const name)75*d6b92ffaSHans Petter Selasky cl_status_t cl_thread_pool_init(IN cl_thread_pool_t * const p_thread_pool,
76*d6b92ffaSHans Petter Selasky 				IN unsigned count,
77*d6b92ffaSHans Petter Selasky 				IN void (*pfn_callback) (void *),
78*d6b92ffaSHans Petter Selasky 				IN void *context, IN const char *const name)
79*d6b92ffaSHans Petter Selasky {
80*d6b92ffaSHans Petter Selasky 	int i;
81*d6b92ffaSHans Petter Selasky 
82*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_thread_pool);
83*d6b92ffaSHans Petter Selasky 	CL_ASSERT(pfn_callback);
84*d6b92ffaSHans Petter Selasky 
85*d6b92ffaSHans Petter Selasky 	memset(p_thread_pool, 0, sizeof(*p_thread_pool));
86*d6b92ffaSHans Petter Selasky 
87*d6b92ffaSHans Petter Selasky 	if (!count)
88*d6b92ffaSHans Petter Selasky 		count = cl_proc_count();
89*d6b92ffaSHans Petter Selasky 
90*d6b92ffaSHans Petter Selasky 	pthread_mutex_init(&p_thread_pool->mutex, NULL);
91*d6b92ffaSHans Petter Selasky 	pthread_cond_init(&p_thread_pool->cond, NULL);
92*d6b92ffaSHans Petter Selasky 
93*d6b92ffaSHans Petter Selasky 	p_thread_pool->events = 0;
94*d6b92ffaSHans Petter Selasky 
95*d6b92ffaSHans Petter Selasky 	p_thread_pool->pfn_callback = pfn_callback;
96*d6b92ffaSHans Petter Selasky 	p_thread_pool->context = context;
97*d6b92ffaSHans Petter Selasky 
98*d6b92ffaSHans Petter Selasky 	p_thread_pool->tid = calloc(count, sizeof(*p_thread_pool->tid));
99*d6b92ffaSHans Petter Selasky 	if (!p_thread_pool->tid) {
100*d6b92ffaSHans Petter Selasky 		cl_thread_pool_destroy(p_thread_pool);
101*d6b92ffaSHans Petter Selasky 		return CL_INSUFFICIENT_MEMORY;
102*d6b92ffaSHans Petter Selasky 	}
103*d6b92ffaSHans Petter Selasky 
104*d6b92ffaSHans Petter Selasky 	p_thread_pool->running_count = count;
105*d6b92ffaSHans Petter Selasky 
106*d6b92ffaSHans Petter Selasky 	for (i = 0; i < count; i++) {
107*d6b92ffaSHans Petter Selasky 		if (pthread_create(&p_thread_pool->tid[i], NULL,
108*d6b92ffaSHans Petter Selasky 				   thread_pool_routine, p_thread_pool) != 0) {
109*d6b92ffaSHans Petter Selasky 			cl_thread_pool_destroy(p_thread_pool);
110*d6b92ffaSHans Petter Selasky 			return CL_INSUFFICIENT_RESOURCES;
111*d6b92ffaSHans Petter Selasky 		}
112*d6b92ffaSHans Petter Selasky 	}
113*d6b92ffaSHans Petter Selasky 
114*d6b92ffaSHans Petter Selasky 	return (CL_SUCCESS);
115*d6b92ffaSHans Petter Selasky }
116*d6b92ffaSHans Petter Selasky 
cl_thread_pool_destroy(IN cl_thread_pool_t * const p_thread_pool)117*d6b92ffaSHans Petter Selasky void cl_thread_pool_destroy(IN cl_thread_pool_t * const p_thread_pool)
118*d6b92ffaSHans Petter Selasky {
119*d6b92ffaSHans Petter Selasky 	int i;
120*d6b92ffaSHans Petter Selasky 
121*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_thread_pool);
122*d6b92ffaSHans Petter Selasky 
123*d6b92ffaSHans Petter Selasky 	for (i = 0; i < p_thread_pool->running_count; i++)
124*d6b92ffaSHans Petter Selasky 		if (p_thread_pool->tid[i])
125*d6b92ffaSHans Petter Selasky 			pthread_cancel(p_thread_pool->tid[i]);
126*d6b92ffaSHans Petter Selasky 
127*d6b92ffaSHans Petter Selasky 	for (i = 0; i < p_thread_pool->running_count; i++)
128*d6b92ffaSHans Petter Selasky 		if (p_thread_pool->tid[i])
129*d6b92ffaSHans Petter Selasky 			pthread_join(p_thread_pool->tid[i], NULL);
130*d6b92ffaSHans Petter Selasky 
131*d6b92ffaSHans Petter Selasky 	p_thread_pool->running_count = 0;
132*d6b92ffaSHans Petter Selasky 
133*d6b92ffaSHans Petter Selasky 	free(p_thread_pool->tid);
134*d6b92ffaSHans Petter Selasky 
135*d6b92ffaSHans Petter Selasky 	pthread_cond_destroy(&p_thread_pool->cond);
136*d6b92ffaSHans Petter Selasky 	pthread_mutex_destroy(&p_thread_pool->mutex);
137*d6b92ffaSHans Petter Selasky 
138*d6b92ffaSHans Petter Selasky 	p_thread_pool->events = 0;
139*d6b92ffaSHans Petter Selasky }
140*d6b92ffaSHans Petter Selasky 
cl_thread_pool_signal(IN cl_thread_pool_t * const p_thread_pool)141*d6b92ffaSHans Petter Selasky cl_status_t cl_thread_pool_signal(IN cl_thread_pool_t * const p_thread_pool)
142*d6b92ffaSHans Petter Selasky {
143*d6b92ffaSHans Petter Selasky 	int ret;
144*d6b92ffaSHans Petter Selasky 	CL_ASSERT(p_thread_pool);
145*d6b92ffaSHans Petter Selasky 	pthread_mutex_lock(&p_thread_pool->mutex);
146*d6b92ffaSHans Petter Selasky 	p_thread_pool->events++;
147*d6b92ffaSHans Petter Selasky 	ret = pthread_cond_signal(&p_thread_pool->cond);
148*d6b92ffaSHans Petter Selasky 	pthread_mutex_unlock(&p_thread_pool->mutex);
149*d6b92ffaSHans Petter Selasky 	return ret;
150*d6b92ffaSHans Petter Selasky }
151