Blame include/complib/cl_threadpool.h

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