Blame include/complib/cl_event_wheel.h

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2004, 2005 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 event wheel abstraction.
Packit 13e616
 */
Packit 13e616
Packit 13e616
#ifndef _CL_EVENT_WHEEL_H_
Packit 13e616
#define _CL_EVENT_WHEEL_H_
Packit 13e616
Packit 13e616
#include <complib/cl_atomic.h>
Packit 13e616
#include <complib/cl_qlist.h>
Packit 13e616
#include <complib/cl_qmap.h>
Packit 13e616
#include <complib/cl_timer.h>
Packit 13e616
#include <complib/cl_spinlock.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/Event_Wheel
Packit 13e616
* NAME
Packit 13e616
*	Event_Wheel
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	The Event_Wheel provides a facility for registering delayed events
Packit 13e616
*  and getting called once they timeout.
Packit 13e616
*
Packit 13e616
*	The Event_Wheel functions operate on a cl_event_wheel_t structure
Packit 13e616
*  which should be treated as opaque and should be manipulated
Packit 13e616
*  only through the provided functions.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Structures:
Packit 13e616
*		cl_event_wheel_t
Packit 13e616
*
Packit 13e616
*	Initialization/Destruction:
Packit 13e616
*		cl_event_wheel_construct, cl_event_wheel_init, cl_event_wheel_destroy
Packit 13e616
*
Packit 13e616
*	Manipulation:
Packit 13e616
*		cl_event_wheel_reg, cl_event_wheel_unreg
Packit 13e616
*
Packit 13e616
*********/
Packit 13e616
/****f* Component Library: Event_Wheel/cl_pfn_event_aged_cb_t
Packit 13e616
* NAME
Packit 13e616
*	cl_pfn_event_aged_cb_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This typedef defines the prototype for client functions invoked
Packit 13e616
*  by the Event_Wheel.  The Event_Wheel calls the corresponding
Packit 13e616
*  client function when the specific item has aged.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef uint64_t
Packit 13e616
    (*cl_pfn_event_aged_cb_t) (IN uint64_t key,
Packit 13e616
			       IN uint32_t num_regs, IN void *context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	key
Packit 13e616
*		[in] The key used for registering the item in the call to
Packit 13e616
*		cl_event_wheel_reg.
Packit 13e616
*
Packit 13e616
*	num_regs
Packit 13e616
*		[in] The number of times this event was registered (pushed in time).
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Client specific context specified in a call to
Packit 13e616
*		cl_event_wheel_reg
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function returns the abosolute time the event should fire in [usec].
Packit 13e616
*  If lower then current time means the event should be unregistered
Packit 13e616
*  immediatly.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	This typedef provides a function prototype reference for
Packit 13e616
*  the function provided by Event_Wheel clients as a parameter
Packit 13e616
*  to the cl_event_wheel_reg function.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_reg
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****s* Component Library: Event_Wheel/cl_event_wheel_t
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	Event_Wheel structure.
Packit 13e616
*
Packit 13e616
*	The Event_Wheel is thread safe.
Packit 13e616
*
Packit 13e616
*	The cl_event_wheel_t structure should be treated as opaque and should
Packit 13e616
*  be manipulated only through the provided functions.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef struct _cl_event_wheel {
Packit 13e616
	cl_spinlock_t lock;
Packit 13e616
	cl_spinlock_t *p_external_lock;
Packit 13e616
	cl_qmap_t events_map;
Packit 13e616
	boolean_t closing;
Packit 13e616
	cl_qlist_t events_wheel;
Packit 13e616
	cl_timer_t timer;
Packit 13e616
} cl_event_wheel_t;
Packit 13e616
/*
Packit 13e616
* FIELDS
Packit 13e616
*	lock
Packit 13e616
*		Spinlock to guard internal structures.
Packit 13e616
*
Packit 13e616
*	p_external_lock
Packit 13e616
*		Reference to external spinlock to guard internal structures
Packit 13e616
*		if the event wheel is part of a larger object protected by its own lock
Packit 13e616
*
Packit 13e616
*	events_map
Packit 13e616
*		A Map holding all registered event items by their key.
Packit 13e616
*
Packit 13e616
*	closing
Packit 13e616
*		A flag indicating the event wheel is closing. This means that
Packit 13e616
*		callbacks that are called when closing == TRUE should just be ignored.
Packit 13e616
*
Packit 13e616
*	events_wheel
Packit 13e616
*		A list of the events sorted by expiration time.
Packit 13e616
*
Packit 13e616
*	timer
Packit 13e616
*		The timer scheduling event time propagation.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****s* Component Library: Event_Wheel/cl_event_wheel_reg_info_t
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_reg_info_t
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	Defines the event_wheel registration object structure.
Packit 13e616
*
Packit 13e616
*	The cl_event_wheel_reg_info_t structure is for internal use by the
Packit 13e616
*	Event_Wheel only.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
typedef struct _cl_event_wheel_reg_info {
Packit 13e616
	cl_map_item_t map_item;
Packit 13e616
	cl_list_item_t list_item;
Packit 13e616
	uint64_t key;
Packit 13e616
	cl_pfn_event_aged_cb_t pfn_aged_callback;
Packit 13e616
	uint64_t aging_time;
Packit 13e616
	uint32_t num_regs;
Packit 13e616
	void *context;
Packit 13e616
} cl_event_wheel_reg_info_t;
Packit 13e616
/*
Packit 13e616
* FIELDS
Packit 13e616
*	map_item
Packit 13e616
*		The map item of this event
Packit 13e616
*
Packit 13e616
*	list_item
Packit 13e616
*		The sorted by aging time list item
Packit 13e616
*
Packit 13e616
*	key
Packit 13e616
*		The key by which one can find the event
Packit 13e616
*
Packit 13e616
*	pfn_aged_callback
Packit 13e616
*		The clients Event-Aged callback
Packit 13e616
*
Packit 13e616
*	aging_time
Packit 13e616
*		The delta time [msec] for which the event should age.
Packit 13e616
*
Packit 13e616
*	num_regs
Packit 13e616
*		The number of times the same event (key) was registered
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		Client's context for event-aged callback.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_construct
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_construct
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function constructs an Event_Wheel object.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
void cl_event_wheel_construct(IN cl_event_wheel_t * const p_event_wheel);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	Allows calling cl_event_wheel_init and cl_event_wheel_destroy.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_init, cl_event_wheel_destroy
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_init
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_init
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function initializes an Event_Wheel object.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_event_wheel_init(IN cl_event_wheel_t * const p_event_wheel);
Packit 13e616
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	CL_SUCCESS if the operation is successful.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_destroy, cl_event_wheel_reg, cl_event_wheel_unreg
Packit 13e616
*
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_init_ex
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_init_ex
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function initializes an Event_Wheel object with an external spinlock
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_event_wheel_init_ex(IN cl_event_wheel_t * const p_event_wheel,
Packit 13e616
		       IN cl_spinlock_t * p_external_lock);
Packit 13e616
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
*	p_external_lock
Packit 13e616
*		[in] Reference to external spinlock to guard internal structures
Packit 13e616
*		if the event wheel is part of a larger object protected by its own lock
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	CL_SUCCESS if the operation is successful.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_destroy, cl_event_wheel_reg, cl_event_wheel_unreg
Packit 13e616
*
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_destroy
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_destroy
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function destroys an Event_Wheel object.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
void cl_event_wheel_destroy(IN cl_event_wheel_t * const p_event_wheel);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	This function does not returns until all client callback functions
Packit 13e616
*  been successfully finished.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_construct, cl_event_wheel_init
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_dump
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_dump
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function dumps the details of an Event_Whell object.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
void cl_event_wheel_dump(IN cl_event_wheel_t * const p_event_wheel);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	Note that this function should be called inside a lock of the event wheel!
Packit 13e616
*  It doesn't aquire the lock by itself.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_construct, cl_event_wheel_init
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_reg
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_reg
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function registers a client with an Event_Wheel object.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
cl_status_t
Packit 13e616
cl_event_wheel_reg(IN cl_event_wheel_t * const p_event_wheel,
Packit 13e616
		   IN const uint64_t key,
Packit 13e616
		   IN const uint64_t aging_time_usec,
Packit 13e616
		   IN cl_pfn_event_aged_cb_t pfn_callback,
Packit 13e616
		   IN void *const context);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
*	key
Packit 13e616
*		[in] The specifc Key by which events are registered.
Packit 13e616
*
Packit 13e616
*	aging_time_usec
Packit 13e616
*		[in] The absolute time this event should age in usec
Packit 13e616
*
Packit 13e616
*	pfn_callback
Packit 13e616
*		[in] Event Aging callback.  The Event_Wheel calls this
Packit 13e616
*		function after the time the event has registed for has come.
Packit 13e616
*
Packit 13e616
*	context
Packit 13e616
*		[in] Client context value passed to the cl_pfn_event_aged_cb_t
Packit 13e616
*		function.
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	On success an Event_Wheel CL_SUCCESS or CL_ERROR otherwise.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_unreg
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_unreg
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_unreg
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function unregisters a client event from an Event_Wheel.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
void
Packit 13e616
cl_event_wheel_unreg(IN cl_event_wheel_t * const p_event_wheel,
Packit 13e616
		     IN uint64_t key);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
*	key
Packit 13e616
*		[in] The key used for registering the event
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	This function does not return a value.
Packit 13e616
*
Packit 13e616
* NOTES
Packit 13e616
*	After the event has aged it is automatically removed from
Packit 13e616
*  the event wheel. So it should only be invoked when the need arises
Packit 13e616
*  to remove existing events before they age.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_reg
Packit 13e616
*********/
Packit 13e616
Packit 13e616
/****f* Component Library: Event_Wheel/cl_event_wheel_num_regs
Packit 13e616
* NAME
Packit 13e616
*	cl_event_wheel_num_regs
Packit 13e616
*
Packit 13e616
* DESCRIPTION
Packit 13e616
*	This function returns the number of times an event was registered.
Packit 13e616
*
Packit 13e616
* SYNOPSIS
Packit 13e616
*/
Packit 13e616
uint32_t
Packit 13e616
cl_event_wheel_num_regs(IN cl_event_wheel_t * const p_event_wheel,
Packit 13e616
			IN uint64_t key);
Packit 13e616
/*
Packit 13e616
* PARAMETERS
Packit 13e616
*	p_event_wheel
Packit 13e616
*		[in] Pointer to an Event_Wheel.
Packit 13e616
*
Packit 13e616
*	key
Packit 13e616
*		[in] The key used for registering the event
Packit 13e616
*
Packit 13e616
* RETURN VALUE
Packit 13e616
*	The number of times the event was registered.
Packit 13e616
*  0 if never registered or eventually aged.
Packit 13e616
*
Packit 13e616
* SEE ALSO
Packit 13e616
*	Event_Wheel, cl_event_wheel_reg, cl_event_wheel_unreg
Packit 13e616
*********/
Packit 13e616
Packit 13e616
END_C_DECLS
Packit 13e616
#endif				/* !defined(_CL_EVENT_WHEEL_H_) */