From 71804f2f7f4062cf6f6a315a817408a372e8a72a Mon Sep 17 00:00:00 2001 From: John Sanpe Date: Thu, 29 Feb 2024 23:51:41 +0800 Subject: [PATCH] feat timer: added initial functions Signed-off-by: John Sanpe --- CMakeLists.txt | 13 +++++++------ include/xdbd.h | 2 +- src/core/xdbd_timer.c | 38 ++++++++++++++++++++++++++++++++++++++ src/core/xdbd_timer.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 src/core/xdbd_timer.c create mode 100644 src/core/xdbd_timer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e97c6c..bd5cadc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,17 +11,18 @@ set(HEADERS set(SRCS ${HEADERS} - ${PROJECT_SOURCE_DIR}/src/xdbd.c ${PROJECT_SOURCE_DIR}/src/adb/adb.c - ${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb.c - ${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb_request.c ${PROJECT_SOURCE_DIR}/src/adb/command.c ${PROJECT_SOURCE_DIR}/src/adb/packet.c + ${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb.c + ${PROJECT_SOURCE_DIR}/src/adb/xdbd_adb_request.c ${PROJECT_SOURCE_DIR}/src/connection/connection.c - ${PROJECT_SOURCE_DIR}/src/event/xdbd_event.c - ${PROJECT_SOURCE_DIR}/src/event/select/xdbd_select.c - ${PROJECT_SOURCE_DIR}/src/core/xdbd_pool.c ${PROJECT_SOURCE_DIR}/src/core/xdbd_buf.c + ${PROJECT_SOURCE_DIR}/src/core/xdbd_pool.c + ${PROJECT_SOURCE_DIR}/src/core/xdbd_timer.c + ${PROJECT_SOURCE_DIR}/src/event/select/xdbd_select.c + ${PROJECT_SOURCE_DIR}/src/event/xdbd_event.c + ${PROJECT_SOURCE_DIR}/src/xdbd.c ) set( diff --git a/include/xdbd.h b/include/xdbd.h index baae989..3c088fd 100644 --- a/include/xdbd.h +++ b/include/xdbd.h @@ -27,7 +27,7 @@ typedef struct xdbd_connection_s xdbd_connection_t; typedef struct xdbd_listening_s xdbd_listening_t; typedef struct xdbd_event_s xdbd_event_t; typedef struct xdbd_buf_s xdbd_buf_t; - +typedef struct xdbd_timer_s xdbd_timer_t; typedef struct xdbd_s xdbd_t; struct xdbd_s { diff --git a/src/core/xdbd_timer.c b/src/core/xdbd_timer.c new file mode 100644 index 0000000..8ff5294 --- /dev/null +++ b/src/core/xdbd_timer.c @@ -0,0 +1,38 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#include + +BFDEV_RB_ROOT_CACHED(xdbd_timers); + +static inline long +timer_cmp(const bfdev_rb_node_t *key1, const bfdev_rb_node_t *key2, void *pdata) +{ + xdbd_timer_t *node1, *node2; + + node1 = bfdev_container_of(key1, xdbd_timer_t, rb); + node2 = bfdev_container_of(key2, xdbd_timer_t, rb); + + /* Ignoring conflicts */ + return node1->time < node2->time ? BFDEV_LT : BFDEV_BT; +} + +xdbd_timer_t * +xdbd_first_timer(void) +{ + return bfdev_rb_cached_first_entry(&xdbd_timers, xdbd_timer_t, rb); +} + +void +xdbd_add_timer(xdbd_timer_t *timer) +{ + bfdev_rb_cached_insert(&xdbd_timers, &timer->rb, timer_cmp, NULL); +} + +void +xdbd_remove_timer(xdbd_timer_t *timer) +{ + bfdev_rb_cached_remove(&xdbd_timers, &timer->rb); +} diff --git a/src/core/xdbd_timer.h b/src/core/xdbd_timer.h new file mode 100644 index 0000000..5c718b6 --- /dev/null +++ b/src/core/xdbd_timer.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2024 John Sanpe + */ + +#ifndef __XDBD_TIMER__H__ +#define __XDBD_TIMER__H__ + +#include +#include + +struct xdbd_timer_s { + bfdev_rb_node_t rb; + xdbd_msec_t time; +}; + +extern bfdev_rb_root_cached_t xdbd_timers; + +extern xdbd_timer_t * +xdbd_first_timer(void); + +extern void +xdbd_add_timer(xdbd_timer_t *timer); + +extern void +xdbd_remove_timer(xdbd_timer_t *timer); + +#endif /* __XDBD_TIMER__H__ */