/*++
"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 cl_qlock_pool_t.
 *	This object represents a threadsafe quick-pool of objects.
 *
 * Environment:
 *	All
 *
 * $Revision: 1.1 $
 */

#ifndef _CL_QLOCKPOOL_H_
#define _CL_QLOCKPOOL_H_

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

/****h* Component Library/Quick Locking Pool
* NAME
*	Quick Locking Pool
*
* DESCRIPTION
*	The Quick Locking Pool represents a thread-safe quick pool.
*
*	This object should be treated as opaque and should be
*	manipulated only through the provided functions.
*
* SEE ALSO
*	Structures:
*		cl_qlock_pool_t
*
*	Initialization:
*		cl_qlock_pool_construct, cl_qlock_pool_init, cl_qlock_pool_destroy
*
*	Manipulation
*		cl_qlock_pool_get, cl_qlock_pool_put
*********/

/****s* Component Library: Quick Locking Pool/cl_qlock_pool_t
* NAME
*	cl_qlock_pool_t
*
* DESCRIPTION
*	Quick Locking Pool structure.
*
*	This object should be treated as opaque and should
*	be manipulated only through the provided functions.
*
* SYNOPSIS
*/
typedef struct _cl_qlock_pool {
	cl_spinlock_t lock;
	cl_qpool_t pool;

} cl_qlock_pool_t;
/*
* FIELDS
*	lock
*		Spinlock guarding the pool.
*
*	pool
*		quick_pool of user objects.
*
* SEE ALSO
*	Quick Locking Pool
*********/

#ifdef __cplusplus
extern "C" {
#endif

/****f* Component Library: Quick Locking Pool/cl_qlock_pool_construct
* NAME
*	cl_qlock_pool_construct
*
* DESCRIPTION
*	This function constructs a Quick Locking Pool.
*
* SYNOPSIS
*/
	void
	 cl_qlock_pool_construct(IN cl_qlock_pool_t * const p_pool);
/*
* PARAMETERS
*	p_pool
*		[in] Pointer to a Quick Locking Pool to construct.
*
* RETURN VALUE
*	This function does not return a value.
*
* NOTES
*	Allows calling cl_qlock_pool_init, cl_qlock_pool_destroy
*
*	Calling cl_qlock_pool_construct is a prerequisite to calling any other
*	method except cl_qlock_pool_init.
*
* SEE ALSO
*	Quick Locking Pool, cl_qlock_pool_init, cl_qlock_pool_destroy
*********/

/****f* Component Library: Quick Locking Pool/cl_qlock_pool_destroy
* NAME
*	cl_qlock_pool_destroy
*
* DESCRIPTION
*	The cl_qlock_pool_destroy function destroys a node, releasing
*	all resources.
*
* SYNOPSIS
*/
	void
	 cl_qlock_pool_destroy(IN cl_qlock_pool_t * const p_pool);
/*
* PARAMETERS
*	p_pool
*		[in] Pointer to a Quick Locking Pool to destroy.
*
* RETURN VALUE
*	This function does not return a value.
*
* NOTES
*	Performs any necessary cleanup of the specified Quick Locking Pool.
*	Further operations should not be attempted on the destroyed object.
*	This function should only be called after a call to
*	cl_qlock_pool_construct or cl_qlock_pool_init.
*
* SEE ALSO
*	Quick Locking Pool, cl_qlock_pool_construct, cl_qlock_pool_init
*********/

/****f* Component Library: Quick Locking Pool/cl_qlock_pool_init
* NAME
*	cl_qlock_pool_init
*
* DESCRIPTION
*	The cl_qlock_pool_init function initializes a Quick Locking Pool for use.
*
* SYNOPSIS
*/
	 cl_status_t
	    cl_qlock_pool_init(IN cl_qlock_pool_t * const p_pool,
			       IN const size_t min_size,
			       IN const size_t max_size,
			       IN const size_t grow_size,
			       IN const size_t object_size,
			       IN cl_pfn_qpool_init_t pfn_initializer OPTIONAL,
			       IN cl_pfn_qpool_dtor_t pfn_destructor OPTIONAL,
			       IN const void *const context);
/*
* PARAMETERS
*	p_pool
*		[in] Pointer to an cl_qlock_pool_t object to initialize.
*
*	min_size
*		[in] Minimum number of objects that the pool should support. All
*		necessary allocations to allow storing the minimum number of items
*		are performed at initialization time, and all necessary callbacks
*		successfully invoked.
*
*	max_size
*		[in] Maximum number of objects to which the pool is allowed to grow.
*		A value of zero specifies no maximum.
*
*	grow_size
*		[in] Number of objects to allocate when incrementally growing the pool.
*		A value of zero disables automatic growth.
*
*	object_size
*		[in] Size, in bytes, of each object.
*
*	pfn_initializer
*		[in] Initialization callback to invoke for every new object when
*		growing the pool. This parameter is optional and may be NULL. If NULL,
*		the pool assumes the cl_pool_item_t structure describing objects is
*		located at the head of each object. See the cl_pfn_qpool_init_t
*		function type declaration for details about the callback function.
*
*	pfn_destructor
*		[in] Destructor callback to invoke for every object before memory for
*		that object is freed. This parameter is optional and may be NULL.
*		See the cl_pfn_qpool_dtor_t function type declaration for details
*		about the callback function.
*
*	context
*		[in] Value to pass to the callback functions to provide context.
*
* RETURN VALUES
*	CL_SUCCESS if the quick pool was initialized successfully.
*
*	CL_INSUFFICIENT_MEMORY if there was not enough memory to initialize the
*	quick pool.
*
*	CL_INVALID_SETTING if a the maximum size is non-zero and less than the
*	minimum size.
*
*	Other cl_status_t value returned by optional initialization callback function
*	specified by the pfn_initializer parameter.
*
* NOTES
*	Allows calling other Quick Locking Pool methods.
*
* SEE ALSO
*	Quick Locking Pool, cl_qlock_pool_construct, cl_qlock_pool_destroy
*********/

/****f* Component Library: Quick Locking Pool/cl_qlock_pool_get
* NAME
*	cl_qlock_pool_get
*
* DESCRIPTION
*	Gets an object wrapper and wire MAD from the pool.
*
* SYNOPSIS
*/
	cl_pool_item_t *cl_qlock_pool_get(IN cl_qlock_pool_t * const p_pool);
/*
* PARAMETERS
*	p_pool
*		[in] Pointer to an cl_qlock_pool_t object.
*
* RETURN VALUES
*	Returns a pointer to a cl_pool_item_t contained in the user object.
*
* NOTES
*	The object must eventually be returned to the pool with a call to
*	cl_qlock_pool_put.
*
*	The cl_qlock_pool_construct or cl_qlock_pool_init must be called before
*	using this function.
*
* SEE ALSO
*	Quick Locking Pool, cl_qlock_pool_put
*********/

/****f* Component Library: Quick Locking Pool/cl_qlock_pool_put
* NAME
*	cl_qlock_pool_put
*
* DESCRIPTION
*	Returns an object to the pool.
*
* SYNOPSIS
*/
	void
	 cl_qlock_pool_put(IN cl_qlock_pool_t * const p_pool,
			   IN cl_pool_item_t * const p_item);
/*
* PARAMETERS
*	p_pool
*		[in] Pointer to an cl_qlock_pool_t object.
*
*	p_item
*		[in] Pointer to the cl_pool_item_t in an object that was previously
*		retrieved from the pool.
*
* RETURN VALUES
*	This function does not return a value.
*
* NOTES
*	The cl_qlock_pool_construct or cl_qlock_pool_init must be called before
*	using this function.
*
* SEE ALSO
*	Quick Locking Pool, cl_qlock_pool_get
*********/

#ifdef __cplusplus
}				/* extern "C" */
#endif
#endif				/* _CL_QLOCKPOOL_H_ */
