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