Blame include/complib/cl_threadpool.h

Packit Service 54dbc3
/*
Packit Service 54dbc3
 * Copyright (c) 2004-2007 Voltaire, Inc. All rights reserved.
Packit Service 54dbc3
 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
Packit Service 54dbc3
 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 * This software is available to you under a choice of one of two
Packit Service 54dbc3
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit Service 54dbc3
 * General Public License (GPL) Version 2, available from the file
Packit Service 54dbc3
 * COPYING in the main directory of this source tree, or the
Packit Service 54dbc3
 * OpenIB.org BSD license below:
Packit Service 54dbc3
 *
Packit Service 54dbc3
 *     Redistribution and use in source and binary forms, with or
Packit Service 54dbc3
 *     without modification, are permitted provided that the following
Packit Service 54dbc3
 *     conditions are met:
Packit Service 54dbc3
 *
Packit Service 54dbc3
 *      - Redistributions of source code must retain the above
Packit Service 54dbc3
 *        copyright notice, this list of conditions and the following
Packit Service 54dbc3
 *        disclaimer.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 *      - Redistributions in binary form must reproduce the above
Packit Service 54dbc3
 *        copyright notice, this list of conditions and the following
Packit Service 54dbc3
 *        disclaimer in the documentation and/or other materials
Packit Service 54dbc3
 *        provided with the distribution.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit Service 54dbc3
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit Service 54dbc3
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit Service 54dbc3
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit Service 54dbc3
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit Service 54dbc3
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service 54dbc3
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit Service 54dbc3
 * SOFTWARE.
Packit Service 54dbc3
 *
Packit Service 54dbc3
 */
Packit Service 54dbc3
Packit Service 54dbc3
/*
Packit Service 54dbc3
 * Abstract:
Packit Service 54dbc3
 *	Declaration of thread pool.
Packit Service 54dbc3
 */
Packit Service 54dbc3
Packit Service 54dbc3
#ifndef _CL_THREAD_POOL_H_
Packit Service 54dbc3
#define _CL_THREAD_POOL_H_
Packit Service 54dbc3
Packit Service 54dbc3
#include <pthread.h>
Packit Service 54dbc3
#include <complib/cl_types.h>
Packit Service 54dbc3
#include <complib/cl_thread.h>
Packit Service 54dbc3
Packit Service 54dbc3
#ifdef __cplusplus
Packit Service 54dbc3
#  define BEGIN_C_DECLS extern "C" {
Packit Service 54dbc3
#  define END_C_DECLS   }
Packit Service 54dbc3
#else				/* !__cplusplus */
Packit Service 54dbc3
#  define BEGIN_C_DECLS
Packit Service 54dbc3
#  define END_C_DECLS
Packit Service 54dbc3
#endif				/* __cplusplus */
Packit Service 54dbc3
Packit Service 54dbc3
BEGIN_C_DECLS
Packit Service 54dbc3
/****h* Component Library/Thread Pool
Packit Service 54dbc3
* NAME
Packit Service 54dbc3
*	Thread Pool
Packit Service 54dbc3
*
Packit Service 54dbc3
* DESCRIPTION
Packit Service 54dbc3
*	The Thread Pool manages a user specified number of threads.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	Each thread in the thread pool waits for a user initiated signal before
Packit Service 54dbc3
*	invoking a user specified callback function. All threads in the thread
Packit Service 54dbc3
*	pool invoke the same callback function.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	The thread pool functions operate on a cl_thread_pool_t structure which
Packit Service 54dbc3
*	should be treated as opaque, and should be manipulated only through the
Packit Service 54dbc3
*	provided functions.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SEE ALSO
Packit Service 54dbc3
*	Structures:
Packit Service 54dbc3
*		cl_thread_pool_t
Packit Service 54dbc3
*
Packit Service 54dbc3
*	Initialization:
Packit Service 54dbc3
*		cl_thread_pool_init, cl_thread_pool_destroy
Packit Service 54dbc3
*
Packit Service 54dbc3
*	Manipulation
Packit Service 54dbc3
*		cl_thread_pool_signal
Packit Service 54dbc3
*********/
Packit Service 54dbc3
/****s* Component Library: Thread Pool/cl_thread_pool_t
Packit Service 54dbc3
* NAME
Packit Service 54dbc3
*	cl_thread_pool_t
Packit Service 54dbc3
*
Packit Service 54dbc3
* DESCRIPTION
Packit Service 54dbc3
*	Thread pool structure.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	The cl_thread_pool_t structure should be treated as opaque, and should be
Packit Service 54dbc3
*	manipulated only through the provided functions.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SYNOPSIS
Packit Service 54dbc3
*/
Packit Service 54dbc3
typedef struct _cl_thread_pool {
Packit Service 54dbc3
	void (*pfn_callback) (void *);
Packit Service 54dbc3
	void *context;
Packit Service 54dbc3
	unsigned running_count;
Packit Service 54dbc3
	unsigned events;
Packit Service 54dbc3
	pthread_cond_t cond;
Packit Service 54dbc3
	pthread_mutex_t mutex;
Packit Service 54dbc3
	pthread_t *tid;
Packit Service 54dbc3
} cl_thread_pool_t;
Packit Service 54dbc3
/*
Packit Service 54dbc3
* FIELDS
Packit Service 54dbc3
*	pfn_callback
Packit Service 54dbc3
*		Callback function for the thread to invoke.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	context
Packit Service 54dbc3
*		Context to pass to the thread callback function.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	running_count
Packit Service 54dbc3
*		Number of threads running.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	events
Packit Service 54dbc3
*		events counter
Packit Service 54dbc3
*
Packit Service 54dbc3
*	mutex
Packit Service 54dbc3
*		mutex for cond variable protection
Packit Service 54dbc3
*
Packit Service 54dbc3
*	cond
Packit Service 54dbc3
*		conditional variable to signal an event to thread
Packit Service 54dbc3
*
Packit Service 54dbc3
*	tid
Packit Service 54dbc3
*		array of allocated thread ids.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SEE ALSO
Packit Service 54dbc3
*	Thread Pool
Packit Service 54dbc3
*********/
Packit Service 54dbc3
Packit Service 54dbc3
/****f* Component Library: Thread Pool/cl_thread_pool_init
Packit Service 54dbc3
* NAME
Packit Service 54dbc3
*	cl_thread_pool_init
Packit Service 54dbc3
*
Packit Service 54dbc3
* DESCRIPTION
Packit Service 54dbc3
*	The cl_thread_pool_init function creates the threads to be
Packit Service 54dbc3
*	managed by a thread pool.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SYNOPSIS
Packit Service 54dbc3
*/
Packit Service 54dbc3
cl_status_t
Packit Service 54dbc3
cl_thread_pool_init(IN cl_thread_pool_t * const p_thread_pool,
Packit Service 54dbc3
		    IN unsigned count,
Packit Service 54dbc3
		    IN void (*pfn_callback) (void *),
Packit Service 54dbc3
		    IN void *context, IN const char *const name);
Packit Service 54dbc3
/*
Packit Service 54dbc3
* PARAMETERS
Packit Service 54dbc3
*	p_thread_pool
Packit Service 54dbc3
*		[in] Pointer to a thread pool structure to initialize.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	thread_count
Packit Service 54dbc3
*		[in] Number of threads to be managed by the thread pool.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	pfn_callback
Packit Service 54dbc3
*		[in] Address of a function to be invoked by a thread.
Packit Service 54dbc3
*		See the cl_pfn_thread_callback_t function type definition for
Packit Service 54dbc3
*		details about the callback function.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	context
Packit Service 54dbc3
*		[in] Value to pass to the callback function.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	name
Packit Service 54dbc3
*		[in] Name to associate with the threads.  The name may be up to 16
Packit Service 54dbc3
*		characters, including a terminating null character.  All threads
Packit Service 54dbc3
*		created in the pool have the same name.
Packit Service 54dbc3
*
Packit Service 54dbc3
* RETURN VALUES
Packit Service 54dbc3
*	CL_SUCCESS if the thread pool creation succeeded.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	CL_INSUFFICIENT_MEMORY if there was not enough memory to inititalize
Packit Service 54dbc3
*	the thread pool.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	CL_ERROR if the threads could not be created.
Packit Service 54dbc3
*
Packit Service 54dbc3
* NOTES
Packit Service 54dbc3
*	cl_thread_pool_init creates and starts the specified number of threads.
Packit Service 54dbc3
*	If thread_count is zero, the thread pool creates as many threads as there
Packit Service 54dbc3
*	are processors in the system.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SEE ALSO
Packit Service 54dbc3
*	Thread Pool, cl_thread_pool_destroy,
Packit Service 54dbc3
*	cl_thread_pool_signal, cl_pfn_thread_callback_t
Packit Service 54dbc3
*********/
Packit Service 54dbc3
Packit Service 54dbc3
/****f* Component Library: Thread Pool/cl_thread_pool_destroy
Packit Service 54dbc3
* NAME
Packit Service 54dbc3
*	cl_thread_pool_destroy
Packit Service 54dbc3
*
Packit Service 54dbc3
* DESCRIPTION
Packit Service 54dbc3
*	The cl_thread_pool_destroy function performs any necessary cleanup
Packit Service 54dbc3
*	for a thread pool.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SYNOPSIS
Packit Service 54dbc3
*/
Packit Service 54dbc3
void cl_thread_pool_destroy(IN cl_thread_pool_t * const p_thread_pool);
Packit Service 54dbc3
/*
Packit Service 54dbc3
* PARAMETERS
Packit Service 54dbc3
*	p_thread_pool
Packit Service 54dbc3
*		[in] Pointer to a thread pool structure to destroy.
Packit Service 54dbc3
*
Packit Service 54dbc3
* RETURN VALUE
Packit Service 54dbc3
*	This function does not return a value.
Packit Service 54dbc3
*
Packit Service 54dbc3
* NOTES
Packit Service 54dbc3
*	This function blocks until all threads exit, and must therefore not
Packit Service 54dbc3
*	be called from any of the thread pool's threads. Because of its blocking
Packit Service 54dbc3
*	nature, callers of cl_thread_pool_destroy must ensure that entering a wait
Packit Service 54dbc3
*	state is valid from the calling thread context.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	This function should only be called after a call to
Packit Service 54dbc3
*	cl_thread_pool_init.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SEE ALSO
Packit Service 54dbc3
*	Thread Pool, cl_thread_pool_init
Packit Service 54dbc3
*********/
Packit Service 54dbc3
Packit Service 54dbc3
/****f* Component Library: Thread Pool/cl_thread_pool_signal
Packit Service 54dbc3
* NAME
Packit Service 54dbc3
*	cl_thread_pool_signal
Packit Service 54dbc3
*
Packit Service 54dbc3
* DESCRIPTION
Packit Service 54dbc3
*	The cl_thread_pool_signal function signals a single thread of
Packit Service 54dbc3
*	the thread pool to invoke the thread pool's callback function.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SYNOPSIS
Packit Service 54dbc3
*/
Packit Service 54dbc3
cl_status_t cl_thread_pool_signal(IN cl_thread_pool_t * const p_thread_pool);
Packit Service 54dbc3
/*
Packit Service 54dbc3
* PARAMETERS
Packit Service 54dbc3
*	p_thread_pool
Packit Service 54dbc3
*		[in] Pointer to a thread pool structure to signal.
Packit Service 54dbc3
*
Packit Service 54dbc3
* RETURN VALUES
Packit Service 54dbc3
*	CL_SUCCESS if the thread pool was successfully signalled.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	CL_ERROR otherwise.
Packit Service 54dbc3
*
Packit Service 54dbc3
* NOTES
Packit Service 54dbc3
*	Each call to this function wakes up at most one waiting thread in
Packit Service 54dbc3
*	the thread pool.
Packit Service 54dbc3
*
Packit Service 54dbc3
*	If all threads are running, cl_thread_pool_signal has no effect.
Packit Service 54dbc3
*
Packit Service 54dbc3
* SEE ALSO
Packit Service 54dbc3
*	Thread Pool
Packit Service 54dbc3
*********/
Packit Service 54dbc3
Packit Service 54dbc3
END_C_DECLS
Packit Service 54dbc3
#endif				/* _CL_THREAD_POOL_H_ */