124 lines
4.0 KiB
C
124 lines
4.0 KiB
C
#ifndef INCLUDED_OS_TIMEWHEEL_H
|
|
#define INCLUDED_OS_TIMEWHEEL_H
|
|
|
|
#ifndef INCLUDED_OS_TYPES_H
|
|
#include <os_types.h>
|
|
#endif /*INCLUDED_OS_TYPES_H*/
|
|
|
|
#ifndef INCLUDED_OS_TIMER_H
|
|
#include <os_timer.h>
|
|
#endif /*INCLUDED_OS_TIMER_H*/
|
|
|
|
#ifndef INCLUDED_OS_MACROS_H
|
|
#include <os_macros.h>
|
|
#endif /*INCLUDED_OS_MACROS_H*/
|
|
|
|
|
|
/* -------------------------------------------------------------------------------------------------------------- */
|
|
/* */
|
|
|
|
#define OS_TIMEWHEEL_TICK_OK 0
|
|
#define OS_TIMEWHEEL_TICK_NEED_SCHEDULE 201
|
|
|
|
|
|
/* -------------------------------------------------------------------------------------------------------------- */
|
|
/* |------|------|------|------|--------|
|
|
* 6 6 6 6 8(R)
|
|
* N3 N2 N1 N0
|
|
*/
|
|
|
|
#define TWR_BITS 8
|
|
#define TWN_BITS 6
|
|
|
|
#define TWN_MASK 0x3F
|
|
|
|
#define TWR_SIZE (1<<TWR_BITS)
|
|
#define TWN_SIZE (1<<TWN_BITS)
|
|
|
|
#define TWR_MAX (0xFFu) /* 1111 1111 */
|
|
#define TWN0_MAX (0x3FFFu) /* 11 1111 1111 1111 */
|
|
#define TWN1_MAX (0xFFFFFu) /* 1111 1111 1111 1111 1111 */
|
|
#define TWN2_MAX (0x3FFFFFFu) /* 11 1111 1111 1111 1111 1111 1111 */
|
|
#define TWN3_MAX (0xFFFFFFFFu) /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
|
|
|
#define TWN_IDX(T, N) (((T)>>(TWR_BITS + (N)*TWN_BITS)) & TWN_MASK)
|
|
|
|
/* -------------------------------------------------------------------------------------------------------------- */
|
|
/* */
|
|
|
|
extern volatile os_tick_t os_timewheel__tick;
|
|
extern os_list_t os_timewheel__R[TWR_SIZE];
|
|
extern os_list_t os_timewheel__N0[TWN_SIZE];
|
|
extern os_list_t os_timewheel__N1[TWN_SIZE];
|
|
extern os_list_t os_timewheel__N2[TWN_SIZE];
|
|
extern os_list_t os_timewheel__N3[TWN_SIZE];
|
|
|
|
OS_STATIC_FORCE_INLINE
|
|
void os_timewheel_init(void) {
|
|
os_timewheel__tick = 0;
|
|
|
|
for (os_size_t i=0; i<OS_ARRAY_SIZE(os_timewheel__R); i++) {
|
|
os_list_init(&os_timewheel__R[i]);
|
|
}
|
|
for (os_size_t i=0; i<OS_ARRAY_SIZE(os_timewheel__N0); i++) {
|
|
os_list_init(&os_timewheel__N0[i]);
|
|
}
|
|
for (os_size_t i=0; i<OS_ARRAY_SIZE(os_timewheel__N1); i++) {
|
|
os_list_init(&os_timewheel__N1[i]);
|
|
}
|
|
for (os_size_t i=0; i<OS_ARRAY_SIZE(os_timewheel__N2); i++) {
|
|
os_list_init(&os_timewheel__N2[i]);
|
|
}
|
|
for (os_size_t i=0; i<OS_ARRAY_SIZE(os_timewheel__N3); i++) {
|
|
os_list_init(&os_timewheel__N3[i]);
|
|
}
|
|
}
|
|
|
|
OS_STATIC_FORCE_INLINE
|
|
os_list_t* os_timewheel_find(os_tick_t tick) {
|
|
if (tick<=TWR_MAX) {
|
|
return &os_timewheel__R[tick];
|
|
}else if (tick<=TWN0_MAX) {
|
|
return &os_timewheel__N0[TWN_IDX(tick, 0)];
|
|
}else if (tick<=TWN1_MAX) {
|
|
return &os_timewheel__N1[TWN_IDX(tick, 1)];
|
|
}else if (tick<=TWN2_MAX) {
|
|
return &os_timewheel__N2[TWN_IDX(tick, 2)];
|
|
}else if (tick<=TWN3_MAX) {
|
|
return &os_timewheel__N3[TWN_IDX(tick, 3)];
|
|
}else {
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
|
|
OS_STATIC_FORCE_INLINE
|
|
void os_timewheel_add_timer(os_timer_t* timer, os_timer_function_t function, void* userdata, os_tick_t ticks, int flags) {
|
|
os_list_remove(&timer->node);
|
|
os_timer_init(timer, function, userdata, ticks, flags);
|
|
timer->expire_tick = os_timewheel__tick + ticks;
|
|
os_list_t* wheel = os_timewheel_find(timer->expire_tick);
|
|
os_list_insert_before(wheel, &timer->node);
|
|
}
|
|
|
|
OS_STATIC_FORCE_INLINE
|
|
void os_timewheel_add_until_timer(os_timer_t* timer, os_timer_function_t function, void* userdata, os_tick_t ticks, int flags) {
|
|
os_timer_init(timer, function, userdata, ticks, flags);
|
|
timer->expire_tick = ticks;
|
|
os_list_t* wheel = os_timewheel_find(timer->expire_tick);
|
|
os_list_insert_before(wheel, &timer->node);
|
|
}
|
|
|
|
|
|
os_err_t os_timewheel_tick(void);
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
#define OS_AddTimer os_timewheel_add_timer
|
|
#define OS_AddUntilTimer os_timewheel_add_until_timer
|
|
#define OS_TimerTick os_timewheel_tick
|
|
|
|
#endif /*INCLUDED_OS_TIMEWHEEL_H*/
|