1aa97dd1cSKonstantin Ananyev/*
2aa97dd1cSKonstantin Ananyev * Copyright (c) 2016 Cisco and/or its affiliates.
3aa97dd1cSKonstantin Ananyev * Licensed under the Apache License, Version 2.0 (the "License");
4aa97dd1cSKonstantin Ananyev * you may not use this file except in compliance with the License.
5aa97dd1cSKonstantin Ananyev * You may obtain a copy of the License at:
6aa97dd1cSKonstantin Ananyev *
7aa97dd1cSKonstantin Ananyev *		 http://www.apache.org/licenses/LICENSE-2.0
8aa97dd1cSKonstantin Ananyev *
9aa97dd1cSKonstantin Ananyev * Unless required by applicable law or agreed to in writing, software
10aa97dd1cSKonstantin Ananyev * distributed under the License is distributed on an "AS IS" BASIS,
11aa97dd1cSKonstantin Ananyev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12aa97dd1cSKonstantin Ananyev * See the License for the specific language governing permissions and
13aa97dd1cSKonstantin Ananyev * limitations under the License.
14aa97dd1cSKonstantin Ananyev */
15aa97dd1cSKonstantin Ananyev/*
16aa97dd1cSKonstantin Ananyev * Copyright (c) 2016  Intel Corporation.
17aa97dd1cSKonstantin Ananyev * Licensed under the Apache License, Version 2.0 (the "License");
18aa97dd1cSKonstantin Ananyev * you may not use this file except in compliance with the License.
19aa97dd1cSKonstantin Ananyev * You may obtain a copy of the License at:
20aa97dd1cSKonstantin Ananyev *
21aa97dd1cSKonstantin Ananyev *     http://www.apache.org/licenses/LICENSE-2.0
22aa97dd1cSKonstantin Ananyev *
23aa97dd1cSKonstantin Ananyev * Unless required by applicable law or agreed to in writing, software
24aa97dd1cSKonstantin Ananyev * distributed under the License is distributed on an "AS IS" BASIS,
25aa97dd1cSKonstantin Ananyev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26aa97dd1cSKonstantin Ananyev * See the License for the specific language governing permissions and
27aa97dd1cSKonstantin Ananyev * limitations under the License.
28aa97dd1cSKonstantin Ananyev */
29aa97dd1cSKonstantin Ananyev
30aa97dd1cSKonstantin Ananyev#ifndef __tle_timer_h__
31aa97dd1cSKonstantin Ananyev#define __tle_timer_h__
32aa97dd1cSKonstantin Ananyev
33aa97dd1cSKonstantin Ananyev#include <stdint.h>
34aa97dd1cSKonstantin Ananyev#include <rte_config.h>
35aa97dd1cSKonstantin Ananyev#include <rte_debug.h>
36aa97dd1cSKonstantin Ananyev#include <rte_malloc.h>
37aa97dd1cSKonstantin Ananyev
38aa97dd1cSKonstantin Ananyev#ifdef __cplusplus
39aa97dd1cSKonstantin Ananyevextern "C" {
40aa97dd1cSKonstantin Ananyev#endif
41aa97dd1cSKonstantin Ananyev
42aa97dd1cSKonstantin Ananyev/** @file
43aa97dd1cSKonstantin Ananyev *	@brief timer definitions
44aa97dd1cSKonstantin Ananyev *
45aa97dd1cSKonstantin Ananyev * Design parameters:
46aa97dd1cSKonstantin Ananyev *	granularity: configurable in terms of units (i.e. cycles or ms).
47aa97dd1cSKonstantin Ananyev *	e.g. with 100ms tick
48aa97dd1cSKonstantin Ananyev *	required max period: 2.5 hours => 150 minutes => 90,000 ticks
49aa97dd1cSKonstantin Ananyev *	Rounding up to 256k ticks yields a two-level 512 slot-per-level
50aa97dd1cSKonstantin Ananyev *	wheel, resulting in a 7-hour max period.
51aa97dd1cSKonstantin Ananyev */
52aa97dd1cSKonstantin Ananyev
53aa97dd1cSKonstantin Ananyevstruct tle_timer_wheel_args {
54aa97dd1cSKonstantin Ananyev	uint32_t tick_size; /** tick size in units */
55aa97dd1cSKonstantin Ananyev
56aa97dd1cSKonstantin Ananyev	int32_t socket_id; /**< socket ID to allocate memory for. */
57aa97dd1cSKonstantin Ananyev
58aa97dd1cSKonstantin Ananyev	uint32_t max_timer; /** maximum number of timers */
59aa97dd1cSKonstantin Ananyev};
60aa97dd1cSKonstantin Ananyev
61aa97dd1cSKonstantin Ananyevstruct tle_timer_wheel;
62aa97dd1cSKonstantin Ananyev
63aa97dd1cSKonstantin Ananyev/** initialize a timer wheel */
64aa97dd1cSKonstantin Ananyevstruct tle_timer_wheel *
65aa97dd1cSKonstantin Ananyevtle_timer_create(struct tle_timer_wheel_args *prm, uint64_t now);
66aa97dd1cSKonstantin Ananyev
67aa97dd1cSKonstantin Ananyev/** free a timer wheel */
68aa97dd1cSKonstantin Ananyevvoid
69aa97dd1cSKonstantin Ananyevtle_timer_free(struct tle_timer_wheel *tw);
70aa97dd1cSKonstantin Ananyev
71aa97dd1cSKonstantin Ananyev/** start a timer */
72aa97dd1cSKonstantin Ananyevvoid *
73aa97dd1cSKonstantin Ananyevtle_timer_start(struct tle_timer_wheel *tw, void *obj, uint64_t interval);
74aa97dd1cSKonstantin Ananyev
75aa97dd1cSKonstantin Ananyev/** stop a timer */
76aa97dd1cSKonstantin Ananyevvoid
77aa97dd1cSKonstantin Ananyevtle_timer_stop(struct tle_timer_wheel *tw, void *timer);
78aa97dd1cSKonstantin Ananyev
79aa97dd1cSKonstantin Ananyev/** run the timer wheel. Call in every tick_size cycles
80aa97dd1cSKonstantin Ananyev * (e.g. equivalent of 100ms).
81aa97dd1cSKonstantin Ananyev */
82aa97dd1cSKonstantin Ananyevvoid
83aa97dd1cSKonstantin Ananyevtle_timer_expire(struct tle_timer_wheel *tw, uint64_t now);
84aa97dd1cSKonstantin Ananyev
85aa97dd1cSKonstantin Ananyev/** bulk retrieve of expired timers */
86aa97dd1cSKonstantin Ananyevint
87aa97dd1cSKonstantin Ananyevtle_timer_get_expired_bulk(struct tle_timer_wheel *tw, void *timers[],
88aa97dd1cSKonstantin Ananyev	uint32_t num);
89aa97dd1cSKonstantin Ananyev
90aa97dd1cSKonstantin Ananyev#ifdef __cplusplus
91aa97dd1cSKonstantin Ananyev}
92aa97dd1cSKonstantin Ananyev#endif
93aa97dd1cSKonstantin Ananyev
94aa97dd1cSKonstantin Ananyev#endif /* __tle_timer_h__ */
95