|
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 |
#if HAVE_CONFIG_H
|
|
Packit Service |
54dbc3 |
# include <config.h>
|
|
Packit Service |
54dbc3 |
#endif /* HAVE_CONFIG_H */
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
#include <math.h>
|
|
Packit Service |
54dbc3 |
#include <stdlib.h>
|
|
Packit Service |
54dbc3 |
#ifdef __CL_EVENT_WHEEL_TEST__
|
|
Packit Service |
54dbc3 |
#include <unistd.h> /* sleep() */
|
|
Packit Service |
54dbc3 |
#endif
|
|
Packit Service |
54dbc3 |
#include <complib/cl_event_wheel.h>
|
|
Packit Service |
54dbc3 |
#include <complib/cl_debug.h>
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
#define CL_DBG(fmt, ...)
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
static cl_status_t __event_will_age_before(IN const cl_list_item_t *
|
|
Packit Service |
54dbc3 |
const p_list_item, IN void *context)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
uint64_t aging_time = *((uint64_t *) context);
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t *p_event;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_list_item, cl_event_wheel_reg_info_t, list_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
if (p_event->aging_time < aging_time)
|
|
Packit Service |
54dbc3 |
return CL_SUCCESS;
|
|
Packit Service |
54dbc3 |
else
|
|
Packit Service |
54dbc3 |
return CL_NOT_FOUND;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
static void __cl_event_wheel_callback(IN void *context)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_event_wheel_t *p_event_wheel = (cl_event_wheel_t *) context;
|
|
Packit Service |
54dbc3 |
cl_list_item_t *p_list_item, *p_prev_event_list_item;
|
|
Packit Service |
54dbc3 |
cl_list_item_t *p_list_next_item;
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t *p_event;
|
|
Packit Service |
54dbc3 |
uint64_t current_time;
|
|
Packit Service |
54dbc3 |
uint64_t next_aging_time;
|
|
Packit Service |
54dbc3 |
uint32_t new_timeout;
|
|
Packit Service |
54dbc3 |
cl_status_t cl_status;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* might be during closing ... */
|
|
Packit Service |
54dbc3 |
if (p_event_wheel->closing)
|
|
Packit Service |
54dbc3 |
return;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
current_time = cl_get_time_stamp();
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
if (NULL != p_event_wheel->p_external_lock)
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* Take care of the order of acquiring locks to avoid the deadlock!
|
|
Packit Service |
54dbc3 |
* The external lock goes first.
|
|
Packit Service |
54dbc3 |
*/
|
|
Packit Service |
54dbc3 |
cl_spinlock_acquire(p_event_wheel->p_external_lock);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_spinlock_acquire(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
p_list_item = cl_qlist_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
if (p_list_item == cl_qlist_end(&p_event_wheel->events_wheel))
|
|
Packit Service |
54dbc3 |
/* the list is empty - nothing to do */
|
|
Packit Service |
54dbc3 |
goto Exit;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* we found such an item. get the p_event */
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_list_item, cl_event_wheel_reg_info_t, list_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
while (p_event->aging_time <= current_time) {
|
|
Packit Service |
54dbc3 |
/* this object has aged - invoke it's callback */
|
|
Packit Service |
54dbc3 |
if (p_event->pfn_aged_callback)
|
|
Packit Service |
54dbc3 |
next_aging_time =
|
|
Packit Service |
54dbc3 |
p_event->pfn_aged_callback(p_event->key,
|
|
Packit Service |
54dbc3 |
p_event->num_regs,
|
|
Packit Service |
54dbc3 |
p_event->context);
|
|
Packit Service |
54dbc3 |
else
|
|
Packit Service |
54dbc3 |
next_aging_time = 0;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* point to the next object in the wheel */
|
|
Packit Service |
54dbc3 |
p_list_next_item = cl_qlist_next(p_list_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* We need to retire the event if the next aging time passed */
|
|
Packit Service |
54dbc3 |
if (next_aging_time < current_time) {
|
|
Packit Service |
54dbc3 |
/* remove it from the map */
|
|
Packit Service |
54dbc3 |
cl_qmap_remove_item(&p_event_wheel->events_map,
|
|
Packit Service |
54dbc3 |
&(p_event->map_item));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* pop p_event from the wheel */
|
|
Packit Service |
54dbc3 |
cl_qlist_remove_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* delete the event info object - allocated by cl_event_wheel_reg */
|
|
Packit Service |
54dbc3 |
free(p_event);
|
|
Packit Service |
54dbc3 |
} else {
|
|
Packit Service |
54dbc3 |
/* update the required aging time */
|
|
Packit Service |
54dbc3 |
p_event->aging_time = next_aging_time;
|
|
Packit Service |
54dbc3 |
p_event->num_regs++;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* do not remove from the map - but remove from the list head and
|
|
Packit Service |
54dbc3 |
place in the correct position */
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* pop p_event from the wheel */
|
|
Packit Service |
54dbc3 |
cl_qlist_remove_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* find the event that ages just before */
|
|
Packit Service |
54dbc3 |
p_prev_event_list_item =
|
|
Packit Service |
54dbc3 |
cl_qlist_find_from_tail(&p_event_wheel->
|
|
Packit Service |
54dbc3 |
events_wheel,
|
|
Packit Service |
54dbc3 |
__event_will_age_before,
|
|
Packit Service |
54dbc3 |
&p_event->aging_time);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* insert just after */
|
|
Packit Service |
54dbc3 |
cl_qlist_insert_next(&p_event_wheel->events_wheel,
|
|
Packit Service |
54dbc3 |
p_prev_event_list_item,
|
|
Packit Service |
54dbc3 |
&p_event->list_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* as we have modified the list - restart from first item: */
|
|
Packit Service |
54dbc3 |
p_list_next_item =
|
|
Packit Service |
54dbc3 |
cl_qlist_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* advance to next event */
|
|
Packit Service |
54dbc3 |
p_list_item = p_list_next_item;
|
|
Packit Service |
54dbc3 |
if (p_list_item == cl_qlist_end(&p_event_wheel->events_wheel))
|
|
Packit Service |
54dbc3 |
/* the list is empty - nothing to do */
|
|
Packit Service |
54dbc3 |
break;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* get the p_event */
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_list_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
list_item);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* We need to restart the timer only if the list is not empty now */
|
|
Packit Service |
54dbc3 |
if (p_list_item != cl_qlist_end(&p_event_wheel->events_wheel)) {
|
|
Packit Service |
54dbc3 |
/* get the p_event */
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_list_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
list_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* start the timer to the timeout [msec] */
|
|
Packit Service |
54dbc3 |
new_timeout =
|
|
Packit Service |
54dbc3 |
(uint32_t) ((p_event->aging_time - current_time + 500) / 1000);
|
|
Packit Service |
54dbc3 |
CL_DBG("__cl_event_wheel_callback: Restart timer in: "
|
|
Packit Service |
54dbc3 |
"%u [msec]\n", new_timeout);
|
|
Packit Service |
54dbc3 |
cl_status = cl_timer_start(&p_event_wheel->timer, new_timeout);
|
|
Packit Service |
54dbc3 |
if (cl_status != CL_SUCCESS) {
|
|
Packit Service |
54dbc3 |
CL_DBG("__cl_event_wheel_callback: ERR 6200: "
|
|
Packit Service |
54dbc3 |
"Failed to start timer\n");
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* release the lock */
|
|
Packit Service |
54dbc3 |
Exit:
|
|
Packit Service |
54dbc3 |
cl_spinlock_release(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
if (NULL != p_event_wheel->p_external_lock)
|
|
Packit Service |
54dbc3 |
cl_spinlock_release(p_event_wheel->p_external_lock);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/*
|
|
Packit Service |
54dbc3 |
* Construct and Initialize
|
|
Packit Service |
54dbc3 |
*/
|
|
Packit Service |
54dbc3 |
void cl_event_wheel_construct(IN cl_event_wheel_t * const p_event_wheel)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_spinlock_construct(&(p_event_wheel->lock));
|
|
Packit Service |
54dbc3 |
cl_timer_construct(&(p_event_wheel->timer));
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_status_t cl_event_wheel_init(IN cl_event_wheel_t * const p_event_wheel)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_status_t cl_status = CL_SUCCESS;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* initialize */
|
|
Packit Service |
54dbc3 |
p_event_wheel->p_external_lock = NULL;
|
|
Packit Service |
54dbc3 |
p_event_wheel->closing = FALSE;
|
|
Packit Service |
54dbc3 |
cl_status = cl_spinlock_init(&(p_event_wheel->lock));
|
|
Packit Service |
54dbc3 |
if (cl_status != CL_SUCCESS)
|
|
Packit Service |
54dbc3 |
return cl_status;
|
|
Packit Service |
54dbc3 |
cl_qlist_init(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
cl_qmap_init(&p_event_wheel->events_map);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* init the timer with timeout */
|
|
Packit Service |
54dbc3 |
cl_status = cl_timer_init(&p_event_wheel->timer, __cl_event_wheel_callback, p_event_wheel); /* cb context */
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
return cl_status;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_status_t cl_event_wheel_init_ex(IN cl_event_wheel_t * const p_event_wheel,
|
|
Packit Service |
54dbc3 |
IN cl_spinlock_t * p_external_lock)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_status_t cl_status;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_status = cl_event_wheel_init(p_event_wheel);
|
|
Packit Service |
54dbc3 |
if (CL_SUCCESS != cl_status)
|
|
Packit Service |
54dbc3 |
return cl_status;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
p_event_wheel->p_external_lock = p_external_lock;
|
|
Packit Service |
54dbc3 |
return cl_status;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
void cl_event_wheel_dump(IN cl_event_wheel_t * const p_event_wheel)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_list_item_t *p_list_item;
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t __attribute__((__unused__)) *p_event;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
p_list_item = cl_qlist_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
while (p_list_item != cl_qlist_end(&p_event_wheel->events_wheel)) {
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_list_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
list_item);
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_dump: Found event key:<0x%"
|
|
Packit Service |
54dbc3 |
PRIx64 ">, num_regs:%d, aging time:%" PRIu64 "\n",
|
|
Packit Service |
54dbc3 |
p_event->key, p_event->num_regs, p_event->aging_time);
|
|
Packit Service |
54dbc3 |
p_list_item = cl_qlist_next(p_list_item);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
void cl_event_wheel_destroy(IN cl_event_wheel_t * const p_event_wheel)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_list_item_t *p_list_item;
|
|
Packit Service |
54dbc3 |
cl_map_item_t *p_map_item;
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t *p_event;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* we need to get a lock */
|
|
Packit Service |
54dbc3 |
cl_spinlock_acquire(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_event_wheel_dump(p_event_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* go over all the items in the list and remove them */
|
|
Packit Service |
54dbc3 |
p_list_item = cl_qlist_remove_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
while (p_list_item != cl_qlist_end(&p_event_wheel->events_wheel)) {
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_list_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
list_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_destroy: Found outstanding event"
|
|
Packit Service |
54dbc3 |
" key:<0x%" PRIx64 ">\n", p_event->key);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* remove it from the map */
|
|
Packit Service |
54dbc3 |
p_map_item = &(p_event->map_item);
|
|
Packit Service |
54dbc3 |
cl_qmap_remove_item(&p_event_wheel->events_map, p_map_item);
|
|
Packit Service |
54dbc3 |
free(p_event); /* allocated by cl_event_wheel_reg */
|
|
Packit Service |
54dbc3 |
p_list_item =
|
|
Packit Service |
54dbc3 |
cl_qlist_remove_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* destroy the timer */
|
|
Packit Service |
54dbc3 |
cl_timer_destroy(&p_event_wheel->timer);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* destroy the lock (this should be done without releasing - we don't want
|
|
Packit Service |
54dbc3 |
any other run to grab the lock at this point. */
|
|
Packit Service |
54dbc3 |
cl_spinlock_release(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
cl_spinlock_destroy(&(p_event_wheel->lock));
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_status_t cl_event_wheel_reg(IN cl_event_wheel_t * const p_event_wheel,
|
|
Packit Service |
54dbc3 |
IN const uint64_t key,
|
|
Packit Service |
54dbc3 |
IN const uint64_t aging_time_usec,
|
|
Packit Service |
54dbc3 |
IN cl_pfn_event_aged_cb_t pfn_callback,
|
|
Packit Service |
54dbc3 |
IN void *const context)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t *p_event;
|
|
Packit Service |
54dbc3 |
uint64_t timeout;
|
|
Packit Service |
54dbc3 |
uint32_t to;
|
|
Packit Service |
54dbc3 |
cl_status_t cl_status = CL_SUCCESS;
|
|
Packit Service |
54dbc3 |
cl_list_item_t *prev_event_list_item;
|
|
Packit Service |
54dbc3 |
cl_map_item_t *p_map_item;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* Get the lock on the manager */
|
|
Packit Service |
54dbc3 |
cl_spinlock_acquire(&(p_event_wheel->lock));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_event_wheel_dump(p_event_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* Make sure such a key does not exists */
|
|
Packit Service |
54dbc3 |
p_map_item = cl_qmap_get(&p_event_wheel->events_map, key);
|
|
Packit Service |
54dbc3 |
if (p_map_item != cl_qmap_end(&p_event_wheel->events_map)) {
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_reg: Already existing key:0x%"
|
|
Packit Service |
54dbc3 |
PRIx64 "\n", key);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* already there - remove it from the list as it is getting a new time */
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_map_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
map_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* remove the item from the qlist */
|
|
Packit Service |
54dbc3 |
cl_qlist_remove_item(&p_event_wheel->events_wheel,
|
|
Packit Service |
54dbc3 |
&p_event->list_item);
|
|
Packit Service |
54dbc3 |
/* and the qmap */
|
|
Packit Service |
54dbc3 |
cl_qmap_remove_item(&p_event_wheel->events_map,
|
|
Packit Service |
54dbc3 |
&p_event->map_item);
|
|
Packit Service |
54dbc3 |
} else {
|
|
Packit Service |
54dbc3 |
/* make a new one */
|
|
Packit Service |
54dbc3 |
p_event = (cl_event_wheel_reg_info_t *)
|
|
Packit Service |
54dbc3 |
malloc(sizeof(cl_event_wheel_reg_info_t));
|
|
Packit Service |
54dbc3 |
if (!p_event) {
|
|
Packit Service |
54dbc3 |
cl_status = CL_ERROR;
|
|
Packit Service |
54dbc3 |
goto Exit;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
p_event->num_regs = 0;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
p_event->key = key;
|
|
Packit Service |
54dbc3 |
p_event->aging_time = aging_time_usec;
|
|
Packit Service |
54dbc3 |
p_event->pfn_aged_callback = pfn_callback;
|
|
Packit Service |
54dbc3 |
p_event->context = context;
|
|
Packit Service |
54dbc3 |
p_event->num_regs++;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_reg: Registering event key:0x%" PRIx64
|
|
Packit Service |
54dbc3 |
" aging in %u [msec]\n", p_event->key,
|
|
Packit Service |
54dbc3 |
(uint32_t) ((p_event->aging_time - cl_get_time_stamp()) / 1000));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* If the list is empty - need to start the timer */
|
|
Packit Service |
54dbc3 |
if (cl_is_qlist_empty(&p_event_wheel->events_wheel)) {
|
|
Packit Service |
54dbc3 |
/* Edward Bortnikov 03/29/2003
|
|
Packit Service |
54dbc3 |
* ++TBD Consider moving the timer manipulation behind the list manipulation.
|
|
Packit Service |
54dbc3 |
*/
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* calculate the new timeout */
|
|
Packit Service |
54dbc3 |
timeout =
|
|
Packit Service |
54dbc3 |
(p_event->aging_time - cl_get_time_stamp() + 500) / 1000;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* stop the timer if it is running */
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* Edward Bortnikov 03/29/2003
|
|
Packit Service |
54dbc3 |
* Don't call cl_timer_stop() because it spins forever.
|
|
Packit Service |
54dbc3 |
* cl_timer_start() will invoke cl_timer_stop() by itself.
|
|
Packit Service |
54dbc3 |
*
|
|
Packit Service |
54dbc3 |
* The problematic scenario is when __cl_event_wheel_callback()
|
|
Packit Service |
54dbc3 |
* is in race condition with this code. It sets timer.in_timer_cb
|
|
Packit Service |
54dbc3 |
* to TRUE and then blocks on p_event_wheel->lock. Following this,
|
|
Packit Service |
54dbc3 |
* the call to cl_timer_stop() hangs. Following this, the whole system
|
|
Packit Service |
54dbc3 |
* enters into a deadlock.
|
|
Packit Service |
54dbc3 |
*
|
|
Packit Service |
54dbc3 |
* cl_timer_stop(&p_event_wheel->timer);
|
|
Packit Service |
54dbc3 |
*/
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* The timeout for the cl_timer_start should be given as uint32_t.
|
|
Packit Service |
54dbc3 |
if there is an overflow - warn about it. */
|
|
Packit Service |
54dbc3 |
to = (uint32_t) timeout;
|
|
Packit Service |
54dbc3 |
if (timeout > (uint32_t) timeout) {
|
|
Packit Service |
54dbc3 |
to = 0xffffffff; /* max 32 bit timer */
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_reg: timeout requested is "
|
|
Packit Service |
54dbc3 |
"too large. Using timeout: %u\n", to);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* start the timer to the timeout [msec] */
|
|
Packit Service |
54dbc3 |
cl_status = cl_timer_start(&p_event_wheel->timer, to);
|
|
Packit Service |
54dbc3 |
if (cl_status != CL_SUCCESS) {
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_reg : ERR 6203: "
|
|
Packit Service |
54dbc3 |
"Failed to start timer\n");
|
|
Packit Service |
54dbc3 |
goto Exit;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* insert the object to the qlist and the qmap */
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* BUT WE MUST INSERT IT IN A SORTED MANNER */
|
|
Packit Service |
54dbc3 |
prev_event_list_item =
|
|
Packit Service |
54dbc3 |
cl_qlist_find_from_tail(&p_event_wheel->events_wheel,
|
|
Packit Service |
54dbc3 |
__event_will_age_before,
|
|
Packit Service |
54dbc3 |
&p_event->aging_time);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_qlist_insert_next(&p_event_wheel->events_wheel,
|
|
Packit Service |
54dbc3 |
prev_event_list_item, &p_event->list_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_qmap_insert(&p_event_wheel->events_map, key, &(p_event->map_item));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
Exit:
|
|
Packit Service |
54dbc3 |
cl_spinlock_release(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
return cl_status;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
void cl_event_wheel_unreg(IN cl_event_wheel_t * const p_event_wheel,
|
|
Packit Service |
54dbc3 |
IN uint64_t key)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t *p_event;
|
|
Packit Service |
54dbc3 |
cl_map_item_t *p_map_item;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_unreg: " "Removing key:0x%" PRIx64 "\n", key);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_spinlock_acquire(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
p_map_item = cl_qmap_get(&p_event_wheel->events_map, key);
|
|
Packit Service |
54dbc3 |
if (p_map_item != cl_qmap_end(&p_event_wheel->events_map)) {
|
|
Packit Service |
54dbc3 |
/* we found such an item. */
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_map_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
map_item);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* remove the item from the qlist */
|
|
Packit Service |
54dbc3 |
cl_qlist_remove_item(&p_event_wheel->events_wheel,
|
|
Packit Service |
54dbc3 |
&(p_event->list_item));
|
|
Packit Service |
54dbc3 |
/* remove the item from the qmap */
|
|
Packit Service |
54dbc3 |
cl_qmap_remove_item(&p_event_wheel->events_map,
|
|
Packit Service |
54dbc3 |
&(p_event->map_item));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_unreg: Removed key:0x%" PRIx64 "\n",
|
|
Packit Service |
54dbc3 |
key);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* free the item */
|
|
Packit Service |
54dbc3 |
free(p_event);
|
|
Packit Service |
54dbc3 |
} else {
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_unreg: did not find key:0x%" PRIx64
|
|
Packit Service |
54dbc3 |
"\n", key);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_spinlock_release(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
uint32_t cl_event_wheel_num_regs(IN cl_event_wheel_t * const p_event_wheel,
|
|
Packit Service |
54dbc3 |
IN uint64_t key)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t *p_event;
|
|
Packit Service |
54dbc3 |
cl_map_item_t *p_map_item;
|
|
Packit Service |
54dbc3 |
uint32_t num_regs = 0;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* try to find the key in the map */
|
|
Packit Service |
54dbc3 |
CL_DBG("cl_event_wheel_num_regs: Looking for key:0x%" PRIx64 "\n", key);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_spinlock_acquire(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
p_map_item = cl_qmap_get(&p_event_wheel->events_map, key);
|
|
Packit Service |
54dbc3 |
if (p_map_item != cl_qmap_end(&p_event_wheel->events_map)) {
|
|
Packit Service |
54dbc3 |
/* ok so we can simply return it's num_regs */
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_map_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
map_item);
|
|
Packit Service |
54dbc3 |
num_regs = p_event->num_regs;
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_spinlock_release(&p_event_wheel->lock);
|
|
Packit Service |
54dbc3 |
return (num_regs);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
#ifdef __CL_EVENT_WHEEL_TEST__
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* Dump out the complete state of the event wheel */
|
|
Packit Service |
54dbc3 |
void __cl_event_wheel_dump(IN cl_event_wheel_t * const p_event_wheel)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_list_item_t *p_list_item;
|
|
Packit Service |
54dbc3 |
cl_map_item_t *p_map_item;
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg_info_t *p_event;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
printf("************** Event Wheel Dump ***********************\n");
|
|
Packit Service |
54dbc3 |
printf("Event Wheel List has %u items:\n",
|
|
Packit Service |
54dbc3 |
cl_qlist_count(&p_event_wheel->events_wheel));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
p_list_item = cl_qlist_head(&p_event_wheel->events_wheel);
|
|
Packit Service |
54dbc3 |
while (p_list_item != cl_qlist_end(&p_event_wheel->events_wheel)) {
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_list_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
list_item);
|
|
Packit Service |
54dbc3 |
printf("Event key:0x%" PRIx64 " Context:%s NumRegs:%u\n",
|
|
Packit Service |
54dbc3 |
p_event->key, (char *)p_event->context,
|
|
Packit Service |
54dbc3 |
p_event->num_regs);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* next */
|
|
Packit Service |
54dbc3 |
p_list_item = cl_qlist_next(p_list_item);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
printf("Event Map has %u items:\n",
|
|
Packit Service |
54dbc3 |
cl_qmap_count(&p_event_wheel->events_map));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
p_map_item = cl_qmap_head(&p_event_wheel->events_map);
|
|
Packit Service |
54dbc3 |
while (p_map_item != cl_qmap_end(&p_event_wheel->events_map)) {
|
|
Packit Service |
54dbc3 |
p_event =
|
|
Packit Service |
54dbc3 |
PARENT_STRUCT(p_map_item, cl_event_wheel_reg_info_t,
|
|
Packit Service |
54dbc3 |
map_item);
|
|
Packit Service |
54dbc3 |
printf("Event key:0x%" PRIx64 " Context:%s NumRegs:%u\n",
|
|
Packit Service |
54dbc3 |
p_event->key, (char *)p_event->context,
|
|
Packit Service |
54dbc3 |
p_event->num_regs);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* next */
|
|
Packit Service |
54dbc3 |
p_map_item = cl_qmap_next(p_map_item);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* The callback for aging event */
|
|
Packit Service |
54dbc3 |
/* We assume we pass a text context */
|
|
Packit Service |
54dbc3 |
static uint64_t __test_event_aging(uint64_t key, uint32_t num_regs, void *context)
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
printf("*****************************************************\n");
|
|
Packit Service |
54dbc3 |
printf("Aged key: 0x%" PRIx64 " Context:%s\n", key, (char *) context);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
int main()
|
|
Packit Service |
54dbc3 |
{
|
|
Packit Service |
54dbc3 |
cl_event_wheel_t event_wheel;
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* init complib */
|
|
Packit Service |
54dbc3 |
if (complib_init_v2() != CL_SUCCESS) {
|
|
Packit Service |
54dbc3 |
printf("complib_init_v2 failed\n");
|
|
Packit Service |
54dbc3 |
exit(1);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
/* construct */
|
|
Packit Service |
54dbc3 |
cl_event_wheel_construct(&event_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* init */
|
|
Packit Service |
54dbc3 |
cl_event_wheel_init(&event_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* Start Playing */
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg(&event_wheel, 1, /* key */
|
|
Packit Service |
54dbc3 |
cl_get_time_stamp() + 3000000, /* 3 sec lifetime */
|
|
Packit Service |
54dbc3 |
__test_event_aging, /* cb */
|
|
Packit Service |
54dbc3 |
"The First Aging Event");
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg(&event_wheel, 2, /* key */
|
|
Packit Service |
54dbc3 |
cl_get_time_stamp() + 3000000, /* 3 sec lifetime */
|
|
Packit Service |
54dbc3 |
__test_event_aging, /* cb */
|
|
Packit Service |
54dbc3 |
"The Second Aging Event");
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg(&event_wheel, 3, /* key */
|
|
Packit Service |
54dbc3 |
cl_get_time_stamp() + 3500000, /* 3.5 sec lifetime */
|
|
Packit Service |
54dbc3 |
__test_event_aging, /* cb */
|
|
Packit Service |
54dbc3 |
"The Third Aging Event");
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
__cl_event_wheel_dump(&event_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
sleep(2);
|
|
Packit Service |
54dbc3 |
cl_event_wheel_reg(&event_wheel, 2, /* key */
|
|
Packit Service |
54dbc3 |
cl_get_time_stamp() + 8000000, /* 8 sec lifetime */
|
|
Packit Service |
54dbc3 |
__test_event_aging, /* cb */
|
|
Packit Service |
54dbc3 |
"The Second Aging Event Moved");
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
__cl_event_wheel_dump(&event_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
sleep(1);
|
|
Packit Service |
54dbc3 |
/* remove the third event */
|
|
Packit Service |
54dbc3 |
cl_event_wheel_unreg(&event_wheel, 3); /* key */
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
/* get the number of registrations for the keys */
|
|
Packit Service |
54dbc3 |
printf("Event 1 Registered: %u\n",
|
|
Packit Service |
54dbc3 |
cl_event_wheel_num_regs(&event_wheel, 1));
|
|
Packit Service |
54dbc3 |
printf("Event 2 Registered: %u\n",
|
|
Packit Service |
54dbc3 |
cl_event_wheel_num_regs(&event_wheel, 2));
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
sleep(5);
|
|
Packit Service |
54dbc3 |
/* destroy */
|
|
Packit Service |
54dbc3 |
cl_event_wheel_destroy(&event_wheel);
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
complib_exit();
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
return (0);
|
|
Packit Service |
54dbc3 |
}
|
|
Packit Service |
54dbc3 |
|
|
Packit Service |
54dbc3 |
#endif /* __CL_EVENT_WHEEL_TEST__ */
|