1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
3219820Sjeff * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4219820Sjeff * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff *
34219820Sjeff */
35219820Sjeff
36219820Sjeff/*
37219820Sjeff * Abstract:
38219820Sjeff *	Implementation of thread pool.
39219820Sjeff *
40219820Sjeff */
41219820Sjeff
42219820Sjeff#if HAVE_CONFIG_H
43219820Sjeff#  include <config.h>
44219820Sjeff#endif				/* HAVE_CONFIG_H */
45219820Sjeff
46219820Sjeff#include <stdlib.h>
47219820Sjeff#include <string.h>
48219820Sjeff#include <pthread.h>
49219820Sjeff#include <complib/cl_threadpool.h>
50219820Sjeff
51219820Sjeffstatic void cleanup_mutex(void *arg)
52219820Sjeff{
53219820Sjeff	pthread_mutex_unlock(&((cl_thread_pool_t *) arg)->mutex);
54219820Sjeff}
55219820Sjeff
56219820Sjeffstatic void *thread_pool_routine(void *context)
57219820Sjeff{
58219820Sjeff	cl_thread_pool_t *p_thread_pool = (cl_thread_pool_t *) context;
59219820Sjeff
60219820Sjeff	do {
61219820Sjeff		pthread_mutex_lock(&p_thread_pool->mutex);
62219820Sjeff		pthread_cleanup_push(cleanup_mutex, p_thread_pool);
63219820Sjeff		while (!p_thread_pool->events)
64219820Sjeff			pthread_cond_wait(&p_thread_pool->cond,
65219820Sjeff					  &p_thread_pool->mutex);
66219820Sjeff		p_thread_pool->events--;
67219820Sjeff		pthread_cleanup_pop(1);
68219820Sjeff		/* The event has been signalled.  Invoke the callback. */
69219820Sjeff		(*p_thread_pool->pfn_callback) (p_thread_pool->context);
70219820Sjeff	} while (1);
71219820Sjeff
72219820Sjeff	return NULL;
73219820Sjeff}
74219820Sjeff
75219820Sjeffcl_status_t
76219820Sjeffcl_thread_pool_init(IN cl_thread_pool_t * const p_thread_pool,
77219820Sjeff		    IN unsigned count,
78219820Sjeff		    IN void (*pfn_callback) (void *),
79219820Sjeff		    IN void *context, IN const char *const name)
80219820Sjeff{
81219820Sjeff	int i;
82219820Sjeff
83219820Sjeff	CL_ASSERT(p_thread_pool);
84219820Sjeff	CL_ASSERT(pfn_callback);
85219820Sjeff
86219820Sjeff	memset(p_thread_pool, 0, sizeof(*p_thread_pool));
87219820Sjeff
88219820Sjeff	if (!count)
89219820Sjeff		count = cl_proc_count();
90219820Sjeff
91219820Sjeff	pthread_mutex_init(&p_thread_pool->mutex, NULL);
92219820Sjeff	pthread_cond_init(&p_thread_pool->cond, NULL);
93219820Sjeff
94219820Sjeff	p_thread_pool->events = 0;
95219820Sjeff
96219820Sjeff	p_thread_pool->pfn_callback = pfn_callback;
97219820Sjeff	p_thread_pool->context = context;
98219820Sjeff
99219820Sjeff	p_thread_pool->tid = calloc(count, sizeof(*p_thread_pool->tid));
100219820Sjeff	if (!p_thread_pool->tid) {
101219820Sjeff		cl_thread_pool_destroy(p_thread_pool);
102219820Sjeff		return CL_INSUFFICIENT_MEMORY;
103219820Sjeff	}
104219820Sjeff
105219820Sjeff	p_thread_pool->running_count = count;
106219820Sjeff
107219820Sjeff	for (i = 0; i < count; i++) {
108219820Sjeff		if (pthread_create(&p_thread_pool->tid[i], NULL,
109219820Sjeff				   thread_pool_routine, p_thread_pool) < 0) {
110219820Sjeff			cl_thread_pool_destroy(p_thread_pool);
111219820Sjeff			return CL_INSUFFICIENT_RESOURCES;
112219820Sjeff		}
113219820Sjeff	}
114219820Sjeff
115219820Sjeff	return (CL_SUCCESS);
116219820Sjeff}
117219820Sjeff
118219820Sjeffvoid cl_thread_pool_destroy(IN cl_thread_pool_t * const p_thread_pool)
119219820Sjeff{
120219820Sjeff	int i;
121219820Sjeff
122219820Sjeff	CL_ASSERT(p_thread_pool);
123219820Sjeff
124219820Sjeff	for (i = 0; i < p_thread_pool->running_count; i++)
125219820Sjeff		if (p_thread_pool->tid[i])
126219820Sjeff			pthread_cancel(p_thread_pool->tid[i]);
127219820Sjeff
128219820Sjeff	for (i = 0; i < p_thread_pool->running_count; i++)
129219820Sjeff		if (p_thread_pool->tid[i])
130219820Sjeff			pthread_join(p_thread_pool->tid[i], NULL);
131219820Sjeff
132219820Sjeff	p_thread_pool->running_count = 0;
133219820Sjeff	pthread_cond_destroy(&p_thread_pool->cond);
134219820Sjeff	pthread_mutex_destroy(&p_thread_pool->mutex);
135219820Sjeff
136219820Sjeff	p_thread_pool->events = 0;
137219820Sjeff}
138219820Sjeff
139219820Sjeffcl_status_t cl_thread_pool_signal(IN cl_thread_pool_t * const p_thread_pool)
140219820Sjeff{
141219820Sjeff	int ret;
142219820Sjeff	CL_ASSERT(p_thread_pool);
143219820Sjeff	pthread_mutex_lock(&p_thread_pool->mutex);
144219820Sjeff	p_thread_pool->events++;
145219820Sjeff	ret = pthread_cond_signal(&p_thread_pool->cond);
146219820Sjeff	pthread_mutex_unlock(&p_thread_pool->mutex);
147219820Sjeff	return ret;
148219820Sjeff}
149