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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
38 * Implementation of thread pool.
44 #endif /* HAVE_CONFIG_H */
49 #include <complib/cl_threadpool.h>
51 static void cleanup_mutex(void *arg)
53 pthread_mutex_unlock(&((cl_thread_pool_t *) arg)->mutex);
56 static void *thread_pool_routine(void *context)
58 cl_thread_pool_t *p_thread_pool = (cl_thread_pool_t *) context;
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);
76 cl_thread_pool_init(IN cl_thread_pool_t * const p_thread_pool,
78 IN void (*pfn_callback) (void *),
79 IN void *context, IN const char *const name)
83 CL_ASSERT(p_thread_pool);
84 CL_ASSERT(pfn_callback);
86 memset(p_thread_pool, 0, sizeof(*p_thread_pool));
89 count = cl_proc_count();
91 pthread_mutex_init(&p_thread_pool->mutex, NULL);
92 pthread_cond_init(&p_thread_pool->cond, NULL);
94 p_thread_pool->events = 0;
96 p_thread_pool->pfn_callback = pfn_callback;
97 p_thread_pool->context = context;
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;
105 p_thread_pool->running_count = count;
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;
118 void cl_thread_pool_destroy(IN cl_thread_pool_t * const p_thread_pool)
122 CL_ASSERT(p_thread_pool);
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]);
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);
132 p_thread_pool->running_count = 0;
133 pthread_cond_destroy(&p_thread_pool->cond);
134 pthread_mutex_destroy(&p_thread_pool->mutex);
136 p_thread_pool->events = 0;
139 cl_status_t cl_thread_pool_signal(IN cl_thread_pool_t * const p_thread_pool)
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);