]>
git.proxmox.com Git - mirror_frr.git/blob - lib/wheel.c
3 * Copyright (C) 2016 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 DEFINE_MTYPE_STATIC(LIB
, TIMER_WHEEL
, "Timer Wheel")
28 DEFINE_MTYPE_STATIC(LIB
, TIMER_WHEEL_LIST
, "Timer Wheel Slot List")
30 static int debug_timer_wheel
= 0;
32 static int wheel_timer_thread(struct thread
*t
)
34 struct listnode
*node
, *nextnode
;
35 unsigned long long curr_slot
;
36 unsigned int slots_to_skip
= 1;
37 struct timer_wheel
*wheel
;
40 wheel
= THREAD_ARG(t
);
41 THREAD_OFF(wheel
->timer
);
43 wheel
->curr_slot
+= wheel
->slots_to_skip
;
45 curr_slot
= wheel
->curr_slot
% wheel
->slots
;
47 if (debug_timer_wheel
)
48 zlog_debug("%s: Wheel Slot: %lld(%lld) count: %d",
49 __PRETTY_FUNCTION__
, wheel
->curr_slot
, curr_slot
,
50 listcount(wheel
->wheel_slot_lists
[curr_slot
]));
52 for (ALL_LIST_ELEMENTS(wheel
->wheel_slot_lists
[curr_slot
], node
,
54 (*wheel
->slot_run
)(data
);
56 while (list_isempty(wheel
->wheel_slot_lists
[(curr_slot
+ slots_to_skip
)
58 && (curr_slot
+ slots_to_skip
) % wheel
->slots
!= curr_slot
)
61 wheel
->slots_to_skip
= slots_to_skip
;
62 thread_add_timer_msec(wheel
->master
, wheel_timer_thread
, wheel
,
63 wheel
->nexttime
* slots_to_skip
, &wheel
->timer
);
68 struct timer_wheel
*wheel_init(struct thread_master
*master
, int period
,
69 size_t slots
, unsigned int (*slot_key
)(void *),
70 void (*slot_run
)(void *))
72 struct timer_wheel
*wheel
;
75 wheel
= XCALLOC(MTYPE_TIMER_WHEEL
, sizeof(struct timer_wheel
));
77 wheel
->slot_key
= slot_key
;
78 wheel
->slot_run
= slot_run
;
80 wheel
->period
= period
;
83 wheel
->master
= master
;
84 wheel
->nexttime
= period
/ slots
;
86 wheel
->wheel_slot_lists
= XCALLOC(MTYPE_TIMER_WHEEL_LIST
,
87 slots
* sizeof(struct listnode
*));
88 for (i
= 0; i
< slots
; i
++)
89 wheel
->wheel_slot_lists
[i
] = list_new();
91 thread_add_timer_msec(wheel
->master
, wheel_timer_thread
, wheel
,
92 wheel
->nexttime
, &wheel
->timer
);
97 void wheel_delete(struct timer_wheel
*wheel
)
101 for (i
= 0; i
< wheel
->slots
; i
++) {
102 list_delete(wheel
->wheel_slot_lists
[i
]);
105 THREAD_OFF(wheel
->timer
);
106 XFREE(MTYPE_TIMER_WHEEL_LIST
, wheel
->wheel_slot_lists
);
107 XFREE(MTYPE_TIMER_WHEEL
, wheel
);
110 int wheel_stop(struct timer_wheel
*wheel
)
112 THREAD_OFF(wheel
->timer
);
116 int wheel_start(struct timer_wheel
*wheel
)
119 thread_add_timer_msec(wheel
->master
, wheel_timer_thread
, wheel
,
120 wheel
->nexttime
, &wheel
->timer
);
125 int wheel_add_item(struct timer_wheel
*wheel
, void *item
)
129 slot
= (*wheel
->slot_key
)(item
);
131 if (debug_timer_wheel
)
132 zlog_debug("%s: Inserting %p: %lld %lld", __PRETTY_FUNCTION__
,
133 item
, slot
, slot
% wheel
->slots
);
134 listnode_add(wheel
->wheel_slot_lists
[slot
% wheel
->slots
], item
);
139 int wheel_remove_item(struct timer_wheel
*wheel
, void *item
)
143 slot
= (*wheel
->slot_key
)(item
);
145 if (debug_timer_wheel
)
146 zlog_debug("%s: Removing %p: %lld %lld", __PRETTY_FUNCTION__
,
147 item
, slot
, slot
% wheel
->slots
);
148 listnode_delete(wheel
->wheel_slot_lists
[slot
% wheel
->slots
], item
);