/*++
"This software program is available to you under a choice of one of two
licenses.  You may choose to be licensed under either the GNU General Public
License (GPL) Version 2, June 1991, available at
http://www.fsf.org/copyleft/gpl.html, or the Intel BSD + Patent License,
the text of which follows:

"Recipient" has requested a license and Intel Corporation ("Intel") is willing
to grant a license for the software entitled InfiniBand(tm) System Software
(the "Software") being provided by Intel Corporation.

The following definitions apply to this License:

"Licensed Patents" means patent claims licensable by Intel Corporation which
are necessarily infringed by the use or sale of the Software alone or when
combined with the operating system referred to below.

"Recipient" means the party to whom Intel delivers this Software.
"Licensee" means Recipient and those third parties that receive a license to
any operating system available under the GNU Public License version 2.0 or
later.

Copyright (c) 1996-2003 Intel Corporation. All rights reserved.

The license is provided to Recipient and Recipient's Licensees under the
following terms.

Redistribution and use in source and binary forms of the Software, with or
without modification, are permitted provided that the following conditions are
met:
Redistributions of source code of the Software may retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form of the Software may reproduce the above
copyright notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

Neither the name of Intel Corporation nor the names of its contributors shall
be used to endorse or promote products derived from this Software without
specific prior written permission.

Intel hereby grants Recipient and Licensees a non-exclusive, worldwide,
royalty-free patent license under Licensed Patents to make, use, sell, offer
to sell, import and otherwise transfer the Software, if any, in source code
and object code form. This license shall include changes to the Software that
are error corrections or other minor changes to the Software that do not add
functionality or features when the Software is incorporated in any version of
a operating system that has been distributed under the GNU General Public
License 2.0 or later.  This patent license shall apply to the combination of
the Software and any operating system licensed under the GNU Public License
version 2.0 or later if, at the time Intel provides the Software to Recipient,
such addition of the Software to the then publicly available versions of such
operating system available under the GNU Public License version 2.0 or later
(whether in gold, beta or alpha form) causes such combination to be covered by
the Licensed Patents. The patent license shall not apply to any other
combinations which include the Software. No hardware per se is licensed
hereunder.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL INTEL OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
--*/

/*
 * Abstract:
 *	Declaration of the asynchronous processing module.
 *
 * Environment:
 *	All
 *
 * $Revision: 1.1 $
 */

#ifndef _CL_ASYNC_PROC_H_
#define _CL_ASYNC_PROC_H_

#include <complib/cl_qlist.h>
#include <complib/cl_qpool.h>
#include <complib/cl_threadpool.h>
#include <complib/cl_spinlock.h>

/****h* Component Library/Asynchronous Processor
* NAME
*	Asynchronous Processor
*
* DESCRIPTION
*	The asynchronous processor provides threads for executing queued callbacks.
*
*	The threads in the asynchronous processor wait for callbacks to be queued.
*
*	The asynchronous processor functions operate on a cl_async_proc_t structure
*	which should be treated as opaque and manipulated only through the provided
*	functions.
*
* SEE ALSO
*	Structures:
*		cl_async_proc_t, cl_async_proc_item_t
*
*	Initialization:
*		cl_async_proc_construct, cl_async_proc_init, cl_async_proc_destroy
*
*	Manipulation:
*		cl_async_proc_queue
*********/

/****s* Component Library: Asynchronous Processor/cl_async_proc_t
* NAME
*	cl_async_proc_t
*
* DESCRIPTION
*	Asynchronous processor structure.
*
*	The cl_async_proc_t structure should be treated as opaque, and should be
*	manipulated only through the provided functions.
*
* SYNOPSIS
*/
typedef struct _cl_async_proc {
	cl_thread_pool_t thread_pool;
	cl_qlist_t item_queue;
	cl_spinlock_t lock;

} cl_async_proc_t;
/*
* FIELDS
*	item_pool
*		Pool of items storing the callback function and contexts to be invoked
*		by the asynchronous processor's threads.
*
*	thread_pool
*		Thread pool that will invoke the callbacks.
*
*	item_queue
*		Queue of items that the threads should process.
*
*	lock
*		Lock used to synchronize access to the item pool and queue.
*
* SEE ALSO
*	Asynchronous Processor
*********/

/*
 * Declare the structure so we can reference it in the following function
 * prototype.
 */
typedef struct _cl_async_proc_item *__p_cl_async_proc_item_t;

/****d* Component Library: Asynchronous Processor/cl_pfn_async_proc_cb_t
* NAME
*	cl_pfn_async_proc_cb_t
*
* DESCRIPTION
*	The cl_pfn_async_proc_cb_t function type defines the prototype for
*	callbacks queued to and invoked by the asynchronous processor.
*
* SYNOPSIS
*/
typedef void
 (*cl_pfn_async_proc_cb_t) (IN struct _cl_async_proc_item * p_item);
/*
* PARAMETERS
*	p_item
*		Pointer to the cl_async_proc_item_t structure that was queued in
*		a call to cl_async_proc_queue.
*
* NOTES
*	This function type is provided as function prototype reference for the
*	function provided by users as a parameter to the cl_async_proc_queue
*	function.
*
* SEE ALSO
*	Asynchronous Processor, cl_async_proc_item_t
*********/

/****s* Component Library: Asynchronous Processor/cl_async_proc_item_t
* NAME
*	cl_async_proc_item_t
*
* DESCRIPTION
*	Asynchronous processor item structure passed to the cl_async_proc_queue
*	function to queue a callback for execution.
*
* SYNOPSIS
*/
typedef struct _cl_async_proc_item {
	cl_pool_item_t pool_item;
	cl_pfn_async_proc_cb_t pfn_callback;

} cl_async_proc_item_t;
/*
* FIELDS
*	pool_item
*		Pool item for queuing the item to be invoked by the asynchronous
*		processor's threads.  This field is defined as a pool item to
*		allow items to be managed by a pool.
*
*	pfn_callback
*		Pointer to a callback function to invoke when the item is dequeued.
*
* SEE ALSO
*	Asynchronous Processor, cl_async_proc_queue, cl_pfn_async_proc_cb_t
*********/

#ifdef __cplusplus
extern "C" {
#endif

/****f* Component Library: Asynchronous Processor/cl_async_proc_construct
* NAME
*	cl_async_proc_construct
*
* DESCRIPTION
*	The cl_async_proc_construct function initializes the state of a
*	thread pool.
*
* SYNOPSIS
*/
	void
	 cl_async_proc_construct(IN cl_async_proc_t * const p_async_proc);
/*
* PARAMETERS
*	p_async_proc
*		[in] Pointer to an asynchronous processor structure.
*
* RETURN VALUE
*	This function does not return a value.
*
* NOTES
*	Allows calling cl_async_proc_destroy without first calling
*	cl_async_proc_init.
*
*	Calling cl_async_proc_construct is a prerequisite to calling any other
*	thread pool function except cl_async_proc_init.
*
* SEE ALSO
*	Asynchronous Processor, cl_async_proc_init, cl_async_proc_destroy
*********/

/****f* Component Library: Asynchronous Processor/cl_async_proc_init
* NAME
*	cl_async_proc_init
*
* DESCRIPTION
*	The cl_async_proc_init function initialized an asynchronous processor
*	for use.
*
* SYNOPSIS
*/
	 cl_status_t
	    cl_async_proc_init(IN cl_async_proc_t * const p_async_proc,
			       IN const uint32_t thread_count,
			       IN const char *const name);
/*
* PARAMETERS
*	p_async_proc
*		[in] Pointer to an asynchronous processor structure to initialize.
*
*	thread_count
*		[in] Number of threads to be managed by the asynchronous processor.
*
*	name
*		[in] Name to associate with the threads.  The name may be up to 16
*		characters, including a terminating null character.  All threads
*		created in the asynchronous processor have the same name.
*
* RETURN VALUES
*	CL_SUCCESS if the asynchronous processor creation succeeded.
*
*	CL_INSUFFICIENT_MEMORY if there was not enough memory to inititalize
*	the asynchronous processor.
*
*	CL_ERROR if the threads could not be created.
*
* NOTES
*	cl_async_proc_init creates and starts the specified number of threads.
*	If thread_count is zero, the asynchronous processor creates as many
*	threads as there are processors in the system.
*
* SEE ALSO
*	Asynchronous Processor, cl_async_proc_construct, cl_async_proc_destroy,
*	cl_async_proc_queue
*********/

/****f* Component Library: Asynchronous Processor/cl_async_proc_destroy
* NAME
*	cl_async_proc_destroy
*
* DESCRIPTION
*	The cl_async_proc_destroy function performs any necessary cleanup
*	for a thread pool.
*
* SYNOPSIS
*/
	void
	 cl_async_proc_destroy(IN cl_async_proc_t * const p_async_proc);
/*
* PARAMETERS
*	p_async_proc
*		[in] Pointer to an asynchronous processor structure to destroy.
*
* RETURN VALUE
*	This function does not return a value.
*
* NOTES
*	This function blocks until all threads exit, and must therefore not
*	be called from any of the asynchronous processor's threads. Because of
*	its blocking nature, callers of cl_async_proc_destroy must ensure that
*	entering a wait state is valid from the calling thread context.
*
*	This function should only be called after a call to
*	cl_async_proc_construct or cl_async_proc_init.
*
* SEE ALSO
*	Asynchronous Processor, cl_async_proc_construct, cl_async_proc_init
*********/

/****f* Component Library: Asynchronous Processor/cl_async_proc_queue
* NAME
*	cl_async_proc_queue
*
* DESCRIPTION
*	The cl_async_proc_queue function queues a callback to an asynchronous
*	processor.
*
* SYNOPSIS
*/
	void
	 cl_async_proc_queue(IN cl_async_proc_t * const p_async_proc,
			     IN cl_async_proc_item_t * const p_item);
/*
* PARAMETERS
*	p_async_proc
*		[in] Pointer to an asynchronous processor structure to initialize.
*
*	p_item
*		[in] Pointer to an asynchronous processor item to queue for execution.
*		The callback and context fields of the item must be valid.
*
* RETURN VALUES
*	This function does not return a value.
*
* SEE ALSO
*	Asynchronous Processor, cl_async_proc_init, cl_pfn_async_proc_cb_t
*********/

#ifdef __cplusplus
}				/* extern "C" */
#endif
#endif				/* !defined(_CL_ASYNC_PROC_H_) */
