2 * Copyright (c) 2004, 2005 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
4 * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 * Declaration of event wheel abstraction.
41 #ifndef _CL_EVENT_WHEEL_H_
42 #define _CL_EVENT_WHEEL_H_
44 #include <complib/cl_atomic.h>
45 #include <complib/cl_qlist.h>
46 #include <complib/cl_qmap.h>
47 #include <complib/cl_timer.h>
48 #include <complib/cl_spinlock.h>
51 # define BEGIN_C_DECLS extern "C" {
52 # define END_C_DECLS }
53 #else /* !__cplusplus */
54 # define BEGIN_C_DECLS
56 #endif /* __cplusplus */
59 /****h* Component Library/Event_Wheel
64 * The Event_Wheel provides a facility for registering delayed events
65 * and getting called once they timeout.
67 * The Event_Wheel functions operate on a cl_event_wheel_t structure
68 * which should be treated as opaque and should be manipulated
69 * only through the provided functions.
75 * Initialization/Destruction:
76 * cl_event_wheel_construct, cl_event_wheel_init, cl_event_wheel_destroy
79 * cl_event_wheel_reg, cl_event_wheel_unreg
82 /****f* Component Library: Event_Wheel/cl_pfn_event_aged_cb_t
84 * cl_pfn_event_aged_cb_t
87 * This typedef defines the prototype for client functions invoked
88 * by the Event_Wheel. The Event_Wheel calls the corresponding
89 * client function when the specific item has aged.
94 (*cl_pfn_event_aged_cb_t) (IN uint64_t key,
95 IN uint32_t num_regs, IN void *context);
99 * [in] The key used for registering the item in the call to
103 * [in] The number of times this event was registered (pushed in time).
106 * [in] Client specific context specified in a call to
110 * This function returns the abosolute time the event should fire in [usec].
111 * If lower then current time means the event should be unregistered
115 * This typedef provides a function prototype reference for
116 * the function provided by Event_Wheel clients as a parameter
117 * to the cl_event_wheel_reg function.
120 * Event_Wheel, cl_event_wheel_reg
123 /****s* Component Library: Event_Wheel/cl_event_wheel_t
128 * Event_Wheel structure.
130 * The Event_Wheel is thread safe.
132 * The cl_event_wheel_t structure should be treated as opaque and should
133 * be manipulated only through the provided functions.
137 typedef struct _cl_event_wheel {
139 cl_spinlock_t *p_external_lock;
141 cl_qmap_t events_map;
143 cl_qlist_t events_wheel;
149 * Spinlock to guard internal structures.
152 * Reference to external spinlock to guard internal structures
153 * if the event wheel is part of a larger object protected by its own lock
156 * A Map holding all registered event items by their key.
159 * A flag indicating the event wheel is closing. This means that
160 * callbacks that are called when closing == TRUE should just be ignored.
163 * A list of the events sorted by expiration time.
166 * The timer scheduling event time propagation.
172 /****s* Component Library: Event_Wheel/cl_event_wheel_reg_info_t
174 * cl_event_wheel_reg_info_t
177 * Defines the event_wheel registration object structure.
179 * The cl_event_wheel_reg_info_t structure is for internal use by the
184 typedef struct _cl_event_wheel_reg_info {
185 cl_map_item_t map_item;
186 cl_list_item_t list_item;
188 cl_pfn_event_aged_cb_t pfn_aged_callback;
192 cl_event_wheel_t *p_event_wheel;
193 } cl_event_wheel_reg_info_t;
197 * The map item of this event
200 * The sorted by aging time list item
203 * The key by which one can find the event
206 * The clients Event-Aged callback
209 * The delta time [msec] for which the event should age.
212 * The number of times the same event (key) was registered
215 * Client's context for event-aged callback.
218 * Pointer to this event wheel object
223 /****f* Component Library: Event_Wheel/cl_event_wheel_construct
225 * cl_event_wheel_construct
228 * This function constructs a Event_Wheel object.
232 void cl_event_wheel_construct(IN cl_event_wheel_t * const p_event_wheel);
236 * [in] Pointer to a Event_Wheel.
239 * This function does not return a value.
242 * Allows calling cl_event_wheel_init and cl_event_wheel_destroy.
245 * Event_Wheel, cl_event_wheel_init, cl_event_wheel_destroy
248 /****f* Component Library: Event_Wheel/cl_event_wheel_init
250 * cl_event_wheel_init
253 * This function initializes a Event_Wheel object.
258 cl_event_wheel_init(IN cl_event_wheel_t * const p_event_wheel);
263 * [in] Pointer to a Event_Wheel.
266 * CL_SUCCESS if the operation is successful.
269 * Event_Wheel, cl_event_wheel_destoy, cl_event_wheel_reg, cl_event_wheel_unreg
273 /****f* Component Library: Event_Wheel/cl_event_wheel_init
275 * cl_event_wheel_init
278 * This function initializes a Event_Wheel object.
283 cl_event_wheel_init_ex(IN cl_event_wheel_t * const p_event_wheel,
284 IN cl_spinlock_t * p_external_lock);
289 * [in] Pointer to a Event_Wheel.
292 * [in] Reference to external spinlock to guard internal structures
293 * if the event wheel is part of a larger object protected by its own lock
296 * CL_SUCCESS if the operation is successful.
299 * Event_Wheel, cl_event_wheel_destoy, cl_event_wheel_reg, cl_event_wheel_unreg
303 /****f* Component Library: Event_Wheel/cl_event_wheel_destroy
305 * cl_event_wheel_destroy
308 * This function destroys a Event_Wheel object.
312 void cl_event_wheel_destroy(IN cl_event_wheel_t * const p_event_wheel);
316 * [in] Pointer to a Event_Wheel.
319 * This function does not return a value.
322 * This function does not returns until all client callback functions
323 * been successfully finished.
326 * Event_Wheel, cl_event_wheel_construct, cl_event_wheel_init
329 /****f* Component Library: Event_Wheel/cl_event_wheel_dump
331 * cl_event_wheel_dump
334 * This function dumps the details of an Event_Whell object.
338 void cl_event_wheel_dump(IN cl_event_wheel_t * const p_event_wheel);
342 * [in] Pointer to a Event_Wheel.
345 * This function does not return a value.
348 * Note that this function should be called inside a lock of the event wheel!
349 * It doesn't aquire the lock by itself.
352 * Event_Wheel, cl_event_wheel_construct, cl_event_wheel_init
355 /****f* Component Library: Event_Wheel/cl_event_wheel_reg
360 * This function registers a client with a Event_Wheel object.
365 cl_event_wheel_reg(IN cl_event_wheel_t * const p_event_wheel,
366 IN const uint64_t key,
367 IN const uint64_t aging_time_usec,
368 IN cl_pfn_event_aged_cb_t pfn_callback,
369 IN void *const context);
373 * [in] Pointer to a Event_Wheel.
376 * [in] The specifc Key by which events are registered.
379 * [in] The absolute time this event should age in usec
382 * [in] Event Aging callback. The Event_Wheel calls this
383 * function after the time the event has registed for has come.
386 * [in] Client context value passed to the cl_pfn_event_aged_cb_t
390 * On success a Event_Wheel CL_SUCCESS or CL_ERROR otherwise.
393 * Event_Wheel, cl_event_wheel_unreg
396 /****f* Component Library: Event_Wheel/cl_event_wheel_unreg
398 * cl_event_wheel_unreg
401 * This function unregisters a client event from a Event_Wheel.
406 cl_event_wheel_unreg(IN cl_event_wheel_t * const p_event_wheel,
411 * [in] Pointer to a Event_Wheel.
414 * [in] The key used for registering the event
417 * This function does not return a value.
420 * After the event has aged it is automatically removed from
421 * the event wheel. So it should only be invoked when the need arises
422 * to remove existing events before they age.
425 * Event_Wheel, cl_event_wheel_reg
428 /****f* Component Library: Event_Wheel/cl_event_wheel_num_regs
430 * cl_event_wheel_num_regs
433 * This function returns the number of times an event was registered.
438 cl_event_wheel_num_regs(IN cl_event_wheel_t * const p_event_wheel,
443 * [in] Pointer to a Event_Wheel.
446 * [in] The key used for registering the event
449 * The number of times the event was registered.
450 * 0 if never registered or eventually aged.
453 * Event_Wheel, cl_event_wheel_reg, cl_event_wheel_unreg
457 #endif /* !defined(_CL_EVENT_WHEEL_H_) */