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 cl_thread_pool_init(IN cl_thread_pool_t * const p_thread_pool,
76				IN unsigned count,
77				IN void (*pfn_callback) (void *),
78				IN void *context, IN const char *const name)
79{
80	int i;
81
82	CL_ASSERT(p_thread_pool);
83	CL_ASSERT(pfn_callback);
84
85	memset(p_thread_pool, 0, sizeof(*p_thread_pool));
86
87	if (!count)
88		count = cl_proc_count();
89
90	pthread_mutex_init(&p_thread_pool->mutex, NULL);
91	pthread_cond_init(&p_thread_pool->cond, NULL);
92
93	p_thread_pool->events = 0;
94
95	p_thread_pool->pfn_callback = pfn_callback;
96	p_thread_pool->context = context;
97
98	p_thread_pool->tid = calloc(count, sizeof(*p_thread_pool->tid));
99	if (!p_thread_pool->tid) {
100		cl_thread_pool_destroy(p_thread_pool);
101		return CL_INSUFFICIENT_MEMORY;
102	}
103
104	p_thread_pool->running_count = count;
105
106	for (i = 0; i < count; i++) {
107		if (pthread_create(&p_thread_pool->tid[i], NULL,
108				   thread_pool_routine, p_thread_pool) != 0) {
109			cl_thread_pool_destroy(p_thread_pool);
110			return CL_INSUFFICIENT_RESOURCES;
111		}
112	}
113
114	return (CL_SUCCESS);
115}
116
117void cl_thread_pool_destroy(IN cl_thread_pool_t * const p_thread_pool)
118{
119	int i;
120
121	CL_ASSERT(p_thread_pool);
122
123	for (i = 0; i < p_thread_pool->running_count; i++)
124		if (p_thread_pool->tid[i])
125			pthread_cancel(p_thread_pool->tid[i]);
126
127	for (i = 0; i < p_thread_pool->running_count; i++)
128		if (p_thread_pool->tid[i])
129			pthread_join(p_thread_pool->tid[i], NULL);
130
131	p_thread_pool->running_count = 0;
132
133	free(p_thread_pool->tid);
134
135	pthread_cond_destroy(&p_thread_pool->cond);
136	pthread_mutex_destroy(&p_thread_pool->mutex);
137
138	p_thread_pool->events = 0;
139}
140
141cl_status_t cl_thread_pool_signal(IN cl_thread_pool_t * const p_thread_pool)
142{
143	int ret;
144	CL_ASSERT(p_thread_pool);
145	pthread_mutex_lock(&p_thread_pool->mutex);
146	p_thread_pool->events++;
147	ret = pthread_cond_signal(&p_thread_pool->cond);
148	pthread_mutex_unlock(&p_thread_pool->mutex);
149	return ret;
150}
151